| ' Puzzlet #100 ' Take two different 2-digit integers, ' a and b, such that a/b * ab produces ' a palindrome. ' Example: 11/20 x 1120 = 616 ' Find all possible examples. ' By Dave Ellis. declare IsPalindrome(p: int) declare PrintResult() def numerator, denominator: int def derived: float color 11,0: cls openconsole print "Searching ...": print print " a b c", print " derived equation" color 14,0 for numerator = 10 to 99 for denominator = 10 to 99 if numerator <> denominator derived = numerator*(100*numerator/denominator + 1) if derived = int(derived) if IsPalindrome(derived) PrintResult() endif endif endif next denominator next numerator color 11,0 print: print "That's all!" print: print "Press any key to exit ... ", do: until inkey$ <> "" closeconsole end sub IsPalindrome(p) ' if p is a palindrome, returns -1, ' otherwise returns 0 def flag, i, j, L: int def p$, q$, r$: string p$ = ltrim$(str$(p)) L = len(p$) j = int(L/2) i = 1 flag = 1 do q$ = mid$(p$, i, 1) r$ = mid$(p$, L + 1 - i, 1) if q$ <> r$ flag = 0 endif i = i + 1 until flag = -1 | i > j return flag sub PrintResult ' pretty printer print using ("###", numerator), print using ("#####", denominator), print using ("#######", derived), print using ("#######", numerator), print "/", print denominator, "x ", print numerator, chr$(8), print denominator, "= ", print using ("#####",derived) return |
| PROGRAM NOTES The code above is slightly revised from the original, published August 1st, 2004. Kirk Bresniker wrote in and pointed out that it missed some of the solutions. This revision corrects that problem. Variable numerator holds the "a" part of a/b. It is iterated through the range of 10 to 99, the appropriate range for 2-digit numerators. Variable denominator holds the "b" part of a/b. It is iterated through the same range as numerator. The code first ensures that numerator and denominator aren't equal, and then goes on to apply the a/b * ab formula, storing the result in a float variable named derived. If derived has a decimal part, it can't be a valid solution, and the line if derived = int(derived) checks for this. if derived is an integer, it's submitted to function IsPalindrome(). If it really is a palindrome, we have a valid result, and it's printed to the computer screen using function PrintResult(). If you want to know more about how IsPalindrome() works, check past Puzzlets or e-mail me directly. When you run the program, you'll see the following table of results printed out to your computer's screen. These are all the possible solutions for this type of problem. |
|
|
|
Searching ... a b c derived equation 11 10 1221 11/10 x 1110 = 1221 11 20 616 11/20 x 1120 = 616 12 60 252 12/60 x 1260 = 252 12 72 212 12/72 x 1272 = 212 13 20 858 13/20 x 1320 = 858 14 49 414 14/49 x 1449 = 414 15 45 515 15/45 x 1545 = 515 16 40 656 16/40 x 1640 = 656 17 50 595 17/50 x 1750 = 595 18 40 828 18/40 x 1840 = 828 18 50 666 18/50 x 1850 = 666 19 38 969 19/38 x 1938 = 969 19 76 494 19/76 x 1976 = 494 21 45 1001 21/45 x 2145 = 1001 22 20 2442 22/20 x 2220 = 2442 27 75 999 27/75 x 2775 = 999 28 98 828 28/98 x 2898 = 828 33 30 3663 33/30 x 3330 = 3663 42 90 2002 42/90 x 4290 = 2002 44 40 4884 44/40 x 4440 = 4884 51 17 15351 51/17 x 5117 = 15351 66 50 8778 66/50 x 6650 = 8778 66 72 6116 66/72 x 6672 = 6116 87 36 21112 87/36 x 8736 = 21112 No more! Press any key to exit ... |