# SPUG: FW: Help..Any Math GENIUS out there?

Jim Flanagan jimfl at tensegrity.net
Thu Jul 25 16:16:25 CDT 2002

```--On Thursday, July 25, 2002 10:50 AM -0700 "Orr, Chuck  (NOC)"
<chuck.orr at attws.com> wrote:

>> I am hoping to use Perl to solve an equation like the below:
>>
>> A + (36*B) + (360*C) + (3600*D) + (129600*E) = 2,307,012
>>
>> I know that each of the variables A - E is a whole number in the
>> range 1-36.
>>
>> Could I load those numbers (1 -36) in an array and substitute each
>> number for each variable until this is solved?

I'm no genius, but here's a possible way to do it. It may not work for
all equations, but seems to work for this one. The strategy is to take as
many multiples (not exceeding the maximum, in this case 36) of the
largest number out first, then subtract that from the total, then work on
the next largest, and so on. The first step is to subtract at least 1
times each of the "factors" from the total, since none of the variables
can be 0.

The answer in this case is:

24 + 3*36 + 8*360 + 28*3600 + 17*129600 = 2307012

Here's a quick and dirty:

my @factors = (1,36,360,3600,129600);
my \$target = 2307012;

my @multipliers = ();

my \$max = 36;
my \$min = 1;

# Assume each factor is multiplied by at least \$min

map {\$target -= \$min * \$_} @factors;

for my \$factor (sort {\$b <=> \$a} @factors) {
\$mult = int(\$target/\$factor);
\$mult = (\$mult <= \$max) ? \$mult : \$max;
\$target -= \$mult * \$factor;
unshift @multipliers, \$min + \$mult;
}

# Show the list of multipliers

@tmp = @multipliers;
print join(' + ', (map {shift(@tmp) . "*\$_"} @factors));
print "\n";

# Check our work
my \$foo = 0;
map {\$foo += \$_ * shift @multipliers} @factors;
print \$foo;
~

--
Flanagan::Jim

http://jimfl.tensegrity.net
mailto:jimfl%40t%65ns%65gr%69ty.n%65t

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL