File tree Expand file tree Collapse file tree 1 file changed +58
-0
lines changed Expand file tree Collapse file tree 1 file changed +58
-0
lines changed Original file line number Diff line number Diff line change
1
+ use v6 ;
2
+
3
+ = begin pod
4
+
5
+ = TITLE Palindromic sums
6
+
7
+ = AUTHOR Shlomi Fish
8
+
9
+ L < http://projecteuler.net/problem=125 >
10
+
11
+ Palindromic sums
12
+
13
+ The palindromic number 595 is interesting because it can be written as the sum
14
+ of consecutive squares: 62 + 72 + 82 + 92 + 102 + 112 + 122.
15
+
16
+ There are exactly eleven palindromes below one-thousand that can be written as
17
+ consecutive square sums, and the sum of these palindromes is 4164. Note that 1
18
+ = 02 + 12 has not been included as this problem is concerned with the squares
19
+ of positive integers.
20
+
21
+ Find the sum of all the numbers less than 108 that are both palindromic and can
22
+ be written as the sum of consecutive squares.
23
+
24
+ = end pod
25
+
26
+ my $ debug = False ;
27
+
28
+ my $ LIMIT = 100_000_000 ;
29
+
30
+ my $ sqrt_limit = $ LIMIT . sqrt . Int ;
31
+
32
+ my % found ;
33
+ my $ sum_found = 0 ;
34
+
35
+ for 1 .. $ sqrt_limit -> $ start
36
+ {
37
+ my $ sum = $ start ** 2 ;
38
+
39
+ for $ start + 1 .. $ sqrt_limit -> $ end
40
+ {
41
+ $ sum += $ end ** 2 ;
42
+ if $ sum > $ LIMIT
43
+ {
44
+ last ;
45
+ }
46
+
47
+ if " $ sum" . flip eq " $ sum"
48
+ {
49
+ if % found {" $ sum" }++ == 0
50
+ {
51
+ say " Found $ sum" ;
52
+ $ sum_found += $ sum ;
53
+ }
54
+ }
55
+ }
56
+ }
57
+
58
+ say " Sum found = $ sum_found" ;
You can’t perform that action at this time.
0 commit comments