### Search

 Search WWW Search DelphiForFun.org

As of October, 2016, Embarcadero is offering a free release of Delphi (Delphi 10.1 Berlin Starter Edition ).     There are a few restrictions, but it is a welcome step toward making more programmers aware of the joys of Delphi.  They do say "Offer may be withdrawn at any time", so don't delay if you want to check it out.  Please use the feedback link to let me know if the link stops working.

Support DFF - Shop

If you shop at Amazon anyway,  consider using this link.

We receive a few cents from each purchase.  Thanks

### Support DFF - Donate

If you benefit from the website,  in terms of knowledge, entertainment value, or something otherwise useful, consider making a donation via PayPal  to help defray the costs.  (No PayPal account necessary to donate via credit card.)  Transaction is secure.

Mensa® Daily Puzzlers

For over 15 years Mensa Page-A-Day calendars have provided several puzzles a year for my programming pleasure.  Coding "solvers" is most fun, but many programs also allow user solving, convenient for "fill in the blanks" type.  Below are Amazon  links to the two most recent years.

(Hint: If you can wait, current year calendars are usually on sale in January.)

### Contact

 Search DelphiForFun.org only

### Problem Description

Two little puzzles involving the number 3:

 Find all 5 digit numbers that contain the same 5 digits when they are multiplied by 3. Find the all of the 3 digit numbers, N, which together with 2*N and 3*N contain the  digits 1 through 9 exactly once.

### Background & Techniques

The 1st puzzle has been sitting completed in my future projects file for a year or so.  Yesterday's Mensa Puzzle-A-Day calendar had a 2nd "three related" puzzle so I decided to combine them into a single program.

I'm not sure that it is feasible to solve the 1st puzzle involving 5 digit numbers with pencil and paper, but the 2nd puzzle almost certainly is if we apply some rules to reduce the search space.  For example,

 3 times N must be a three digit number since there can only be 9 digits altogether so N must be less than 333.   So the 1st digit of the number must be 1, 2, or 3. If N starts with 1,  then 2*N will start with 2, or 3 and 3*N will start with 3, 4, or 5 since there can be no carry greater than  1 or 2.  Similar  restrictions for 2*N and 3*N if N starts with 2 or 3. The last digit cannot be 5 or else 3 times the number would also end in 5. There can be no zeros anywhere. If we choose a trial units digit, we know the units digits for all 3 numbers which greatly reduces the possibilities for the 10's digit. Etc.

I did not solve it manually, but believe I could have - it was just more fun to write the code to let the computer do the grunt work.  J

#### Notes for Programmers

The fist problem, find N and 3*N with 5 digits and containing the same digits is simple once we have the SortDigits procedure; simply  sort the digits of i and 3*i for numbers in the range of 10000 (the smallest 5 digit number) and 33333 (the largest with 3*i still containing only 5 digits).   SortDigits  returns the digits of an input integer as a new integer with the digits in sorted sequence.  First we split out the digits of the input number into an integer array with one digit in each location (6 lines of code).  Then we sort the array in increasing sequence (3 lines of code).  then we rebuild the sorted digit array as a single integer (2 lines of code) and return it as the result.

Version 2, The generalized version of Problem 1, is slightly more complicated, but uses exactly the same algorithm as Version 1.

Problem 2, a three digit number combined with double and triple the number will contain digits 1 to 9 exactly one time each is also not too hard.  The trick is to separate out the digits of i2*i and 3*i into an array and somehow make that all of the digits 1 to 9 are found and that no digit occurs more than one time.  I settled on using a 9 character string, DigitCheck, initialized to "NNNNNNNNN" for each value to be tested.  Then when we check the digits, a 1 will replace  the "N" with "Y" in DigitCheck[1], etc. One additional check makes sure that  an "N" is in a position before placing the 'Y" there -  this implies that this is the first occurrence of that digit.  If it is not "N" then we'll put an "X" there.   All of this happens in a local function named Check.   Afterr calling Check with i, 2*i and 3*i, we have found a solution if DigitCheck="YYYYYYYYY".