-
Notifications
You must be signed in to change notification settings - Fork 6
/
061 Cyclical figurate numbers.sf
38 lines (30 loc) · 1.11 KB
/
061 Cyclical figurate numbers.sf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/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}"
}}}}}}
}