686 Powers of Two.sf 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #!/usr/bin/ruby
  2. # Daniel "Trizen" Șuteu
  3. # Date: 05 November 2019
  4. # https://github.com/trizen
  5. # https://projecteuler.net/problem=686
  6. # Runtime: 1 minute, 19 seconds.
  7. # The first few exponents are: 90, 379, 575, 864, 1060, 1545, 1741, 2030, 2226, 2515, 2711, 3000, 3196, 3681, 3877, 4166, 4362, 4651, 4847, 5136, ...
  8. # The set of differences between consecutive exponents is: {196, 289, 485}.
  9. define ln2 = log(2)
  10. define ln5 = log(5)
  11. define ln10 = log(10)
  12. func isok(n) {
  13. #floor(2**(n - floor((n*ln2)/ln10) + 2) * 5**(2 - floor((n*ln2)/ln10))) == 123
  14. floor(exp(ln2*(n - floor((n*ln2)/ln10) + 2) + ln5*(2 - floor((n*ln2)/ln10)))) == 123
  15. }
  16. var count = 1
  17. var k = (1..Inf -> first(isok))
  18. var nth = 678910
  19. loop {
  20. if (isok(k+196)) {
  21. ++count
  22. k += 196
  23. }
  24. elsif (isok(k+289)) {
  25. ++count
  26. k += 289
  27. }
  28. elsif (isok(k+485)) {
  29. ++count
  30. k += 485
  31. }
  32. #say "#{count} out of #{nth} with k = #{k}"
  33. if (count == nth) {
  34. say "Final result: #{k}"
  35. break
  36. }
  37. }