Reputation: 12631
The sum of squares of the 3 consecutive numbers 11, 12 and 13 is 434 (that is 121 + 144 + 169 = 434). The number 434 reads the same from both ways and is called a palindrome. I need to find out the sum of the numbers less than 10^7
that can be expressed as the sum of consecutive squares and results in a
palindrome. If in 2 different sequences, a number repeats, then sum
them twice. That is if 11 occurs in 2 consecutive number sequences, sum it twice.
I need to write a program based on the above scenario.
What I understood is we have to find squares up to 10,000,000 and then all the numbers. How should I approach writing a program to do this in C?
Upvotes: 2
Views: 556
Reputation: 4753
Using brute force way is one such possible way.
Iterate a variable i
from 1 to 10^7 - 2
so that you are going to take sum of squares of first three value of variable (including i) and find whether its palindrome or not.
ie. when i=5
, in a for
loop
you need to find whether i^2 + (i+1)^2 + (i+2)^2 is palindrome or not.
I am not sure but you rather take long long
as you need to calculate squares.
Upvotes: 0
Reputation: 9424
You probably need a for loop which increments a variable? Using this variable you can generate 3 consecutive numbers.. then sum up the squared numbers.. if it's above your max number you stop the loop. if it's below you check whether it's a palindrom?
Upvotes: 0