061 Cyclical figurate numbers.sf 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. #!/usr/bin/ruby
  2. # Author: Daniel "Trizen" Șuteu
  3. # Date: 24 September 2017
  4. # License: GPLv3
  5. # Website: https://github.com/trizen
  6. # https://projecteuler.net/problem=61
  7. # Runtime: 1.782s
  8. var(:trig, :square, :penta, :hexa, :hepta, :octa)
  9. 1000..9999 -> lazy.grep{ .digit(1) }.each { |n|
  10. var (h, t) = (String(n)/2 -> ...)
  11. trig{h}{t} = true if is_polygonal(n, 3)
  12. square{h}{t} = true if is_polygonal(n, 4)
  13. penta{h}{t} = true if is_polygonal(n, 5)
  14. hexa{h}{t} = true if is_polygonal(n, 6)
  15. hepta{h}{t} = true if is_polygonal(n, 7)
  16. octa{h}{t} = true if is_polygonal(n, 8)
  17. }
  18. var a = [trig, square, penta, hexa, hepta, octa]
  19. a.permutations { |*h|
  20. h.dig(0) \\ next -> each_k { |ah|
  21. h.dig(0, ah) \\ next -> each_k { |at|
  22. h.dig(1, at) \\ next -> each_k { |bt|
  23. h.dig(2, bt) \\ next -> each_k { |ct|
  24. h.dig(3, ct) \\ next -> each_k { |dt|
  25. h.dig(4, dt) \\ next -> each_k { |et|
  26. h.dig(5, et, ah) \\ next
  27. var nums = "#{ah}#{at} #{at}#{bt} #{bt}#{ct} #{ct}#{dt} #{dt}#{et} #{et}#{ah}".nums
  28. say "sum(#{nums.join(', ')}) = #{nums.sum}"
  29. }}}}}}
  30. }