This handy, little Perl script will display the audioactive sequence in the user-specified non-zero base. It should be noted that this sequence grows exponentially for all bases greater than 1. In base 1, however, the sequence grows linearly. Try AUDIOACTIVE.PL 1 10 and see for yourself (or just look at the short example runs below).
#!/usr/bin/perl
use strict;
# AUDIOACTIVE.PL
# This script takes 2 command line arguments: base and iterations
# If the base is not 1, 2, or 3 it is assumed to be 4
# because after 3, the sequence is the same regardless of base.
# Example: perl AUDIOACTIVE.PL 2 10
if($#ARGV != 1) {
print STDERR "Usage: perl AUDIOACTIVE.PL <base> <iterations>\n";
exit;
}
my ($str, $base, $max) = ('1', shift, shift);
my %hDecToBin = qw(1 1 2 10 3 11 4 100);
my %hDecToTri = qw(1 1 2 2 3 10 4 11);
my %hDecToQua = qw(1 1 2 2 3 3 4 10);
my $hDecToBase = undef;
if($base < 1) {
print STDERR "Base must be greater than 0, exiting program...\n";
exit;
} elsif($base == 1) {
print '1' x $_, "1\n" for (0..$max-1);
exit;
} elsif($base == 2) {
$hDecToBase = \%hDecToBin;
} elsif($base == 3) {
$hDecToBase = \%hDecToTri;
} else {
$hDecToBase = \%hDecToQua;
}
print "$str\n";
for(my $i=0; $i < $max-1; $i++) {
my @str = split('', $str);
$str = '';
my ($num, $count) = ($str[0], 0);
foreach (@str) {
if($_ == $num) {
$count++;
} else {
$str .= $hDecToBase->{$count};
$str .= $num;
$num = $_;
$count = 1;
}
}
$str .= $hDecToBase->{$count};
$str .= $num;
print "$str\n";
}
Examples:
perl AUDIOACTIVE.PL 1 6
1
11
111
1111
11111
111111
perl AUDIOACTIVE.PL 2 6
1
11
101
111011
11110101
100110111011
perl AUDIOACTIVE.PL 3 6
1
11
21
1211
111221
1012211
perl AUDIOACTIVE.PL 4 6
1
11
21
1211
111221
312211
perl AUDIOACTIVE.PL 5 6
1
11
21
1211
111221
312211