简体   繁体   中英

pow() function gives wrong result

I want to execute this math function:

3^(3^1000000000) mod 1000000007

the result of this is: 930782551

But do it directly in python takes a huge amount of time, and the program hangs:

return pow(3,pow(3,1000000000),1000000007) 

So I thought that execute this will be the same:

return pow(3,pow(3,1000000000, 1000000007),1000000007) 

but the result is: 270196661

How can I get the correct result 930782551 in a reasonable time?

Based on your edit in your question, use

>>> pow(3, pow(3, 1000000000, 500000003), 1000000007)
930782551

Anything else will take forever to compute. This expression was obtained using Fermat's little theorem.

I asked a question on math.stackexchange.com. Bottom line, pow does not print the incorrect result. It is absolutely correct. Your input was wrong.

Edit

At first I thought it was an issue of sintaxis, so I answered:

return pow(3,pow(3,1000000000),1000000007)

But it takes an unreasonable amount of time. So I tried to solve the problem of the computing time, but I didn't do it in time :).

The perfect answer is the @Coldspeed one, he could solve the computing time issue just great, the whole explain is there

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM