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