-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpe122.spk
57 lines (54 loc) · 1.48 KB
/
pe122.spk
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//an algorithm to solve Project Euler problem 122 https://projecteuler.net/problem=122
//it took around 433s to get the solution
import lang.speka
class PE122
{
method new(){
this.set = Vector.new()
this.set.append(1)
this.min = 99999
return this
}
method find(N,step){
if step >= this.min | this.set.last()> N
return 0
i = 0
while i < this.set.length(){
if this.set.get(this.set.length() - i -1) + this.set.last() == N
{
if step < this.min {
this.min = step
return 0
}
}
i += 1
}
i = 0
while i < this.set.length(){
if this.set.get(this.set.length() - i -1) + this.set.last() < N
{
this.set.append(this.set.get(this.set.length() - i -1) + this.set.last())
this.find(N,step + 1)
if this.min <= step {
return 0
}
this.set.removeLast()
}
i += 1
}
}
}
class Main
{
method main(){
sum = 0
k = 2
while k <= 200 {
p = PE122.new()
p.find(k,0)
sum += p.min + 1 // well, p.min is actually the actual answer minus 1 ...
k += 1
}
System.print(sum)
}
}