#!/usr/local/bin/perl use v5.18.1; use warnings; # Name: A246425.pl sub a { state @a; my $n= shift; for my $i (($#a + 1) .. $n) { my $m= ($i >> 2); my $r= ($i % 4); if (0 == $r) { $a[$i]= -1 * $m; # 8x+1 -> 6x+ 1, ( 6x+ 1) - (8x+1) = -2x -> -1m } elsif (1 == $r) { $a[$i]= 2 * $m + 1; # 8x+3 -> 12x+ 5, (12x+ 5) - (8x+3) = 4x+2 -> 2m+1 } elsif (2 == $r) { my $d= -3 * $m - 2; # 8x+5 -> 2x+ 1, ( 2x+ 1) - (8x+5) = -6x-4 -> -3m-2 $d+= $a[$i + $d]; # perform "modified Collatz" step, via self-reference $a[$i]= $d; } else { $a[$i]= 2 * $m + 2; # 8x+7 -> 12x+11, (12x+11) - (8x+7) = 4x+4 -> 2m+2 } } return $a[$n]; } my $N= $ARGV[0] // 100; if ($ARGV[1]) { say "$_ ", a($_) for 0 .. $N; } else { say join ",", map a($_), 0 .. $N; } __END__ Example runs: $ ./A246425.pl $ ./A246425.pl 10000 $ ./A246425.pl 20 1