Result outputs 0

Hi there,

I am just testing a simple maths equation here.
Lets say i want to perform (12345678901234567890^9876543219876543) mod 12345678975313579, wolframalpha output "8676260740635552". However, i am getting an output of "0" in my program. Much help appreciated, thank you!

1
2
3
4
 unsigned long long hi = 0;
    hi = pow(12345678901234567890.0,9876543219876543);
    cout << hi%12345678975313579;
    return 0;
The value returned by pow is probably too big for double to handle so it returns infinity. I think it's undefined what will happen when you convert infinity to an integer but in your case it looks like hi got the value 0.
its out of range...
you need handmade calculator
it will take enormous calculation time!!
my program takes 16sec for simple 9^500 =

1322070819480806636890455259752144365965422032752148167664920368
2268285973467048995407783138506080619639097776968725823559509545
8210061891186534272525795367402762022519832080387801477422896484
1274390400117588618041128947815623094438061566173054086674490506
1781254803444055470543970388958174653682549161362208302685637785
8229022841639830788789691855640408489893760937324217184635993869
5516765018940588109060426089671438864102814350385648747165832010
614366132173102768902855220001
Exponentiation and modulo on big numbers can be computed more efficiently if it's done as one operation. That's why some Big Number libraries have functions for doing exponentiation and modulo together. Take a look at the GMP library, it has the function mpz_powm that does what you want: https://gmplib.org/manual/Integer-Exponentiation.html
Last edited on
Thanks for the inputs, i will take a look. Thank you!
i think that your compiler doesnt support out of range number so it will output what you initialized it , then it is 0
Topic archived. No new replies allowed.