Python optimization with big numbers -
I want to print all the prime numbers with <0> 10000000000 with fewer than 7 hundreds 7. I was using category () while using MemoryError because the generated array could not be archived, so I used to loop the while Turned into loop.
However the program is really slow. It takes more than a minute to print the first number.
Import Maths Def is_prime (n): If n% 2 == 0 and n> 2: return I = 3 while I & lt; For Math.sqrt (n) + 1: # range (3, int (math.sqrt (n)) + 1, 2): if n% i == 0: return wrong I + = 2 Return True def is_super_happy ( N): Count = 0 while n! = 0: if n% 10 == 7: count == 1 if count == 7: return is true: calculation = 0 n / = 10 return number ==7 i = 7777777 while I & lt; For 10e10: # range (7777777, int (10e10)): if is_super_happy (i) and is_prime (i): print ii + = 1 I can not talk about anything It can run fast, and I want it to be very fast.
Any ideas, suggestions?
two suggestions:
-
7e777777 < Reduce number of 7, 8, and 9-digit less than / code>code> directly (instead of checking each and every number change) and then check them out. There is a relatively small number of such numbersFor example, here's a way to create a list of all the numbers that have <7> 7777777 with the end [Int (x86 +7777777) * X for X in Xrange (100)]
-
A potential test for preference checking (e.g.) Consider using. It tells you whether a number is prominent with the probability of a very high level of
xand how fast it is possible that a number less thanxfor divisiveness Probably thousands of probes may be examined in
Comments
Post a Comment