Three Digit Square w/ Numbers 1-9

502 Views Asked by At

Using a computer/program, can anyone figure out:

The square of 567 is 321,489. These two numbers contain each of the digits from 1 to 9 exactly once between them. What other three-digit number and its square have this property?

And also explain how?

1

There are 1 best solutions below

0
On

The key here is that you can use a computer program. The easiest way to do this is using Python. First, create a function that turns the number into a list of digits, like so:

def digits(n):
    return [int(d) for d in str(n)]

Now, let's say our three-digit number is $a$. We can then combine the digits of $a$ with the digits of $a^2$ using list concatenation:

all_digits = digits(a)+digits(a**2)

Finally, we need to make sure all_digits contains all the digits from $1$ through $9$. The easiest way to do this is to sort them and to see if it is the array $1, 2, 3, 4, ..., 9$:

if sorted(all_digits) == [1, 2, 3, 4, 5, 6, 7, 8, 9]: print(a)

Finally, we need to loop $a$ from $100$ to $999$ to get through all the three-digit numbers. We can do this with a for loop, so the full program becomes:

def digits(n):
    return [int(d) for d in str(n)]

for a in range(100, 999+1):
    all_digits = digits(a)+digits(a**2)
    if sorted(all_digits) == [1, 2, 3, 4, 5, 6, 7, 8, 9]: print(a)

This prints out $567$ and $854$, so the final answer is $854$.