0

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?

developer_hatch
  • 15,898
  • 3
  • 42
  • 75
  • Your WA expression should've been `(3^((3^1000000000)mod1000000007))mod1000000007`. What you've put there and what you put here aren't the same. – cs95 Jun 10 '17 at 20:51
  • Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See [mcve]. – Moses Koledoye Jun 10 '17 at 21:00
  • You need Fermat's Little theorem / Euler's theorem to do this efficiently. Fortunately, 1000000007 is prime so it's easy to calculate the totient function for it. – PM 2Ring Jun 10 '17 at 21:45
  • @PM2Ring I asked a question on math.stackexchange to figure out how to compute this. Take a look at my answer. – cs95 Jun 10 '17 at 21:46
  • The question is a very good one, I suggest an edit putting the original pow function that takes a huge amount of time, and ask how to make it in a reasonable time – developer_hatch Jun 11 '17 at 01:29
  • @MosesKoledoye, the question is a really good one, an a valid one, the accepted answer as you can see, put people to think and work, so I think giving a second chance maybe is a good idea this time :) – developer_hatch Jun 11 '17 at 02:03

2 Answers2

2

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.

cs95
  • 379,657
  • 97
  • 704
  • 746
  • 1
    Once I'd verified that 1000000007 is prime I just did `pow(3, pow(3,1000000000, 1000000006), 1000000007)` – PM 2Ring Jun 10 '17 at 21:48
  • I didn't know about Fermat's theorem so I had to ask a question. x) – cs95 Jun 10 '17 at 21:49
  • 1
    Fermat's Little theorem is a beautiful piece of number theory, and it's not too hard to understand. And once you have that down, it's not too hard to prove Euler's theorem. And when you have _that_, you can understand how RSA encryption works. – PM 2Ring Jun 10 '17 at 21:54
  • this is brilliant ! – Don't call me Jun 10 '17 at 21:55
  • @PM2Ring Yep. I've got a lot to learn. Also, I thought Euler's Theorem _was_ the proof for Fermat's? – cs95 Jun 10 '17 at 22:19
  • Very beautiful work you did, just brilliant the idea of look up for help in another community and put all the pieces together... Just a perfect well done work, congratulations. I just thought the problem was a simple sintáxis problem, but I learned a lot with all of it. Is a plus one in my opinion – developer_hatch Jun 11 '17 at 01:25
1

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

developer_hatch
  • 15,898
  • 3
  • 42
  • 75