fork download
  1. import time
  2.  
  3. def is_prime(number):
  4. i = 3
  5. while i**2 <= number:
  6. if number % i == 0:
  7. return False
  8. i += 2
  9. return True
  10.  
  11. def lucas_lehmer(p):
  12. s = 4
  13. M = 2**p - 1
  14.  
  15. for _ in range(p - 2):
  16. s = ((s * s) - 2) % M
  17. return s == 0
  18.  
  19. def mersenne_primes():
  20. p = 3
  21. while True:
  22. if is_prime(p) and lucas_lehmer(p):
  23. yield (p, 2**p - 1)
  24. p += 2
  25.  
  26. start = time.time()
  27. numbers = mersenne_primes()
  28.  
  29. for _ in range(10):
  30. p, mersenne = next(numbers)
  31. perfect = 2**(p-1) * mersenne
  32. print(perfect)
  33.  
  34. print('Executado em ' + str(time.time() - start) + 's')
Success #stdin #stdout 0.02s 27712KB
stdin
Standard input is empty
stdout
28
496
8128
33550336
8589869056
137438691328
2305843008139952128
2658455991569831744654692615953842176
191561942608236107294793378084303638130997321548169216
13164036458569648337239753460458722910223472318386943117783728128
Executado em 0.0003993511199951172s