#!/usr/bin/perl #re https://www.perlmonks.org/?node_id=11102502 use strict; use warnings; #my $abc = "abc-αβγ"; #handy greek letters #my $xyz = "χψζ-xyz"; my( $number, @components ) = grep s/\A([0-9]+)\Z/$1/, @ARGV; unless( $number and @components == ( @ARGV - 1 ) ){ exit q[Argument isn't numeric] } # removes duplicates prior to processing, to indicate to the user this is known; my %not_quite_an_Oset_A; @not_quite_an_Oset_A{@components} = (undef) x @components; my $dup; if( keys %not_quite_an_Oset_A != @components){ print "Duplicated numbers will be removed in processing\n"; $dup = 1; }; @components = sort map { $_ + 0 } keys %not_quite_an_Oset_A; # understanding of LOMS implementations versus Perl Data Structures may vary just now :} print "Interpreting [ $number ] as sums of [ @components ]\n"; my $allocated = compute_recursively( $number, \@components ); #use Data::Dumper; #print Dumper( $allocated ); unfurl_recursive( $allocated ); sub compute_recursively{ my ( $number, $components ) = @_; my $allocating = {}; foreach my $comp ( @$components ) { $allocating->{ $comp } = do { if( $comp == $number ){ if( defined $dup ){ $|++; print "removing dup comp"; sleep 1; print " ."; sleep 1; print ".\n" }; undef }elsif( $comp > $number ){ 'X' }else{ compute_recursively( $number - $comp, $components ) } }; } return $allocating } sub unfurl_recursive{ # optimism unshift @_, 'Mset [', if @_ == 1; # reality #$_[0] =~ s!\AMset \[!Set {!; # hmmm..? $_[0] =~ s!\AMset \[!LOMS? {!; unshift @_, 'Mset [', if @_ == 1; my $Emulated_Numerical_Equivilation = shift; my $perlHash = shift; unless( defined $perlHash ){ print "$Emulated_Numerical_Equivilation }\n"; return } if( $perlHash eq 'X' ){ return ''; } foreach my $n ( keys %$perlHash ) { unfurl_recursive( "$Emulated_Numerical_Equivilation $n", $perlHash->{$n} ) } } __END__ >numsums.pl 15 3 5 7 OUTPUT Interpreting [ 15 ] as sums of [ 3 5 7 ] LOMS? { 3 3 3 3 3 } LOMS? { 3 7 5 } LOMS? { 3 5 7 } LOMS? { 7 3 5 } LOMS? { 7 5 3 } LOMS? { 5 3 7 } LOMS? { 5 7 3 } LOMS? { 5 5 5 } Interpreting [ 2 ] as sums of [ 3 ] Interpreting [ 55 ] as sums of [ 3 5 7 ] 86000ish Interpreting [ 105 ] as sums of [ 3 5 7 ] Out of Memory!