Given: Factors 2, 3, 5 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, ... 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, ... 5, 10, 15, 20, 25, 30, ... R = 2, 3, 4, 5, 6, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 22, 24, 25, 26, 27, 28, 30, ... ##```## #!/usr/bin/perl use strict; use warnings; use constant HEAD => 0; use constant TAIL => -1; my \$end = shift || 10; my @num = @ARGV ? @ARGV : (2, 3, 5); my \$next = merge_multiple( \@num ); print \$next->(), "\n" for 1 .. \$end; sub merge_multiple { my \$list = shift; return () if ! \$list || ref \$list ne 'ARRAY'; my \$n = 1; my \$h = \$list->[TAIL]; my (@pool, @stream); return sub { return shift @pool if @pool; for ( 0 .. \$#\$list ) { my \$mult = \$list->[ \$_ ]; my \$beg = \$h * (\$n - 1) / \$mult + 1; my \$end = \$h * \$n / \$mult; \$stream[ \$_ ] = [ map { \$mult * \$_ } \$beg .. \$end ]; } ++\$n; @pool = merge( \@stream ); return shift @pool; } } sub merge { my \$stream = shift; my \$end = \$#\$stream; my @merged; while ( 1 ) { my \$low; for ( 0 .. \$end ) { my \$val = \$stream->[ \$_ ][HEAD]; next if ! defined \$val; \$low = \$_ if ! defined \$low || \$val < \$stream->[ \$low ][HEAD]; } last if ! defined \$low; my \$num = shift @{ \$stream->[ \$low ] }; next if defined \$merged[ TAIL ] && \$merged[ TAIL ] == \$num; push @merged, \$num; } return @merged; } ```