In cleaning out our basement ready for our move, I came across the folders of my notes I’d laboriously written during the lectures for my Mathematics degree. There in the middle of Year 2’s notes were some notes on programming, and in there were a set of five puzzles. Presumably we were given these to see whether we could write a program to solve them. In FORTRAN, natch, because that’s all we had at the time.
So let’s see how we do. Here’s the first puzzle.
If OODDF is the square root of WONDERFUL, where each letter represents a different digit, then which do they represent?
Without starting any coding with some gigantic 100,000 iteration loop (or even a cleverer 900 iterations, with O varying from 1 to 9 and D & F from 0 to 9), we should look at the problem mathematically with calculator in hand.
The first point is that the square root of the largest 9-digit number – all the 9s – is roughly 31,622, so quite quickly we can see that O is either 1 or 2.
The next point recognizes that if O is 1, the square of OODDF would then start with 1 as well, meaning that W would equal O. (11,0002 = 121,000,000 and 11,9992 = 142,976,001) Hence, we can quickly state that O is 2. (Our putative “loop” is now down to only 81 iterations: nine possible values for D and nine for F.)
Following on from that, by squaring 22,000 and 22,999, we can see that W is either 4 or 5. (22,0002 = 484,000,000 and 22,9992 = 528,954,001, with the move from 4 to 5 happening between 22,360 and 22,361.) What this tells us in a roundabout way is that D cannot equal 5, since that would make W equal to 5 as well. In other words, D only has eight possible values.
The final point takes a look at the other end of the numbers. If OODDF ends in 0, so does WONDERFUL, and that’s not allowed. By the same reasoning, F cannot be 1, 5, 6. F also cannot equal 2, because that’s what O is. So, F has only five possible values: 3 (meaning L is 9); 4 (L = 6); 7 (L = 9), 8 (L = 4), 9 (L = 1). Our putative loop is now down to 40 iterations.
On to the solution! In JavaScript, of course.
/*jslint white this */
/*global console*/
(function () {
"use strict";
var Fvalues = [3, 4, 7, 8, 9];
var Dvalues = [0, 1, 3, 4, 6, 7, 8, 9];
var verify = function (F) {
if (F !== this.D) {
var ooddf = 22000 + (this.D * 110) + F;
var wonderful = (ooddf * ooddf);
var wonderfulAsString = wonderful.toString();
if ((wonderfulAsString[1] === "2") &&
(wonderfulAsString[3] === this.DasString) &&
(wonderfulAsString[6] === F.toString())) {
console.log(ooddf, wonderful);
}
}
};
var calculate = function (Dvalue) {
var scratchPad = {D: Dvalue, DasString: Dvalue.toString()};
Fvalues.forEach(verify, scratchPad);
};
Dvalues.forEach(calculate);
}());
I coded up the possible values of D and F as arrays, and then used the array.forEach()
method to iterate through each of those arrays. I check to see if the values for O, D, and F are replicated in the squared value, and just print out the valid ones. As it happens, fortuitously, only one answer was printed (OODDF = 22,887; WONDERFUL = 523,814,769), negating the need to check that all the digits in WONDERFUL were different.
(This is a kind of a meta-assumption: we assume that the puzzle as posed has a valid answer, so if we can only produce one possibility through our calculations, it must automatically be the right one. We never have to check that all the digits in the square are different.)
Aside: if you have Node.js installed, save this script as, say puzzleone.js, and you can just run it from the command line with node puzzleone.js
.
No Responses
Feel free to add a comment...
Leave a response
Note: some MarkDown is allowed, but HTML is not. Expand to show what's available.
_emphasis_
**strong**
[text](url)
`IEnumerable`
* an item
1. an item
> Now is the time...
Preview of response