Euler problems as perl oneliners

From AJS.COM

Jump to: navigation, search

The Euler problems are a list of math problems which are supposed to be solved with the aid of a computer.

This page is full of spoilers, as it chronicles my first-pass attack at these problems as Perl one-liners. This collection of solutions is not intended to be elegant or particularly insightful. These aren't the ideal solutions. This is just what came to me as I worked on solutions.

You can find the problems on the Project Euler site at: http://projecteuler.net/

Contents

Problem 1

Add all the natural numbers below one thousand that are multiples of 3 or 5.

My first solution was wrong:

$ perl -le '$n=0;for($i=3;$i<1000;$i+=3) {$n+=$i}
       for($i=5;$i<1000;$i+=5) {$n+=$i} print $n'
266333

This was incorrect because I mis-read the problem, and did not notice that the result had to be the sum of the numbers that matched both criteria, not the sum of the two sequences. Adjusting for this, I wrote:

$ perl -le '$n=0;for($i=3;$i<1000;$i+=3) {$n+=$i}
       for($i=5;$i<1000;$i+=5) {$n+=$i unless $i%3 == 0} print $n'
233168

Which yields the correct answer.

Problem 2

Find the sum of all the even-valued terms in the Fibonacci sequence which do not exceed four million.

My first run was correct:

$ perl -le '@f=(1,2);$sum=2;while(($f=$f[0]+$f[1]) <= 4_000_000){
       shift @f; push @f, $f;$sum+=$f if $f%2==0 }print $sum'
4613732

But just in case there was overflow, I tried it with bigint:

$ perl -Mbigint -le '@f=(1,2);$sum=2;while(($f=$f[0]+$f[1]) <= 4_000_000){
       shift @f; push @f, $f;$sum+=$f if $f%2==0 }print $sum'
4613732

Problem 3

Find the largest prime factor of a composite number.

In this case, I'd already written an integer factorization program, so there was nothing to do but feed it in:

$ fac 600851475143
600851475143: 71, 839, 1471, 6857

Problem 4

Find the largest palindrome made from the product of two 3-digit numbers.

I didn't think of anything off the top of my head that was all that creative, so I just wrote this quick and dirty solver that assumed that the minimum would by 900*900. Had that proven to be false, I would have made a second, more comprehensive pass, but it turned out to be quite close to the correct answer.

$ perl -le 'for($n=999;$n>900;$n--) {
               for($m=999;$m>900;$m--) {
                  my $p=$n*$m;
                  if ($p eq reverse($p)) {
                      print "$m * $n = $p"; exit 0 } } }'
913 * 993 = 906609

Problem 5

What is the smallest number divisible by each of the numbers 1 to 20?

This required almost no work on my part. I just wrote this brain-dead multiplication:

$ perl -le 'print 2*3*5*7*11*13*17*19'
9699690

Which was wrong, as I was multiplying all of the unique prime factors. Oops. So, I re-ran it, multiplying all of the unique sets of prime factors to get the LCM:

$ perl -le 'print 2*2*2*2*3*3*5*7*11*13*17*19'
232792560

This works because it's 2*3*5*7*11*13*17*19 (all of the prime factors) times 2*2*2 (allowing us to make (4, 8 and 16) and 3 (allowing us to make 9). The LCM of a series is, by definition, the product of the set of prime factors that contains all of the sets of prime factors of your series, so we're done.

... more to come ...

Personal tools