Open In App

Last digit in a power of 2

Given a number n, we need to find the last digit of 2n
 

Input : n = 4 
Output : 6 
The last digit in 2^4 = 16 is 6
Input : n = 11 
Output : 8 
The last digit in 2^11 = 2048 is 8 

A Naive Solution is to first compute power = pow(2, n), then find the last digit in power using power % 10. This solution is inefficient and also has an integer arithmetic issue for slightly large n.
An Efficient Solution is based on the fact that the last digits repeat in cycles of 4 if we leave 2^0 which is 1. Powers of 2 (starting from 2^1) are 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, … 
We can notice that the last digits are 2, 4, 8, 6, 2, 4, 8, 6, 2, 4, 8, …
1) We compute rem = n % 4. Note that the last rem will have a value from 0 to 3. 
2) We return the last digit according to the value of the remainder. 
 

Remainder   Last Digit
1 2
2 4
3 8
0 6

Illustration : Let n = 11, rem = n % 4 = 3. Last digit in 2^3 is 8 which is same as last digit of 2^11.




// C++ program to find last digit in a power of 2.
#include <bits/stdc++.h>
using namespace std;
 
int lastDigit2PowerN(int n)
{
 
    // Corner case
    if (n == 0)
        return 1;
 
    // Find the shift in current cycle
    // and return value accordingly
    else if (n % 4 == 1)
        return 2;
    else if (n % 4 == 2)
        return 4;
    else if (n % 4 == 3)
        return 8;
    else
        return 6; // When n % 4 == 0
}
 
// Driver code
int main()
{
    for (int n = 0; n < 20; n++)
        cout << lastDigit2PowerN(n) << " ";
    return 0;
}




// Java program to find last
// digit in a power of 2.
import java.io.*;
import java.util.*;
 
class GFG{
     
static int lastDigit2PowerN(int n)
{
     
    // Corner case
    if (n == 0)
        return 1;
 
    // Find the shift in current cycle
    // and return value accordingly
    else if (n % 4 == 1)
        return 2;
    else if (n % 4 == 2)
        return 4;
    else if (n % 4 == 3)
        return 8;
    else
        return 6; // When n % 4 == 0
}
 
// Driver code
public static void main(String[] args)
{
    for (int n = 0; n < 20; n++)
    System.out.print(lastDigit2PowerN(n) + " ");
}
}
 
// This code is contributed by coder001




# Python3 program to find last
# digit in a power of 2.
def lastDigit2PowerN(n):
     
    # Corner case
    if n == 0:
        return 1
 
    # Find the shift in current cycle
    # and return value accordingly
    elif n % 4 == 1:
        return 2
    elif n % 4 == 2:
        return 4
    elif n % 4 == 3:
        return 8
    else:
        return 6 # When n % 4 == 0
 
# Driver code
for n in range(20):
    print(lastDigit2PowerN(n), end = " ")
 
# This code is contributed by divyeshrabadiya07   




// C# program to find last
// digit in a power of 2.
using System;
class GFG{
     
static int lastDigit2PowerN(int n)
{
     
    // Corner case
    if (n == 0)
        return 1;
 
    // Find the shift in current cycle
    // and return value accordingly
    else if (n % 4 == 1)
        return 2;
    else if (n % 4 == 2)
        return 4;
    else if (n % 4 == 3)
        return 8;
    else
        return 6; // When n % 4 == 0
}
 
// Driver code
public static void Main(string[] args)
{
    for (int n = 0; n < 20; n++)
    {
        Console.Write(lastDigit2PowerN(n) + " ");
    }
}
}
 
// This code is contributed by rutvik_56




<script>
 
      // JavaScript program to find
      // last digit in a power of 2.
 
      function lastDigit2PowerN(n)
      {
        // Corner case
        if (n == 0)
        return 1;
         
        // Find the shift in current cycle
        // and return value accordingly
        else if (n % 4 == 1)
        return 2;
        else if (n % 4 == 2)
        return 4;
        else if (n % 4 == 3)
        return 8;
        else
        return 6; // When n % 4 == 0
      }
 
      // Driver code
      for (var n = 0; n < 20; n++)
      document.write(lastDigit2PowerN(n) + " ");
       
    </script>

Output
1 2 4 8 6 2 4 8 6 2 4 8 6 2 4 8 6 2 4 8







Time Complexity: O(1) 
Auxiliary Space: O(1)
Can we generalize it for any input numbers? Please refer Find Last Digit of a^b for Large Numbers

Approach: Using Bitwise Operators

Steps:

Below is the code implementation of the above approach:




#include<bits/stdc++.h>
using namespace std;
 
int last_digit_of_2n(int n) {
    // compute rem = n % 4
    int rem = n & 3; // equivalent to n % 4
 
    // return the last digit based on the remainder
    if (rem == 0) return 6;
    else if (rem == 1) return 2;
    else if (rem == 2) return 4;
    else return 8;
}
 
int main() {
    int n=4;
    cout << last_digit_of_2n(n) << endl;
    return 0;
}




/*package whatever //do not write package name here */
 
import java.io.*;
 
public class LastDigitOf2N {
 
    // Function to compute the last digit of 2^n
    static int lastDigitOf2N(int n) {
        // Compute rem = n % 4
        int rem = n & 3; // Equivalent to n % 4
 
        // Return the last digit based on the remainder
        if (rem == 0) return 6;
        else if (rem == 1) return 2;
        else if (rem == 2) return 4;
        else return 8;
    }
 
    public static void main(String[] args) {
        int n = 4;
        System.out.println(lastDigitOf2N(n));
    }
}
 
// This code is contributed by guptapratik




def last_digit_of_2n(n):
    # Compute rem = n % 4
    rem = n % 4
 
    # Return the last digit based on the remainder
    if rem == 0:
        return 6
    elif rem == 1:
        return 2
    elif rem == 2:
        return 4
    else:
        return 8
 
def main():
    n = 4
    print(last_digit_of_2n(n))
 
if __name__ == "__main__":
    main()




using System;
 
class Program
{
    static int LastDigitOf2N(int n)
    {
        // Compute rem = n % 4
        int rem = n & 3; // Equivalent to n % 4
 
        // Return the last digit based on the remainder
        if (rem == 0) return 6;
        else if (rem == 1) return 2;
        else if (rem == 2) return 4;
        else return 8;
    }
 
    static void Main()
    {
        int n = 4;
        Console.WriteLine(LastDigitOf2N(n));
    }
}




function lastDigitOf2n(n) {
    // Compute rem = n % 4
    const rem = n % 4;
 
    // Return the last digit based on the remainder
    if (rem === 0) return 6;
    else if (rem === 1) return 2;
    else if (rem === 2) return 4;
    else return 8;
}
 
// Test the function
const n = 4;
console.log(lastDigitOf2n(n));

Output
6








Time Complexity: O(1)
Auxiliary Space: O(1)


Article Tags :