Skip to content

Instantly share code, notes, and snippets.

@rustyconover
Created May 5, 2015 21:01
Show Gist options
  • Save rustyconover/a66440e2124282449555 to your computer and use it in GitHub Desktop.
Save rustyconover/a66440e2124282449555 to your computer and use it in GitHub Desktop.
Collate sequence counter for Perl
use strict;
use warnings;
sub collatz_count_until_1 {
my $n = shift;
my $count = 0;
while ($n != 1) {
$n = (($n % 2 == 0) ? ($n/2) : (($n*3) + 1));
$count++;
}
return $count;
}
my $max;
foreach my $x (1..300000) {
my $length = collatz_count_until_1($x);
if (!defined($max) || $max->[0] < $length) {
$max = [$length, $x];
}
}
printf("Maximum stopping distance %d, starting number %d\n", $max->[0], $max->[1]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment