Shlomi Fish avatar Shlomi Fish committed 3475439 Merge

Merged.

Comments (0)

Files changed (1)

project-euler/125/euler-125.pl

+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+use Math::BigInt lib => "GMP", ":constant";
+
+=head1 DESCRIPTION
+
+The palindromic number 595 is interesting because it can be written as the sum
+of consecutive squares: 6^2 + 7^2 + 8^2 + 9^2 + 10^2 + 11^2 + 12^2.
+
+There are exactly eleven palindromes below one-thousand that can be written as
+consecutive square sums, and the sum of these palindromes is 4164. Note that 1
+= 02 + 12 has not been included as this problem is concerned with the squares
+of positive integers.
+
+Find the sum of all the numbers less than 108 that are both palindromic and can
+be written as the sum of consecutive squares.
+
+=cut
+
+# my $limit = shift(@ARGV);
+
+my $limit = 100_000_000;
+
+my $sqrt_limit = int(sqrt($limit));
+
+my %found;
+my $sum_found = 0;
+
+foreach my $start (1 .. $sqrt_limit)
+{
+    my $sum = $start ** 2;
+
+    SUM_LOOP:
+    foreach my $end (($start+1) .. $sqrt_limit)
+    {
+        $sum += $end ** 2;
+        if ($sum > $limit)
+        {
+            last SUM_LOOP;
+        }
+
+        if (scalar(reverse("$sum")) eq "$sum")
+        {
+            if (! $found{"$sum"}++)
+            {
+                print "Found $sum\n";
+                $sum_found += $sum;
+            }
+        }
+    }
+}
+
+print "Sum found = $sum_found\n";
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.