#! perl -slw use strict; use Data::Dump qw[ pp ]; $Data::Dump::WIDTH = 1000; use List::Util qw[ sum ]; use Algorithm::Combinatorics qw[ combinations ]; use Time::HiRes qw[ time ]; =comment I have a number of sizes of weights, for argument purposes, say they are .01 .12 1.1 2.2 5.3 10.5 50.1 100.6 gram. Each weight will also have a known precision but that complexity will only be used in the program once I figure out the basics. I need to write a program that calculates the smallest total number of weights to use to obtain a total that is as close to a desired as possible. =cut our $M //= 1; my $start = time; my %results; my @vals = ( qw[ 0.01 0.12 1.1 2.2 5.3 10.5 50.1 100.6 ] ) x $M; for my $k ( 1 .. $#vals ) { my $iter = combinations( \@vals, $k ); while( $_ = $iter->next ) { push @{ $results{ sum @$_ } }, [ @$_ ]; } } printf "Finding all %d results took %6.3f seconds\n", scalar( keys %results ), time() - $start; printf " to see them all"; ; pp \%results; __END__ C:\test>1216334 -M=1 Finding all 254 results took 0.004 seconds to see them all