A number theory problem by Ankush Tiwari

What is the largest value of the natural number n n such that the sum 1 2 + 2 2 + + n 2 1^2 + 2^2 + \ldots + n^2 is a perfect square ?


The answer is 24.

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.

4 solutions

Mark Mottian
Jan 8, 2014

After struggling to solve this problem with pen and paper, I eventually resorted to writing a Python programme to "spoon-feed" me the answer. Since this question does not fall into any distinct category, I would presume that programming solutions are allowed.

Here's my the Python programme:

import math

def perfectsquare (n):  
    root = math.sqrt(n)
    if int(root)**2 == n:
        return True
    else:
        return False

possiblevalues = []
Sum = 0

for x in xrange(1, 1000):
    Sum = Sum + x**2

    if perfectsquare(Sum) == True:
        possiblevalues.append(x)

print max(possiblevalues)

The programme outputs 24 as the answer.

Actually 24 24 is the only possible solution.

And , this is a problem of number theory

Ankush Tiwari - 7 years, 5 months ago

Log in to reply

1 1 is also a possible solution.

Jubayer Nirjhor - 7 years, 4 months ago

No it isn't! Check out my python code in the solution, there are other numbers as well.

Nashita Rahman - 2 years, 2 months ago

There exists an "elementary number theory" proof to this result, which is quite convoluted. For more details, see American Mathmatical Monthly .

Calvin Lin Staff - 6 years, 5 months ago

Well my code says 24 isn't the largest natural number satisfying the given condition. There are many other numbers besides 24.

Nashita Rahman - 2 years, 2 months ago
Vinod Kumar
Nov 4, 2020

Sum the series as (1/6)n(n+1)(2n+1)=m^2

The solutions are n=24 and m=70, which gives answer as 24.

Nashita Rahman
Mar 16, 2019

I used the following python code. Also I used the while True loop which is an infinite loop so that I could find other possibilities besides 24

1
2
3
4
5
6
7
8
s=0
i=1
import math
while True:
    s=s+math.pow(i,2)
    i=i+1
    if math.sqrt(s)-int(math.sqrt(s))==0 and i != 2:
        print (i-1)

The answer in the question is wrong as 24 isn't the largest natural number such that the given sum is a perfect square

Output 24 2547750 2828093 3974004 4636697 5019548 5065968 5529976 6434771 6741437 7101496 7898119 8192485 9530917 9774922 9916100 10303942 10514246 10656671 11945152 12032855 12153127 12799512 12819261 13801485 15276205 15347381 15960792 16108784 16236906 16357744 16400624 16439442 16457969 16676437 16866476 17203662 17315619 17440708 17576703 17592831 17798035 17829491 17844733 17918157 18401354 18439817 18604362 18849433 18868909 18982624

You are getting multiple answers because of precision issues with your code. 1 and 12 are the only possible values. You can check this by printing your square roots and squaring them.

Ankush Tiwari - 2 years, 2 months ago

Log in to reply

Even I thought the same at first, I checked my code thrice and it's surprising to know that 24 isn't the largest natural number satisfying the condition. See my code, if you find any error tell me please.

Nashita Rahman - 2 years, 2 months ago

Log in to reply

The sum for n = 2547750 n = 2547750 is 5512510526093580875 5512510526093580875 which is not a perfect square. The square root is approximately 2347873617.9985456 2347873617.9985456 . Your code is rounding off the value of square root which is causing the error. As I said, you can use a print(math.sqrt(s)) in your loop to check the same.

Ankush Tiwari - 2 years, 2 months ago

Log in to reply

@Ankush Tiwari Yeah I did that and for n=2547750, the square root it gave is 2347873618. How did you get it in floating point? Did you use coding because I don't think a manual calculator can solve this??

Nashita Rahman - 2 years, 2 months ago

Log in to reply

@Nashita Rahman Yes, I used a seperate python code to find square root of 5512510526093580875 5512510526093580875 .

Ankush Tiwari - 2 years, 2 months ago

Log in to reply

@Ankush Tiwari But how ? I can't really understand where am I going wrong, my code is alright!

Nashita Rahman - 2 years, 2 months ago

Log in to reply

@Nashita Rahman The problem is that math.sqrt() does not give precise value of square root. Instead you can use this code which does not depend on precision of math.sqrt .

import math
s = 0
i = 1
while True:
    s = s + i*i
    a = int(math.sqrt(s))
    if a*a == s:
        print(i)
    i = i + 1

Ankush Tiwari - 2 years, 2 months ago

Log in to reply

@Ankush Tiwari Why are you taking a to be an integer? The square root maybe float!

Nashita Rahman - 2 years, 2 months ago

Log in to reply

@Nashita Rahman because a will be an integer if s is a perfect square. If s is not a perfect square, then a will be rounded to nearest integer and if condition will be false, so it doesn't matter.

Ankush Tiwari - 2 years, 2 months ago

Log in to reply

@Ankush Tiwari Not necessary a will be a perfect square when it is an integer!

Nashita Rahman - 2 years, 2 months ago
Md Zuhair
Mar 15, 2019
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
    int arr[100000];
    long long int sum=0;
    int count=0;
    for(long long int i=1;i<100000;i++)
    {
        sum=sum+i*i;
        double x=sqrt(sum);
        long long int y=sqrt(sum);
        if(x==y)
        {
            arr[count]=i;
            count++;
        }
    }
    for(int i=0;i<count;i++)
    {
        cout<<arr[i]<<endl;
    }
} 

OUTPUT

1

24

24 isn't the largest natural number!!! My python code gave other numbers besides 24, check out the solution. There are other possibilities, try running an infinite loop you will get other possibilities besides 24.

Nashita Rahman - 2 years, 2 months ago

Log in to reply

Actually no. My code also gave something like that.. 24 is the greatest... Can be proved with Number theory and my code gave some different and wrong values as I didn't took long long int somewhere... I guess. Similar problems might be there in ur program.

Md Zuhair - 2 years, 2 months ago

Log in to reply

24 isn't the greatest. You can check with the number 2547750 . Put it in the range of your code and see the result!!!

Nashita Rahman - 2 years, 2 months ago

Log in to reply

@Nashita Rahman C++ isnt capable of calculating more than what i did.. Python is a good calculator... so ... :) ... Idk.. but it didnt gave anything else... other than 1 and 24.

Md Zuhair - 2 years, 2 months ago

Log in to reply

@Md Zuhair Ohh I see!

Nashita Rahman - 2 years, 2 months ago

Heyyy! Where are you??? How was your JEE in April??

Nashita Rahman - 2 years, 2 months ago

Log in to reply

@Nashita Rahman Lets see.... :D .... Idk.. let the result come out... Even idk about the marks i will be scoring accrdng to Resonance answer keys as response sheet is not out... Lets wait... what else can we do :( ....

Md Zuhair - 2 years, 2 months ago

Log in to reply

@Md Zuhair Good luck with the results :) I am sure you'll do well!

Nashita Rahman - 2 years, 2 months ago

Log in to reply

@Nashita Rahman dont be so sure ;)

Md Zuhair - 2 years, 2 months ago

Log in to reply

@Md Zuhair I am 110% sure :P

Nashita Rahman - 2 years, 2 months ago

Log in to reply

@Nashita Rahman byas... ebar hoye gelo gorbor

Md Zuhair - 2 years, 2 months ago

Log in to reply

@Md Zuhair gorbor keno hobe? Brilliant students der results e kono dino gorbor hoena :P

Nashita Rahman - 2 years, 2 months ago

Log in to reply

@Nashita Rahman orom mone hoy...

Md Zuhair - 2 years, 2 months ago

Log in to reply

@Md Zuhair The answer key has been released?? How much are you getting?

Nashita Rahman - 2 years, 2 months ago

Log in to reply

@Nashita Rahman 262 asche...

Md Zuhair - 2 years, 1 month ago

Log in to reply

@Md Zuhair WOW!! Last time your percentile was 99.508, I think this time it will be more, congratulations!!

Nashita Rahman - 2 years, 1 month ago

Log in to reply

@Nashita Rahman Ekhono don't deduce anything. It's still a long way

Md Zuhair - 2 years, 1 month ago

Log in to reply

@Md Zuhair Okaaaaaaaay! Btw I have a doubt in coding, help me please.While concatenating two strings, when I am giving a sentence as input in first string and for input in second string, it's not being concatenated. But the program is working for words. In short, I can't concatenate two sentences. I am doing it in C programming not Python.

Nashita Rahman - 2 years, 1 month ago

Log in to reply

@Nashita Rahman Is it because while concatenating, the program is reading the spaces as termination? But spaces don't imply '\0' ??? I mean space is not null, right??

Nashita Rahman - 2 years, 1 month ago

Log in to reply

@Nashita Rahman Did u used fflush(stdin) ? (I use this in c++) u can search for d substitute in google

Md Zuhair - 2 years, 1 month ago

Log in to reply

@Md Zuhair Nope I didn't. Anyway, thank you.

Nashita Rahman - 2 years, 1 month ago

@Nashita Rahman More over final answer keys are not out... It's just tentative. Wait... My score might decrease

Md Zuhair - 2 years, 1 month ago

Log in to reply

@Md Zuhair Best of luck with the final answer key!

Nashita Rahman - 2 years, 1 month ago

1 pending report

Vote up reports you agree with

×

Problem Loading...

Note Loading...

Set Loading...