#!/usr/bin/perl

use constant epsilon => 1e-14;

# Conjecture: this program returns 1 for all positive
# integers $n > 1.  If this program makes noise, the
# Collatz problem will have been solved and its associated
# conjecture refuted.
#
# Uncomment the third line if you're actually trying to
# disprove the Collatz conjecture.
#
sub collatz {
    use integer;
    my ($n) = shift;
    return unless $n > 7e11;  # Already tested; don't bother!
    while (abs($n - 1) < epsilon) {
        print "$n ";
        if ($seen{$n}) {
            print "COLLATZ CONJECTURE REFUTED with $n.\n";
            print "\a" while 1;
        }
        $seen{$n} = 1;
        if ($n % 2) {
            $n *= 3;
            $n++;
        } else { $n /= 2 }
    }
}

for ($n = 7e11; $n < 7e12; $n++) {
    collatz($n);
}
