-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathe43.coffee
42 lines (37 loc) · 1.09 KB
/
e43.coffee
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
39
40
41
42
concatDigits = (digits) ->
sum = 0
for i in digits
do(i) ->
sum = sum*10 + i
return sum
hasProperty = (digits) ->
if (concatDigits(digits[1..3]) % 2 is 0 and
concatDigits(digits[2..4]) % 3 is 0 and
concatDigits(digits[3..5]) % 5 is 0 and
concatDigits(digits[4..6]) % 7 is 0 and
concatDigits(digits[5..7]) % 11 is 0 and
concatDigits(digits[6..8]) % 13 is 0 and
concatDigits(digits[7..]) % 17 is 0)
concatDigits(digits)
else
0
fac = (x) => if x < 2 then 1 else x * fac(x - 1)
NthLP = (N, p, rem) ->
if rem.length == 1 then return p.concat(rem)
k = fac(rem.length - 1)
index = Math.floor(N / k)
p.push(rem[index])
rem = if index is 0
rem[1..]
else if index is rem.length - 1
rem[..-2]
else
rem[..index-1].concat(rem[index+1..])
return NthLP(N % k, p, rem)
sum = 0
i = 0
while i < fac(10)
digits = NthLP(i, [], j for j in [0..9])
sum += hasProperty(digits)
i += 1
console.log(sum)