fork(1) download
  1. import time
  2. N=10.0**80
  3.  
  4. start = time.clock()
  5. products_of_powers_of_2and3 = []
  6. power_of_2 = 1
  7. while power_of_2 < N:
  8. product_of_powers_of_2and3 = power_of_2
  9. while product_of_powers_of_2and3 < N:
  10. products_of_powers_of_2and3.append(product_of_powers_of_2and3)
  11. product_of_powers_of_2and3 *= 3
  12. power_of_2 *= 2
  13. products_of_powers_of_2and3.sort()
  14. #print products_of_powers_of_2and3
  15. stop = time.clock()
  16. s1=stop - start
  17. print "Elapsed", stop - start
  18.  
  19. start = time.clock()
  20. S = [1]
  21. i2 = 0 # current index in 2S
  22. i3 = 0 # current index in 3S
  23. while True:
  24. n2 = 2 * S[i2]
  25. n3 = 3 * S[i3]
  26. m=min(n2, n3)
  27. if m>=N:
  28. break
  29. S.append(m)
  30. i2 += n2 <= n3
  31. i3 += n2 >= n3
  32. #print S
  33. stop = time.clock()
  34. s2=stop - start
  35. print "Elapsed", stop - start
  36.  
  37. print len(products_of_powers_of_2and3), len(S), s2/s1
Success #stdin #stdout 0.09s 7996KB
stdin
Standard input is empty
stdout
Elapsed 0.03
Elapsed 0.05
22496 22496 1.66666666667