-
Notifications
You must be signed in to change notification settings - Fork 6
/
125 Palindromic sums.jl
59 lines (48 loc) · 1019 Bytes
/
125 Palindromic sums.jl
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
58
59
#!/usr/bin/julia
# Author: Daniel "Trizen" Șuteu
# License: GPLv3
# Website: https://github.com/trizen
# https://projecteuler.net/problem=125
# Runtime: 3.127s
@inline function is_csquare(n::Int64)
s = isqrt(n)
for i in 1:s-1
sum = i^2
for j in i+1:s
sum += j^2
sum > n && break
sum == n && return true
end
end
return false
end
function count_sum(limit::Int64)
sum = 0
for i in 1:9
if is_csquare(i)
sum += i
end
end
i = 1
while true
s = string(i)
r = reverse(s)
p = s * r
k = parse(Int64, p)
k >= limit && break
if is_csquare(k)
sum += k
end
for j in 0:9
p = s * string(j) * r
k = parse(Int64, p)
k >= limit && break
if is_csquare(k)
sum += k
end
end
i += 1
end
return sum
end
println(count_sum(100000000))