#!/usr/athena/bin/perl # # usage: ./collatzst.pl (START) (STOP) use strict; sub st; sub cnext; # get arguments my $n = shift; my $nstop = shift; # defaults. $nstop = $n, $n = 1 if ($nstop == undef); $nstop = 100 if ($nstop == undef); # bounds check $n = 1 if ($n < 1); $nstop = 1 if ($nstop < 1); print $n . "\t" . st ($n++) . "\n" while ($n <= $nstop); exit; sub st { my $n = shift; my $st = 0; $n = cnext ($n), $st++ while ($n != 1); return ($st); } sub cnext { my $c = shift; if ($c % 2) { return (3 * $c + 1); } else { return ($c / 2); } }