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

Yitzchak Scott-Thoennes sthoenna at efn.org
Fri Jul 26 11:17:47 CDT 2002


In article <fiEQ9gzkg6bH092yn at efn.org>,
sthoenna at efn.org (Yitzchak Scott-Thoennes) wrote:
>>> 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.
>In your example, there appears to be a relationship between the coefficients
>(1 (implied), 36, 360, 3600, 129600).  If that is true in general, you can
>solve it without going through all the combinations.  For instance, you
>can easily find E = 17 and A = 24 in the above, leaving you with:
>
>B + 10*C + 100*D = 2883
>
>which has several solutions (by inspection, about 14).

I got several requests for explanation.  You can solve for E by doing
2307012/129600, since even with maximum values, A+36*B+360*C+3600*D is
much less than 129600.  (This also means many numbers will have no
solutions).  Then, since the coefficients of B-E are multiples of 36,
A is the remainder from 2307012/36, or 36 if there is no remainder.

>From the simplified equation in B,C, and D, you can see that the last
digit of B is 3, so B is one of 3,13,23,33.  Substituting each of these
leads to an equation C+10*D=X, where the last digit of C is the same as
the last digit of X, giving 4 solutions for C if X ends in 1..6 and
3 solutions otherwise.  Total number of solutions is (solutions for B)
times (avg solutions for C for a given B), or about 14.

Here is Perl code to apply the above to any answer:
================
use strict;
use warnings;

# solve A + 36*B + 360*C + 3600*D + 129600*E = F
# for integral A-E in 1..36

my ($A, $B, $C, $D, $E, $F, $tmp, $tmp2, @F);
chomp(@F = <DATA>);  # read in sample data and remove newlines

for $F (@F) {

   # 129600 is greater than all the other terms, even if A-D are all 36, so
   # we can just integer-divide to get E
   $E = do { use integer; $F/129600 };
   $tmp = $F-$E*129600;

   $A = $tmp % 36 || 36; # all other terms are divisible by 36, so A must be
   $tmp -= $A;           # the remainder of $tmp/36, or 36 if no remainder

   $tmp /= 36;           # remove common factor of 36

   # Now we have a reduced equation to solve:
   # $B + 10 * $C + 100 * $D == $tmp

   # since the coefficients of C and D are multiples of 10,
   # the last digit of $tmp is the same as the last digit of B,
   # so loop through all B's that have matching last digit

   TRY_B:
   for ($B = $tmp % 10 || 10; $B <= 36; $B += 10) {

      $tmp2 = ($tmp - $B) / 10;

      # now, $C + 10 * $D == $tmp2
      # same as above.  last digit of $tmp2 is same as
      # last digit of C.

      for ($C = $tmp2 % 10 || 10;  $C <= 36; $C += 10) { 

         # does that leave us with an acceptable D?

         $D = ($tmp2 - $C) / 10;
         if ($D > 0 && $D <= 36) {
            print "$A + 36*$B + 360*$C + 3600*$D + 129600*$E == $F\n";
            # last TRY_B;  # uncomment to generate only one solution
         }
      }
   }
}
__DATA__
1393940
1983444
2307012
2934888
3072934
3930488
================

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
      Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
 For daily traffic, use spug-list for LIST ;  for weekly, spug-list-digest
     Seattle Perl Users Group (SPUG) Home Page: http://seattleperl.org




More information about the spug-list mailing list