Freaky Square Number !

Find the ONLY 5-digit positive integer that has ALL of the following properties:

i) It is a square number
ii) The digits are in an increasing sequence
iii) Each digit of the number is a perfect square

This problem was inspired by problem #3 from the South African Programming Olympiad 2014


The answer is 11449.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

17 solutions

Scott Kominers
Aug 6, 2014

Here is a rather inelegant, but one-line Mathematica solution:

Select[Flatten[
  Array[If[IntegerQ[
       Sqrt[Extract[{1, 4, 9}, #1]*10^4 + 
         Extract[{1, 4, 9}, #2]*10^3 + Extract[{1, 4, 9}, #3]*10^2 + 
         Extract[{1, 4, 9}, #4]*10^1 + Extract[{1, 4, 9}, #5]*10^0]] &&
       OrderedQ[{Extract[{1, 4, 9}, #1], Extract[{1, 4, 9}, #2], 
        Extract[{1, 4, 9}, #3], Extract[{1, 4, 9}, #4], 
        Extract[{1, 4, 9}, #5]}], 
     Extract[{1, 4, 9}, #1]*10^4 + Extract[{1, 4, 9}, #2]*10^3 + 
      Extract[{1, 4, 9}, #3]*10^2 + Extract[{1, 4, 9}, #4]*10^1 + 
      Extract[{1, 4, 9}, #5]*10^0, 0] &, {3, 3, 3, 3, 3}]], # != 0 &]

The program returns {11449} .

Any square digit implies only three possible numbers 1,4,9; and we all know that for a number to be a perfect square it has to be either divisible by 4 or gives remainder 1 with 4 as divisor. So the last two digits must be 49 (as 99 does not satisfy the condition above) . There fore the possibilities are 1xy49 , where x,y can be 1 or 4. (a+b)^2= a^2+b^2 +2ab Set a=100. To find out b, square(100)=10000 and square(130)=16900 hence b can only be 7 from the condition that only b^2 can produce last two digits. Hence the number is (100+7)^2 = 11449

Ananta Shahane - 6 years, 8 months ago

Log in to reply

Nice! But only one question, why did you set a=100 from the beginning?

Jorge D. Llamas - 6 years, 5 months ago

Here is a rather primitively done solution:

The requirement that each digit had to be a square number meant that only five digit numbers containing the digits 1, 4, 9 would satisfy. The requirement that they be ordered further limited the possibilities. I then tested possibilities 44444-99999 as these were just six possibilities. Once I deduced that the first digit had to start with 1, I started squaring numbers beginning with 100, 101...107^2 = 11449 which satisfies the requirements for this problem.

Dallas Carter - 6 years, 8 months ago

11449 does not satisfy the the requirement that the digits be in an increasing sequence. It is only non decreasing.

Kevin Glentworth - 4 years, 8 months ago

Why 144 is incorrect ?

nishi patel - 6 years, 10 months ago

Log in to reply

Coz it has to be a 5 digit number.

Alexander Tesfazgi - 6 years, 9 months ago

It asks for positive integers, and 0 is not a positive integer

Mark Hannan - 6 years, 10 months ago

find 5 digit number

samarth sangam - 6 years, 9 months ago

Log in to reply

Okkkk understood!!Thanks !!

nishi patel - 6 years, 9 months ago

Because questions is find the five digit positive no.. And ur ans in 3 digit..

Aniket Raj - 6 years, 9 months ago

asked fr a 5 digit no.

ram kapoor - 6 years, 9 months ago

5 digit number

John Wilcox - 6 years, 7 months ago
Hariraman R
Aug 6, 2014
  1. Every digit is square number so the options are only 1,4,9.
  2. It is an ordered number,so there are only few cases 1 * 9
  3. we can write the combinations and get the square root of each to find the correct answer

thats what i do

Jeggo Jidden - 6 years, 9 months ago

It can also contain 0

Aryan Gaikwad - 6 years, 3 months ago

Log in to reply

Actually, no. I don't think so. It is increasing in sequence. So in no way, 0 is included here

Abyoso Hapsoro - 6 years ago
Amanda Hulme
Aug 7, 2014

The smallest possible 5 digit number, where each digit is a square number is 11111. The square root of this is approximately 105.4. Set up a spreadsheet to calculate the squares of numbers greater than 105. The answer is 11449, which is the square of 107.

Andrei Popescu
Jan 8, 2015

This is Java:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
    public class Run {
      public static void main(String[] args) {
        for (int iterator = 10000; iterator < 99999; iterator++) {
            if (checkroot(iterator)) {
                if (checkdigits(iterator)) {
                    if (checkorder(iterator)) {
                        System.out.println("\t\t\t\t\t\tthis is it: " + iterator + " !!! ");}}}}
    }

    private static boolean checkorder(int number) {
        boolean result = true;
        int digit = 0;
        for (int i = 0; i < 5; i++) {
            digit = (number % 10);
            result = result && (digit >= (number / 10) % 10);
            number = number / 10;}
        return result;
    }
    private static boolean checkdigits(int number) {
        int remember = number;
        boolean result = true;
        int digit = (number % 10);
        for (int i = 0; i < 5; i++) {
            result = result && checkroot(digit);
            number = number / 10;
            digit = number % 10;}
        if (result) System.out.println("digits are perfect square for: " + remember);
        return result;
    }
    private static boolean checkroot(int number) {
        double root = Math.sqrt(number);
        return (root % 1 == 0);
    }
} 

David Holcer
Apr 4, 2015
1
2
3
4
5
6
7
from math import *
for i in range(10**4,10**5):
    nums=[]
    for j in str(i):
        nums.append(int(j))
    if sqrt(i).is_integer() and all(nums[i] <= nums[i+1] for i in xrange(len(nums)-1)) and sqrt(int(str(i)[0])).is_integer()and sqrt(int(str(i)[1])).is_integer() and sqrt(int(str(i)[2])).is_integer() and sqrt(int(str(i)[3])).is_integer() and sqrt(int(str(i)[4])).is_integer():
        print i

Freaky Square :P

Aryan Gaikwad
Mar 5, 2015

Java solution -

for(int i = 10_000; i < 100_000; i++)
        if(Math.round(Math.sqrt(i))==Math.sqrt(i))
            if (String.valueOf(i).contains("2") || 
                String.valueOf(i).contains("3") || 
                String.valueOf(i).contains("5") || 
                String.valueOf(i).contains("6") ||
                String.valueOf(i).contains("7") || 
                String.valueOf(i).contains("8") ){}
            else{
                System.out.println(i);
            }

}

This returns -

10000
10404
11449
14400
19044
40000
40401
44100
44944
90000

You can now manually pick the ordered number. I intentionally did this to keep the algorithm short but you can also pick up the number programmatically by converting it into a array.

Joaquim Joaquim
Jan 3, 2015

The most basic solution I could think of:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
    int i, j, k, l, m, n;
    int temp;

    for(i = 10000 ; i < 100000 ; i++){

        temp = sqrt(i);

        if( i == temp*temp){

               //extracting the digits of the number

        j = i%10;                        //5th digit
        k = (i/10)%10;                 //4th digit
        l = (i/100)%10;               //3rd digit
        m = (i/1000)%10;             //2nd digit
        n = (i/10000);                //1st digit

       //checking all the conditions (I don't like using && command, so I did separated if cases)

        if( n <= m ){
            if ( m <= l ){
                if( l <= k ){
                    if( k <= j ){
                        temp = sqrt(n);
                        if( temp*temp == n ){
                            temp = sqrt(m);
                            if( temp*temp == m ){
                                temp = sqrt(l);
                                if( temp*temp == l ){
                                    temp = sqrt(k);
                                    if( temp*temp == k ){
                                        temp = sqrt(j);
                                        if( temp*temp == j ){
                                            temp = sqrt(i);
                                            if( temp*temp == i ){
                                                printf("%d\n", i);  }}}}}}}}}}}}

    return 0;
} 

that's my solution ,it's simple DFS :

def dfs(n,num): if n==5: if is_sqr(num) : s=str(num) if not not( s[1]>=s[0] and s[2]>=s[1] and s[3]>=s[2] \ and s[4]>=s[3] ): print num elif n==0: for i in range(1,4): dfs(n+1,a[i]) else: n+=1 for i in range(4): dell=num*10+a[i] dfs(n,dell)

Nafees Zakir
Sep 29, 2014

Here's my python Solution:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
import math
num = 0
for a in range(10):
    if(a==4 or a==9 or a==1):
        for b in range(a, 10):
            if(b==0 or b==4 or b==9or b==1):
                for c in range(b, 10):
                    if(c==0 or c==4 or c==9or c==1):
                        for d in range(c, 10):
                            if(d==0 or d==4 or d==9or d==1):
                                for e in range(d, 10):
                                    if(e==0 or e==4 or e==9or e==1):
                                        num = a*10000+b*1000+c*100+d*10+e
                                        if((math.sqrt(num))== (int)(math.sqrt(num))):
                                            print num

Output = 11449

Kenny Lau
Sep 29, 2014

java code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
    public class brilliant201409291605{
        public static void main(String[] args){
            int[] a = new int[10];
            a[0] = 1;
            a[1] = 4;
            a[4] = 9;
            int[] b = new int[5];
            for(int i=0;i<5;i++) b[i] = 0;
            for(int i=0;i<625;i++){
                for(int j=4;j>=0;j--){
                    if(b[j]==9){
                        b[j]=0;
                        continue;
                    }
                    b[j] = a[b[j]];
                    break;
                }
                if(b[0]==0) continue;
                boolean c = false;
                for(int j=0;j<4;j++){
                    if(b[j]>b[j+1]){
                        c = true;
                        break;
                    }
                }
                if(c) continue;
                int d = 0;
                for(int j=0;j<5;j++){
                    d *= 10;
                    d += b[j];
                }
                if(Math.sqrt(d)-((int)(Math.sqrt(d)))==0) System.out.println(d);
            }
        }
    }

output:

11449
Press any key to continue . . .
Nj Rafi
Sep 14, 2014

My C code for the problem. :)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <stdio.h>
#include <math.h>
int main()
{
        float a,b,c,d,e;
        float num;

                for(a=1;a<=9;a++)
                        if(a==4 || a==9 || a==1)
                        for(b=a;b<=9;b++)
                                if(b==0 || b==4 || b==9|| b==1)
                                for(c=b;c<=9;c++)
                                        if(c==0 || c==4 || c==9|| c==1)
                                        for(d=c;d<=9;d++)
                                                if(d==0 || d==4 || d==9|| d==1)
                                                for(e=d;e<=9;e++)
                                                        if(e==0 || e==4 || e==9|| e==1)
                                                        {
                                                               num = a*10000+b*1000+c*100+d*10+e;
                                                               if(sqrt(num)== (int)sqrt(num) )
                                                                printf("%.0f", num);
                                                        }

        return 0;
}

Aniket Raj
Aug 21, 2014

Each no are square then squres no. In one digit is 1,4,&9 then it says its ordered no. Then we orderd 1, 4 & 9 in five digits its become a no 11449 the is a square of 107.. Thats the ans...

Member Wilcox
Aug 13, 2014

The most constricting condition is that the digits must each be square, so that means 1, 4, or 9 (or 0... but that would not work as this an ordered integer). I solved it by guessing that the answer would probably have all three and there are only a few such examples and I just used calculator to quickly spot the one with an integer square root. Hmmm what if you lack a calculator!? Then you first assume that the 5-digit number starts with 1. Then you know that the square root has to be between 100 (10.000) and 142 (root of 20,000 is 100 times root 2). And if you assume the number ends with 9, it the root has to end with 7. So try squaring 107, 117, 127, or 137. Happily the first one works. (If none of those had worked, and we would next have assumed the number starts with 4 and tried roots between 200 and 224 and so 207 and 217. The number cannot start with 9 since 99,999 is three squared times 11,111... but if 11,111 were a square then both 11,111 and 99,999 would be answers and there is only one answer. So if none of those had worked we would have known the number ends with 4 and been forced to go back and test 102, 108, 112, 118, 122, 128, 132, 138, 202, 208, 212, and 218)

why square root has to be between 100 and 142

N veeresh - 6 years, 9 months ago
Venu Gvgk
Aug 8, 2014

I don't propose this as a "Solution" but here is how i did it:

The only viable digits are 1, 4 and 9.

5 digit square numbers start from the square of 100. They seem to end somewhere near the square of 300. Set up an column of numbers from 100 to 350 in excel. Calculate their square in the next column.

Now, soon as I did this, I saw the seventh number from the the top fits the bill. Answer is 11449, square of 107.

If it was not this apparent, what would I have done? Take out all the numbers that do not start with 1, 4 or 9.

The only viable number that starts with 9 is 99999. the square root is not a whole number. Take out all numbers starting with 9.

take out all numbers starting with 4 but have a lesser digit next to it. Then take out all numbers that have any digit except 4 or 9 as the second digit from left....and so on....

I am glad i didn't have to do this. Doesn't sound particularly interesting or elegant...

Is there any solution that does not involve such exhaustive checking?

Michael Mendrin
Aug 6, 2014

You know, you forgot 44100, which is 210 squared. 0 is a square integer too.

Yes..right but 44100 is not an ordered number

dinesh c - 6 years, 10 months ago

Log in to reply

It is reverse ordered. I think @Michael Mendrin is correct.

Pranjal Jain - 6 years, 4 months ago

Also, 0 is not a square integer because according to the clarifications:

... A square is a number obtained by multiplying a p o s i t i v e \color{#D61F06}{positive} whole number by itself. ...

Michael Fischer - 6 years, 9 months ago
Vaibhav Borale
Aug 6, 2014

include<conio.h>

include<stdio.h>

void main()

{ long int temp[220];

int i,j,a,m,n,o,p,f1,f2,f3,f4,f5;

a=1;

for(i=100;i<317;i++)

{
    temp[a]=i*i;

    a++;
}

for(j=1;j<218;j++)

   {
    m=temp[j]/10;    

    f1=temp[j]%10;      

    n=m/10;

    f2=m%10;

    o=n/10;

    f3=n%10;

    p=o/10;

    f4=o%10;

    f5=p;

    if(f1==0 || f1==1 || f1==4 || f1== 9)

    {
        if(f2==0 || f2==1 || f2==4 || f2== 9)

        {
            if(f3==0 || f3==1 || f3==4 || f3== 9)

            {
                if(f4==0 || f4==1 || f4==4 || f4== 9)

                {
                    if(f5==1 || f5==4 || f5== 9)

                    {
                    printf("Number is %lu \n",temp[j]);
                    }
                }
            }
        }
    }
}
getch();

}

Mark Mottian
Aug 5, 2014

Here is a Python 3.4.1 solution:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
    import math

    count = 0

    #Method to determine a square number
    def isSquare(n):
        if math.sqrt(n) == int(n**(1/2)):
            return True
        else:
            return False

    #This method determines if number is ordered
    def ordered(n):
        if sorted(list(str(n))) == list(str(n)):
            return True
        else:
            return False

    #Inspect all 5-digit numbers
    for x in range (10000, 100000): 
        if isSquare(x):    #check if it is a square
            for a in str(x):   
                #each digit must be a square
                if isSquare(int(a)):
                    #count = 5; if all digits are squares
                    count += 1 

            if count == 5:
                #check if the number is ordered
                if ordered(x):   
                    print (x)    #print out the result

        count = 0   #reset count

The program returns 11449 as the answer.

Why 144 is incorrect???

nishi patel - 6 years, 10 months ago

Log in to reply

the answer should be a 5-digit positive integer

Eric Escober - 6 years, 7 months ago

here is a solution in haskell
import Data.List
import Data.Char

is_square :: Int -> Bool
is_square n = sq * sq == n
    where sq = floor $ sqrt $ (fromIntegral n::Double)

is_elem_square :: [Int] -> Bool
is_elem_square [] = True
is_elem_square (x:xs) = is_square x && is_elem_square xs

is_ordered :: [Int] -> Bool
is_ordered [] = True
is_ordered (x:[]) = True
is_ordered (x:y:xs) = (x <= y) && is_ordered (y:xs)

check_all :: Int -> Bool
check_all x = is_square x && is_ordered list_x && is_elem_square list_x
    where list_x = map digitToInt $ show x

solution = find check_all [10000..99999]

main = putStrLn $ show solution

Ricky Hariady - 6 years, 9 months ago

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...