For example I have a number is 55. Can I check this for how many counts number's square create this number.
For example I have 55 So, I know this number total from 5 number, which are
55 = 1^2+ 2^2+ 3^2+ 4^2 + 5^2(totally 5* number)
I found the counts is 5 for 55 . How can I found counts any number. Is there any formula or equation? I dont want to know which number's square. I just want to know how many numbers squares create this number. In my example my answer is 5. but If I can calculate this 10 digit any number its too complex.
For example if my number was 652369. How can I found how many numbers square total from? I just want to find how many number. I m sorry my english. I m using Delphi programming language.
Note: The numbers are not "consecutive" everytime.
There is a closed-form formula for the sum of the first n squares:
1^2 + 2^2 + ... + n^2 = n(n+1)(2n+1)/6
To answer your question, you have to "invert" the function above; in other words, you need to solve the equation
n(n+1)(2n+1)/6 = 55 or 2n^3 + 3n^2 + n - 330 = 0
One way to solve equations like that is by using Newton's method. We have
f(n) = 2n^3 + 3n^2 + n - 330
f'(n) = 6n^2 + 6n + 1
Then you make an initial guess (e.g., n_0 = (330/2)^(1/3)
because 3 is the dominant power) and improve that guess using the formula
n_(k+1) = n_k - f(n_k)/f'(n_k)
You can terminate the algorithm when the change from n_k
to n_(k+1)
is small enough.
I don't know Delphi so here is an implementation in Java.
public class SumOfSquares {
public static double f(double x) {
return ((2*x+3)*x+1)*x;
}
public static double fp(double x) {
return (6*x+6)*x+1;
}
public static void main(String[] args) {
int target = Integer.parseInt(args[0]);
double guess = Math.pow(target/2.0,1/3.0);
double epsilon = 0.00001;
double x0, x1;
x1 = guess;
do {
x0 = x1;
x1 = x0 - (f(x0)-6*target)/fp(x0);
} while (Math.abs(x0-x1)>epsilon);
System.out.println(x1);
// check
int sum = 0;
for (int i=0; i<=Math.floor(x1); i++) {
sum += i*i;
}
System.out.println(sum);
}
}
java SumOfSquares 55
prints out 5.0, and checks the sum of squares up to 5^2 is 55. java SumOfSquares 652369
prints out 124.5855... which indicates that 652369 isn't exactly a sum of squares. The sum of squares below it is 643250.