challenges

my solutions to various "programming challenge" problems
git clone https://wehaveforgeathome.hates.computer/challenges.git
Log | Files | Refs | LICENSE

014.pl (486B)


      1 #!/usr/bin/perl
      2 
      3 use strict;
      4 use Memoize;
      5 
      6 memoize('count_hailstones');
      7 
      8 my $highest_start = 13;
      9 my $highest_count = 10;
     10 for(my $i = 15; $i < 1_000_000; $i += 2) {
     11   my $count = count_hailstones($i);
     12   if ($count > $highest_count) {
     13     $highest_start = $i;
     14     $highest_count = $count;
     15   }
     16 }
     17 print "$highest_start\n";
     18 
     19 sub count_hailstones {
     20   my $n = shift;
     21   return 1 if ($n == 1);
     22   return 1 + count_hailstones($n / 2) if ($n % 2 ==0);
     23   return 1 + count_hailstones((3* $n) + 1);
     24 }