Over a million developers have joined DZone. {{announcement.body}}
{{announcement.title}}

# How Do I Calculate the Remainder for Extremely Large Exponential Numbers using Java?

DZone 's Guide to

# How Do I Calculate the Remainder for Extremely Large Exponential Numbers using Java?

· Java Zone ·
Free Resource

Comment (0)

Save
{{ articles.views | formatCount}} Views

How do i calculate the remainder for extremely large exponential numbers using java ?
eg. (48^26)/2401
I thought the answer was worth reproducing as there is a surprisingly simple solution.
An answer is to modulus the value in each iteration of calculating the power

```(a * b)% n
((A * n + AA)*(B * n + BB))% n             | AA=a%n & BB=b%n
(A * B * n^2+ A * N * BB + AA * B * n + AA * BB)% n
AA * BB % n                                   since x*n%n ==0(a % n)*(b % n)% n```
In your case, you can write
`48^26%2401(48^2)^13%2401`
as
```int n =48;
for(int i =1; i <26; i++)
n =(n *48)%2401;
System.out.println(n);
int n2 =48*48;
for(int i =1; i <13; i++)
n2 =(n2 *48*48)%2401;

System.out.println(n2);
System.out.println(BigInteger.valueOf(48).pow(26).mod(BigInteger.valueOf(2401)));```
prints
```1128
1128
1128```
Topics:

Comment (0)

Save
{{ articles.views | formatCount}} Views

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.