Mercurial > coderloop
diff src/collatz.pl @ 0:307a81e46071 tip
initial committ
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Tue, 18 Oct 2011 01:17:49 -0700 |
parents | |
children |
line wrap: on
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/collatz.pl Tue Oct 18 01:17:49 2011 -0700 1.3 @@ -0,0 +1,69 @@ 1.4 +#!/usr/bin/perl 1.5 + 1.6 +use bigint; 1.7 +$a = "/home/r/coderloop-test/euler14-a.in"; 1.8 +$b = "/home/r/coderloop-test/euler14-b.in"; 1.9 + 1.10 +sub read_integer{ 1.11 + ##reads an integer from a file 1.12 + (my $file) = @_; 1.13 + my $int; 1.14 + open FILE, "<".$file or die $!; 1.15 + for (<FILE>){ chomp; /^\W*(\d+)\W*$/; $int = $1;} 1.16 + close FILE; 1.17 + return $int;} 1.18 + 1.19 + 1.20 +@collatz = (1); 1.21 + 1.22 + 1.23 +sub collatz_next { 1.24 + my $n = @_[0]; 1.25 + if (1 == $n) {return 1;} 1.26 + if (0 == ($n % 2)){ return $n/2;} 1.27 + if (1 == ($n % 2)){ return (($n*3)+1);} 1.28 +} 1.29 + 1.30 +sub different_collatz_length { 1.31 + my $n = $_[0]; 1.32 + my $d = 1; 1.33 + while ($n != 1){ 1.34 + $d++; 1.35 + $n = collatz_next($n);} 1.36 + return $d;} 1.37 + 1.38 +sub collatz_length { 1.39 + ## calculate the length of the collatz sequence 1.40 + ## http://en.wikipedia.org/wiki/Collatz_conjecture 1.41 + ## starting at n 1.42 + my $n = @_[0]; 1.43 + #print $n,"\n"; 1.44 + #memoization 1.45 + #print "***** ", $collatz[$n], " ********\n"; 1.46 + if (defined $collatz[$n]){return $collatz[$n];} 1.47 + if (1 == $n) {return 1;} 1.48 + if (0 == ($n % 2)){ return (1 + &collatz_length($n/2))} 1.49 + if (1 == ($n % 2)){ return (1 + &collatz_length(($n*3)+1));} 1.50 +} 1.51 + 1.52 + 1.53 +sub max_collatz { 1.54 + my $n = @_[0]; 1.55 + my $max_length = 1; 1.56 + my $max_val = 1; 1.57 + for (my $i = 1; $i < $n; $i++){ 1.58 + my $collatz = &different_collatz_length($i); 1.59 + #push(@collatz, $collatz); 1.60 + if ( $collatz >= $max_length){ 1.61 + $max_length = $collatz ; 1.62 + $max_val = $i}} 1.63 + print "\n number is : $max_length --- "; 1.64 + print &collatz_length($max_val) . "\n"; 1.65 + return $max_val; 1.66 +} 1.67 + 1.68 + 1.69 + 1.70 + 1.71 +if (defined @ARGV){ 1.72 + print &max_collatz(read_integer($ARGV[0])), "\n";}