#!/usr/bin/perl # Raphael Robinson, as mentioned in "Metamagical Themas", Douglas Hofstadter # This one just iterates on a string of digits. Self-describing strings: # 22 - 2 twos # 21322314 - 2 ones, 3 twos, 2 threes, 1 four # 3122331415 - 3 ones, 2 twos, 3 threes, 1 four, 1 five # 3122331416 - 3 ones, 2 twos, 3 threes, 1 four, 1 six # 10311233 - 1 zero, 3 ones, 1 two, 3 threes # 1031223314 - 1 zero, 3 ones, 2 twos, 3 threes, 1 four use strict; use diagnostics; my $line = "1"; $line = $ARGV[0] if defined $ARGV[0]; my $lastline = "x"; my $previouslastline = "y"; my @digitcount = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0); my @letters = ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9'); my $ord_zero = ord('0'); print $line, "\n"; while ($line ne $lastline) { $previouslastline = $lastline; $lastline = $line; foreach my $idx (0..9) { $digitcount[$idx] = 0; } my @a = split //, $line; foreach my $letter (@a) { ++$digitcount[ord($letter) - $ord_zero]; } $line = ''; foreach my $idx (0..9) { if ($digitcount[$idx]) { $line .= $digitcount[$idx]; $line .= $letters[$idx]; } } if ($line eq $previouslastline) { print "Cycle\n"; last; } print $line, "\n"; }