Matt Owen

source code for "files/pe/pe_044.pl"

return to portfolio
  1.  #!/usr/bin/perl
  2.  
  3.  use strict;
  4.  use warnings;
  5.  
  6.  
  7.  # get n given pent(n)
  8.  sub inv_pent {
  9.   my ($n) = shift;
  10.   return (1 + sqrt(1+24*$n))/6;
  11.  }
  12.  
  13.  
  14.  # is pentagonal?
  15.  sub is_pent {
  16.   my ($n) = inv_pent shift;
  17.   return $n == int $n ? 1 : 0;
  18.  }
  19.  
  20.  
  21.  # get n-th pentagonal
  22.  sub pent {
  23.   my ($n) = shift;
  24.   return $n*(3*$n-1)/2;
  25.  }
  26.  
  27.  
  28.  my @diffs;
  29.  
  30.  for my $p (1..10000000) {
  31.   next unless is_pent $p;
  32.   for my $m (1.. int inv_pent $p) {
  33.   my $pent = pent $m;
  34.   my ($n) = inv_pent($p+$pent);
  35.  
  36.   if ($n == int $n) {
  37.   print "** $p = P_$n - P_$m\n" if is_pent(pent($n) + $pent);
  38.   }
  39.   }
  40.  }