This is actually a simpler problem, because the output for each iteration comes directly off of one of the input streams. One interesting property of that is that you can easily advance yourself in the output stream without generating the whole sequence.
use strict;
use warnings;
{
my @streams = map [$_, 0], (2,3,5);
sub limbic_sequence {
my $first_after = shift;
if (defined $first_after) {
$_->[1] = int($first_after/$_->[0]) for @streams;
}
my ($lowest) = sort {$a <=> $b} map {($_->[1]+1) * $_->[0]} @s
+treams;
$_->[1]++ for grep {($_->[1]+1) * $_->[0] == $lowest} @streams
+;
$lowest;
}
}
print join ', ', map limbic_sequence, 1..50;
print "\n";
printf "First after 100000 is: %d\n", limbic_sequence(100000);
print join ', ', map limbic_sequence, 1..50;
print "\n";
__END__
#Output
2, 3, 4, 5, 6, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 22, 24, 25, 26, 2
+7, 28, 30, 32, 33, 34, 35, 36, 38, 39, 40, 42, 44, 45, 46, 48, 50, 51
+, 52, 54, 55, 56, 57, 58, 60, 62, 63, 64, 65, 66, 68
First after 100000 is: 100002
100004, 100005, 100006, 100008, 100010, 100011, 100012, 100014, 100015
+, 100016, 100017, 100018, 100020, 100022, 100023, 100024, 100025, 100
+026, 100028, 100029, 100030, 100032, 100034, 100035, 100036, 100038,
+100040, 100041, 100042, 100044, 100045, 100046, 100047, 100048, 10005
+0, 100052, 100053, 100054, 100055, 100056, 100058, 100059, 100060, 10
+0062, 100064, 100065, 100066, 100068, 100070, 100071
Caution: Contents may have been coded under pressure.