' Puzzlet #46
' To find two integers, neither of which
' ends in a zero, and whose product is
' exactly 1,000,000.
' By Dave Ellis.
def flag, num1, num2, prod: int
openconsole
print "Searching ...": print
num1 = 5
do
flag = 0
num2 = 2
do
prod = num1 * num2
if prod = 1000000
flag = 1
print num1, "x ", num2,
print "= 1,000,000"
else
num2 = num2 + 2
' prevent num2 ending in zero
if num2 % 10 = 0
num2 = num2 + 2
endif
endif
until flag | prod > 1000000
' force num1 to end in 05 or 25
if num1 % 25 = 0
num1 = num1 + 80
else
num1 = num1 + 20
endif
until num1 > 500000
print: print "No more!"
print: print "Press a key to exit ... ",
do: until inkey$ <> ""
closeconsole
end
PROGRAM NOTES
This problem could easily be solved by hand, but the computerised solution is shown since it could be used for more difficult problems of the same kind using more difficult number combinations.
Variables num1 and num2 are used in two DO loops to generate all valid integers. Their product is stored in prod.
One of the integers must always end in 05 or 25, and the outer DO loop generates all such integers in the range. The other must be an even number, but not one ending in a zero, and the inner DO loop generates the appropriate integers.
As soon as values for num1 and num2 are tried which result in a prod value of 1,000,000 their values are printed to the screen and the program continues on trying out other values until num1 exceeds 500,000. This would in fact be an illegal value, so you could make this 250,000 or smaller, but would eventually end up solving the puzzlet by hand!
When you run the code, it will produce the inset screen very quickly.
This is the only solution.
Searching ...
15625 x 64 = 1,000,000
No more!
Press any key to exit ...
MAIN MENU
DOWNLOAD CODE
ARCHIVES
Site design/maintenance: Dave Ellis E-mail me!
Last Updated: February 22nd, 2004.