in reply to check possible combination of sum using given array

some progress

Clearly the issue for me is about the implementation of the data structures. However, returning to this SoPW. I have made some progress toward understanding how exactly the Array and Hash perl data structures do not work as I may have supposed they might.

Having said that I am now drafting a Meditation that will go into a bit more detail about, what it is I think I'm doing versus what it is I think I am trying to do. I may get some more appropriate responses after I do such. I hope, and thanks for bearing with me.

Essentially I am attempting to implement LOMS. This may already exist within the perl universe, and I may have bypassed that somewhere. Initially though, it would appear that the perl data structures Array and Hash, might be considered as Lists, and Sets respectively. And I am still debating that, they may be Osets and Multisets (they're not they're perl data structures), but the hands have to start getting dirty somewhere. The idea being that, treating either as a specific structure from another field, is most likely akin to mixing up system and software IO. Principally not a good idea, but maybe a starting point for headache inducing thoughts.

In my previous study to the OP question Re: check possible combination of sum using given array I most definetely hinted towards this, but at the level of attempting to ascertain what data structure the natural numbers fitted into. Again with some difficulty. However in the mean time, I have been developing my understanding around the areas of comp sci and maths, as it relates or, as much as I percieve it relates to these ideas.

A near answer

#!/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 ma +y 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; prin +t " ."; 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!

The OP asked for a check of combinations, the program returns combinations that sum to teh required number. I would argue that these are not permutations, as there would be multiples of the permutation of the Multiplicities of only 3 or 5. but there are only 1 of each.

In the printout what we see is actually a mixing of types, there are 2 Multisets, and 6 Osets, or 2 Multisets, and 6 Perms of 1 Set.

What the OP does not get from, my response anyway, is an Array containg the combinations if any exist, or an Array conataining 'fail' should any not exist. I thought I'd just add that in about 4 hours ago, needless to say I just reverted to giving a response. Think I see how recursiveness can be a pain now.

The point being that the OP was actually rather vague about what they were after, so the answer they recieved was/is rather vague.

When perl dwim I think it is looking to translate some text from one place to another, when someone would like to start playing around with number theory, perl is not really bothered. Perl likes transcribing clay tablets, over evaluating them.


Meditation coming soon to a perlmonks webpage near you.