Created
May 5, 2015 21:01
-
-
Save rustyconover/a66440e2124282449555 to your computer and use it in GitHub Desktop.
Collate sequence counter for Perl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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