Open In App

# Find last five digits of a given five digit number raised to power five

Given a five-digit number N., The task is to find the last five digits of the given number raised to the power of 5 after modifying it by arranging the digits as:

`first digit, third digit, fifth digit, fourth digit, second digit.`

Examples:

`Input : N = 12345Output : 71232Explanation : After modification the number becomes 13542. (13542)5 is 455422043125550171232Input : N = 10000Output : 00000`

Approach: In this problem, just implementation of the actions described in the statement is required. However, there are two catches in this problem.
The first catch is that the fifth power of a five-digit number cannot be represented by a 64-bit integer. But we do not actually need the fifth power, we need the fifth power modulo 105. And mod operation can be applied after each multiplication.
The second catch is that you need to output five digits, not the fifth power modulo 105. The difference is when the fifth digit from the end is zero. To output, a number with the leading zero one can either use corresponding formatting (%05d in printf) or extract digits and output them one by one.

Below is the implementation of the above approach :

## C++

 `// CPP program to find last five digits``// of a five digit number raised to power five` `#include ``using` `namespace` `std;` `// Function to find the last five digits``// of a five digit number raised to power five``int` `lastFiveDigits(``int` `n)``{``    ``n = (n / 10000) * 10000``        ``+ ((n / 100) % 10)``              ``* 1000``        ``+ (n % 10)``              ``* 100``        ``+ ((n / 10) % 10)``              ``* 10``        ``+ (n / 1000) % 10;` `    ``long` `long` `ans = 1;``    ``for` `(``int` `i = 0; i < 5; i++) {``        ``ans *= n;``        ``ans %= 100000;``    ``}` `    ``printf``(``"%05d"``, ans);``}` `// Driver code``int` `main()``{``    ``int` `n = 12345;` `    ``lastFiveDigits(n);` `    ``return` `0;``}`

## Java

 `// Java program to find last five digits``// of a five digit number raised to power five` `class` `GfG {` `    ``// Function to find the last five digits``    ``// of a five digit number raised to power five``    ``static` `void` `lastFiveDigits(``int` `n)``    ``{``        ``n = (n / ``10000``) * ``10000``            ``+ ((n / ``100``) % ``10``)``                  ``* ``1000``            ``+ (n % ``10``)``                  ``* ``100``            ``+ ((n / ``10``) % ``10``)``                  ``* ``10``            ``+ (n / ``1000``) % ``10``;` `        ``int` `ans = ``1``;``        ``for` `(``int` `i = ``0``; i < ``5``; i++) {``            ``ans *= n;``            ``ans %= ``100000``;``        ``}` `        ``System.out.println(ans);``    ``}` `    ``// Driver code``    ``public` `static` `void` `main(String[] args)``    ``{``        ``int` `n = ``12345``;` `        ``lastFiveDigits(n);``    ``}``}`

## Python3

 `# Python3 program to find last five digits``# of a five digit number raised to power five` `# Function to find the last five digits``# of a five digit number raised to power five``def` `lastFiveDigits(n):``    ``n ``=` `((``int``)(n ``/` `10000``) ``*` `10000` `+``        ``((``int``)(n ``/` `100``) ``%` `10``) ``*` `1000` `+` `(n ``%` `10``) ``*` `100` `+``        ``((``int``)(n ``/` `10``) ``%` `10``) ``*` `10` `+` `(``int``)(n ``/` `1000``) ``%` `10``)``    ``ans ``=` `1``    ``for` `i ``in` `range``(``5``):``        ``ans ``*``=` `n``        ``ans ``%``=` `100000``    ``print``(ans)` `# Driver code``if` `__name__ ``=``=` `'__main__'``:``    ``n ``=` `12345` `    ``lastFiveDigits(n)` `# This code contributed by PrinciRaj1992`

## C#

 `// C# program to find last five``// digits of a five digit number``// raised to power five``using` `System;` `class` `GFG``{` `    ``// Function to find the last``    ``// five digits of a five digit``    ``// number raised to power five``    ``public` `static` `void` `lastFiveDigits(``int` `n)``    ``{``        ``n = (n / 10000) * 10000 +``           ``((n / 100) % 10) * 1000 +``            ``(n % 10) * 100 +``           ``((n / 10) % 10) * 10 +``            ``(n / 1000) % 10;` `        ``int` `ans = 1;``        ``for` `(``int` `i = 0; i < 5; i++)``        ``{``            ``ans *= n;``            ``ans %= 100000;``        ``}` `        ``Console.WriteLine(ans);``    ``}` `    ``// Driver code``    ``public` `static` `void` `Main(``string``[] args)``    ``{``        ``int` `n = 12345;` `        ``lastFiveDigits(n);``    ``}``}` `// This code is contributed``// by Shrikant13`

## Javascript

 ``

## PHP

 ``

Output

```71232

```

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

## Approach: Modified Digit Arrangement and Modulo Operation”

The “Modified Digit Arrangement and Modulo Operation” approach to finding the last five digits of a five-digit number raised to power five consists of the following steps:

1. Modify the number as per the given arrangement of digits.
2. Calculate the power of 5 of the modified number.
3. Take modulo with 100000 (10^5) to get the last five digits.

The key idea behind this approach is to use the given arrangement of digits to modify the original number in such a way that the resulting number has the same last five digits as the original number when raised to power five. Then, by taking modulo with 100000, we can obtain the last five digits of the resulting number.

## C++

 `#include ` `// Function to calculate (base^exponent) % modulus using modular exponentiation``int` `power(``int` `base, ``int` `exponent, ``int` `modulus) {``    ``if` `(exponent == 0)``        ``return` `1;``    ` `    ``long` `long` `result = 1;``    ``long` `long` `x = base % modulus;``    ` `    ``while` `(exponent > 0) {``        ``if` `(exponent % 2 == 1)``            ``result = (result * x) % modulus;``        ` `        ``x = (x * x) % modulus;``        ``exponent /= 2;``    ``}``    ` `    ``return` `result;``}` `// Function to find the last five digits of the number N after performing the given operations``int` `findLastFiveDigits(``int` `N) {``    ``// Extract individual digits of the number``    ``int` `digit1 = N / 10000;``    ``int` `digit2 = (N / 1000) % 10;``    ``int` `digit3 = (N / 100) % 10;``    ``int` `digit4 = (N / 10) % 10;``    ``int` `digit5 = N % 10;` `    ``// Modify the number as per the given arrangement of digits``    ``int` `new_N = digit1 * 10000 + digit3 * 1000 + digit5 * 100 + digit4 * 10 + digit2;``    ``std::cout << ``"Modified number: "` `<< new_N << std::endl;` `    ``// Calculate the power of 5 of the modified number using modular exponentiation``    ``int` `powerVal = power(new_N, 5, 100000);``    ``std::cout << ``"Power of 5: "` `<< powerVal << std::endl;` `    ``// Take modulo with 100000 to get the last five digits``    ``int` `last_five_digits = powerVal % 100000;``    ``std::cout << ``"Last five digits: "` `<< last_five_digits << std::endl;` `    ``return` `last_five_digits;``}` `int` `main() {``    ``std::cout << findLastFiveDigits(12345) << std::endl;``    ``std::cout << findLastFiveDigits(10000) << std::endl;` `    ``// This code is contributed by Shivam Tiwari` `    ``return` `0;``}`

## Java

 `public` `class` `GFG {``    ``public` `static` `void` `main(String[] args)``    ``{``        ``System.out.println(findLastFiveDigits(``12345``));``        ``System.out.println(findLastFiveDigits(``10000``));``    ``}` `    ``public` `static` `int` `findLastFiveDigits(``int` `N)``    ``{``        ``// Modify the number as per the given arrangement of``        ``// digits``        ``int` `new_N = Integer.parseInt(``            ``""` `+ Integer.toString(N).charAt(``0``)``            ``+ Integer.toString(N).charAt(``2``)``            ``+ Integer.toString(N).charAt(``4``)``            ``+ Integer.toString(N).charAt(``3``)``            ``+ Integer.toString(N).charAt(``1``));``        ``System.out.println(``"Modified number: "` `+ new_N);` `        ``// Calculate the power of 5 of the modified number``        ``long` `power = (``long``)Math.pow(new_N, ``5``);``        ``System.out.println(``"Power of 5: "` `+ power);` `        ``// Take modulo with 100000 to get the last five``        ``// digits``        ``int` `last_five_digits = (``int``)(power % ``100000``);``        ``System.out.println(``"Last five digits: "``                           ``+ last_five_digits);` `        ``return` `last_five_digits;``    ``}``}`

## Python3

 `def` `find_last_five_digits(N):``    ``# Modify the number as per the given arrangement of digits``    ``new_N ``=` `int``(``str``(N)[``0``] ``+` `str``(N)[``2``] ``+` `str``(N)[``4``] ``+` `str``(N)[``3``] ``+` `str``(N)[``1``])``    ``print``(f``"Modified number: {new_N}"``)` `    ``# Calculate the power of 5 of the modified number``    ``power ``=` `new_N ``*``*` `5``    ``print``(f``"Power of 5: {power}"``)` `    ``# Take modulo with 100000 to get the last five digits``    ``last_five_digits ``=` `power ``%` `100000``    ``print``(f``"Last five digits: {last_five_digits}"``)` `    ``return` `last_five_digits`  `print``(find_last_five_digits(``12345``))``print``(find_last_five_digits(``10000``))`

## C#

 `using` `System;` `public` `class` `GFG {``  ``public` `static` `void` `Main(``string``[] args)``  ``{``    ``Console.WriteLine(findLastFiveDigits(12345));``    ``Console.WriteLine(findLastFiveDigits(10000));``  ``}` `  ``public` `static` `int` `findLastFiveDigits(``int` `N)``  ``{` `    ``// Modify the number as per the given arrangement of``    ``// digits``    ``int` `new_N = ``int``.Parse(``      ``""` `+ N.ToString()[0]``      ``+ N.ToString()[2]``      ``+ N.ToString()[4]``      ``+ N.ToString()[3]``      ``+ N.ToString()[1]);``    ``Console.WriteLine(``"Modified number: "` `+ new_N);` `    ``// Calculate the power of 5 of the modified number``    ``long` `power = (``long``)Math.Pow(new_N, 5);``    ``Console.WriteLine(``"Power of 5: "` `+ Math.Abs(power));` `    ``// Take modulo with 100000 to get the last five``    ``// digits``    ``int` `last_five_digits = (``int``)(power % 100000);``    ``Console.WriteLine(``"Last five digits: "``                      ``+ Math.Abs(last_five_digits));` `    ``return` `Math.Abs(last_five_digits);``  ``}``}``// This code is contributed by shiv1o43g`

## Javascript

 `function` `findLastFiveDigits(N) {``  ``// Modify the number as per the given arrangement of digits``  ``let new_N = parseInt(N.toString()[0] + N.toString()[2] + N.toString()[4] + N.toString()[3] + N.toString()[1]);``  ``console.log(``"Modified number: "` `+ new_N);` `  ``// Calculate the power of 5 of the modified number``  ``let power = Math.pow(new_N, 5);``  ``console.log(``"Power of 5: "` `+ power);` `  ``// Take modulo with 100000 to get the last five digits``  ``let lastFiveDigits = power % 100000;``  ``console.log(``"Last five digits: "` `+ lastFiveDigits);` `  ``return` `lastFiveDigits;``}` `console.log(findLastFiveDigits(12345));``console.log(findLastFiveDigits(10000));`

Output

```Modified number: 13542
Power of 5: 455422043125550171232
Last five digits: 71232
71232
Modified number: 10000
Power of 5: 100000000000000000000
Last five digits: 0
0

```

The time complexity  is O(1)
The auxiliary space is also O(1)