-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathp87.lobster
45 lines (38 loc) · 1009 Bytes
/
p87.lobster
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
import dictionary
import std
def compute_primes(max):
// Prime sieve
let composites = dictionary<int, bool>(max)
for_range(2, max/2) n:
var m = n * 2
while m <= max:
composites.set(m, true)
m += n
let primes = []
for_range(2, max) i:
if not composites.get(i, false):
push(primes, i)
return primes
def solve(max):
// Compute primes
let square_root = floor(sqrt(max))
let cube_root = floor(pow(float(max), float(1)/float(3)))
let fourth_root = floor(pow(float(max), float(1)/float(4)))
let primes = compute_primes(square_root)
def for_primes(n_max, f):
for(primes) n:
if n > n_max:
return
f(n)
// Compute sums
let sums = dictionary<int, bool>(max / 5)
for_primes(square_root) a:
for_primes(cube_root) b:
for_primes(fourth_root) c:
let sum = a*a + b*b*b + c*c*c*c
if sum < max:
sums.set(sum, true)
// Count sums
print sums.get_keys().length()
// Main entry point
solve(50000000)