Mini-Challenge #2: Answer

There are 87 square numbers with all the digits 0 to 9 exactly once.

(1026753849, 1042385796, 1098524736, 1237069584, 1248703569, 1278563049, 1285437609, 1382054976, 1436789025, 1503267984, 1532487609, 1547320896, 1643897025, 1827049536, 1927385604, 1937408256, 2076351489, 2081549376, 2170348569, 2386517904, 2431870596, 2435718609, 2571098436, 2913408576, 3015986724, 3074258916, 3082914576, 3089247561, 3094251876, 3195867024, 3285697041, 3412078569, 3416987025, 3428570916, 3528716409, 3719048256, 3791480625, 3827401956, 3928657041, 3964087521, 3975428601, 3985270641, 4307821956, 4308215769, 4369871025, 4392508176, 4580176329, 4728350169, 4730825961, 4832057169, 5102673489, 5273809641, 5739426081, 5783146209, 5803697124, 5982403716, 6095237184, 6154873209, 6457890321, 6471398025, 6597013284, 6714983025, 7042398561, 7165283904, 7285134609, 7351862049, 7362154809, 7408561329, 7680594321, 7854036129, 7935068241, 7946831025, 7984316025, 8014367529, 8125940736, 8127563409, 8135679204, 8326197504, 8391476025, 8503421796, 8967143025, 9054283716, 9351276804, 9560732841, 9614783025, 9761835204, and 9814072356)


Step-by-step solution

The square numbers we seek must have exactly ten digits, so they are between 1000000000 and 9999999999 (in scientific notation, 1e10 and 1e11). This means their square roots must be between 31623 and 100000. So we have approximately 68400 numbers to test. This a simple task for a computer like a Raspberry Pi.

The square numbers with all digits can be found with just one line of Python:

[i**2 for i in range(31623, 100000) if len(set(list(str(i**2)))) == 10]

This is achieved by combining together several different language elements. (In general, it is not a good idea to write complicated one-line programs, especially if you are new to programming. It can make it difficult for people to work out what the program does. Also, if you make a typo, it is difficult to spot where it has been made.)

What does this code do?  It forms a list of squares made from the numbers in the range 31623 and 100000, including only those which satisfy a key condition: that the square has all ten digits once only.

But how is this condition implemented? Let's consider the steps in the one-line program, using an (small) example number i=66 to illustrate what's going on:
  1. First, the square number (i**2) is converted into a string, with the str function. So (e.g.) 56**2 = 3136 is converted to a string '3136'.
  2. The string is converted into a list of digits, using the list function. So '3136' becomes ['3','1','3','6']
  3. The list is converted into a set, using set.  This has the effect of removing the duplicate elements. So ['3','1','3','6'] is converted to {'3','1','6'}.
  4. The function len tells us the number of elements in the set, i.e., 3 in this example.
  5. Finally, we test the length of the set (i.e. the number of distinct digits), to see whether it is equal to 10. If so, it is included in the list, if not it is excluded.