Skip to content

Instantly share code, notes, and snippets.

@alexxroche
Last active December 12, 2016 19:25
Show Gist options
  • Save alexxroche/16032a7b48127ec248d91220f25d55b2 to your computer and use it in GitHub Desktop.
Save alexxroche/16032a7b48127ec248d91220f25d55b2 to your computer and use it in GitHub Desktop.
Collatz Conjecture perl
#!/usr/bin/env perl
# sudo yum install -y perl-Math-BigInt-GMP || sudo apt-get install -y libmath-bigint-gmp-perl
use strict;
use Math::BigInt lib => 'GMP';
my $in = $ARGV[0] || &help;
$in=~s/,//g;
$in = Math::BigInt->new($in);
sub help{print "What number?\n"; exit;}
sub col(){
my $i = shift;
if ($i % 2 == 0 ){
return $i/2;
}else{
return ($i*3)+1;
}
}
my $count =0;
while($in > 1){
$count++;
my $r = &col($in);
print "$count:";
print "$r";
print "\n";
$in=$r;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment