fork download
  1. def foo(n):
  2. # stackoverflow.com/a/4601480/849891
  3. p2 = 1 # current power of 2
  4. p3 = 1 # current power of 3
  5. e3 = 0 # exponent of current power of 3
  6. t = 1 # number less than or equal to the current power of 2
  7. while t < n:
  8. p2 *= 2
  9. if p3 * 3 < p2:
  10. p3 *= 3
  11. e3 += 1
  12. t += 1 + e3
  13. candidates = [p2]
  14. c = p2
  15. for i in range(e3):
  16. c /= 2
  17. c *= 3
  18. if c > p2: c /= 2
  19. candidates.append(c)
  20. return sorted(candidates)[n - (t - len(candidates))]
  21.  
  22. print( foo(100))
  23. print( foo(10000))
  24. print( foo(1000))
Runtime error #stdin #stdout #stderr 0.01s 27704KB
stdin
Standard input is empty
stdout
98304.0
1.6196824734745036e+53
stderr
Traceback (most recent call last):
  File "./prog.py", line 24, in <module>
  File "./prog.py", line 20, in foo
IndexError: list index out of range