Permalink
Browse files

backfill

  • Loading branch information...
1 parent 891db9a commit 068a8671271a70bf78539209ab85cb0b1499ff83 ben hengst committed Jul 24, 2009
Showing with 104 additions and 0 deletions.
  1. +13 −0 perl5/001/01.pl
  2. +28 −0 perl5/002/01.pl
  3. +42 −0 perl5/003/01.pl
  4. +21 −0 perl5/006/01.pl
View
@@ -0,0 +1,13 @@
+#!/usr/bin/perl
+use strict;
+use warnings;
+use List::Util qw{sum};
+
+=pod
+If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
+
+Find the sum of all the multiples of 3 or 5 below 1000.
+
+=cut
+print sum(grep{$_ % 3 == 0 || $_ % 5 == 0} 1..999);
+
View
@@ -0,0 +1,28 @@
+#!/usr/bin/perl
+use strict;
+use warnings;
+use Memoize;
+
+=pod
+Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
+
+1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
+
+Find the sum of all the even-valued terms in the sequence which do not exceed four million.
+=cut
+
+# thanks MJD http://perldoc.perl.org/Memoize.html
+memoize('fib');
+sub fib {
+ my $n = shift;
+ return $n if $n < 2;
+ fib($n-1) + fib($n-2);
+}
+
+my $i = 2; # we really could start anywhere but we know that 1 = 1 and it's not even
+my $sum = 0;
+while ((my $f = fib($i++)) < 4000000) {
+ $sum += $f unless $f %2;
+}
+print $sum;
+
View
@@ -0,0 +1,42 @@
+#!/usr/bin/perl
+use strict;
+use warnings;
+use Memoize;
+use Test::Most qw{no_plan};
+use POSIX qw{ceil};
+use Data::Dumper;
+
+=pod
+The prime factors of 13195 are 5, 7, 13 and 29.
+
+What is the largest prime factor of the number 600851475143 ?
+=cut
+
+memoize('prime');
+sub prime {
+ my ($num) = @_;
+ # we go past the range limit if we do this
+ #grep{ $num % $_ == 0 && scalar(prime($_)) == 0 } 2..ceil($num/2);
+
+ my %primes;
+ my $x = 2;
+ my $max = ceil($num/2);
+ while ( $x++ < $max ) {
+ my $result = $num/$x;
+
+warn Dumper({ $num => sprintf(q{%s * %s},$x,$result) }) if $x < 35;
+ if ($result == int($result) ) {
+ #it's a factor, for speed lets check for primeness of both sides.
+ map{$primes{$_}=1} grep{scalar(prime($_))} $x, $result;
+warn Dumper({ $x => \%primes }) if $x < 35;
+
+ }
+ }
+ return sort {$a <=> $b} keys %primes;
+}
+
+
+eq_or_diff( [prime(13)], [] );
+eq_or_diff( [prime(13195)], [5,7,13,29] );
+#eq_or_diff( [prime(600851475143)], {} );
+
View
@@ -0,0 +1,21 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use Util::Log;
+
+=pod
+The sum of the squares of the first ten natural numbers is,
+1^(2) + 2^(2) + ... + 10^(2) = 385
+
+The square of the sum of the first ten natural numbers is,
+(1 + 2 + ... + 10)^(2) = 55^(2) = 3025
+
+Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 − 385 = 2640.
+
+Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.
+
+=cut
+
+
+

0 comments on commit 068a867

Please sign in to comment.