#!/usr/bin/perl -W use Math::BigInt; use strict; my $length = int($ARGV[0]); my $numbit = int($ARGV[1]); # All first rows should end up in this array my @results; # This should contain the processing stack my @queue; my $start_value = (2 ** $numbit) - 1; my $start_factor = $numbit - 1; my $start_level = $length - $numbit + 1; push @queue, [ $start_level, $start_factor, $start_value ]; for my $item (@queue) { my ($level, $factor, $value) = @{$item}; #print "Found Item $factor, $level, $value\n"; for my $x (1..$level) { push @results, $value * (2 ** ($x-1)); } #push @results, $value; my $next_level = $level - 1; if($next_level) { foreach my $next_factor (1..$factor) { #print "Calculating value ($value * 2) - ((2 ** $next_factor) -1 )\n"; my $next_value = ($value * 2) - ((2 ** $next_factor) -1); push @queue, [ $next_level, $next_factor, $next_value ]; } } } print "MATH: ".join(",", sort @results)."\n"; # Manual Checking my $max = 2**$length; for my $r (@results) { print "A: ABOVE MAX $r > $max\n" and next if $r > $max; my $bin = sprintf("%0".$length."b", $r); my $count = ($bin =~ tr/1//); print "B: TOO MANY SET BITS $r $bin($count) > $numbit\n" and next if $count > $numbit; }