#!/usr/bin/env perl # # collatz - print n and collatz(n) for each positive n # # $Id$ use warnings; use strict; $| = 1; # unbuffer output my %c = (1 => 1); # caches the results for (my $i = 1; 1; ++$i) { my ($n,$c) = ($i,1); if (!$c{$n}) { while (!$c{$n}) { $n = $n % 2 ? 3*$n + 1 : $n / 2; ++$c; } $c{$n} = $c; } print "$i $c\n"; }