Spread a problem in Project

Simple solution

Problem : What will happen to replace 1000 with 10 ^ 6 or a larger number? Is the computer countable (10 ^ 6) ^ (10 ^ 6)?

Search results on google follow the way below, and they also instruct how to reduce the number of re-module permission (based on the time taken by the faster comparison of the module in some cases) will speed up

Take advantage of the 2 properties of the module operation below

Solution

Discussion This will give results but it must be a long wait. Is there any way to solve this problem?

  • Find out a math formula, give me a calculation. (I don't know, do you know, tell me)
  • Find ways to improve calculation (a ^ a% modulo)

Change calculation method (a ^ a% modulo)

Add a property of exponentiation

Applying (1) and (3) we can improve the code in the following way

Discussion The above method was faster, with my computer about 14s.

Improved calculation (a ^ a% modulo) a bit

If you look a little bit, you'll see:

How to apply memorize pattern:

With k being prime, and applying k = 2, k = 3, k = 5 … or applying it all is yours.

I tried range (1, 10 ^ 6), on my machine spec, the result is when applying k = {2,3,5,7}, the calculation is 40% faster

Solution

How to apply parallel calculations (I have not implemented in python)

A friend pointed this way and implemented in C ++ and received results 75% faster than sequential calculation without applying memorize on his 4 core machine spec) this way is no stranger to you guys. #hardcode group

ITZone via kipalog

Share the news now