0
[Challenge] Palindromic Sum
The palindromic number 595 is interesting because it can be written as the sum of consecutive squares: 6^2+7^2Ā +8^2Ā +9^2Ā +10^2+ 11^2+12^2. There are exactly eleven palindromes below one-thousand that can written as consecutive square sums, and the sum of these palindromes is 4164. Note that 1 = 0^2Ā + 1^2Ā has not been included as this problem is concerned with the squares of positive integers. Find sum of all numbers less than 10^8Ā that are both palindromic & can be written as t
3 Answers
+ 10
you can better use playground to post codes š
+ 2
proposed 10 days ago...at least try finding new ones !
- 2
/*
Description: The code finds sum of all numbers less than 10^8Ā that are both palindromic & can be written as the sum of consecutive squares
*/
import java.util.ArrayList;
import java.util.Collections;
public class PalindromicSum{
public static void main(String...arg){
int n=100_000_000;
System.out.println("Test case for n < 1000 :");
FindSum((int)Math.sqrt(1000),1000);
System.out.println("\n\nOutput for n < 10^8 :");
FindSum((int)Math.sqrt(n),n);
}
static void FindSum(int sqrtN,int N){
long sum=0;
ArrayList<Integer>list=new ArrayList<>();
for(int i=1;i<=sqrtN;i++){
int n=i*i;
for(int j=i+1;j<=sqrtN;j++){
n+=j*j;
if(n>N) break;
if(IsPalindrome(n)&&!list. contains(n)){
sum+=n;
list.add(n);
}
}
}
Collections.sort(list);
System.out.print("Total sum = "+sum+"\nList of numbers:\n"+list);
}
static boolean IsPalindrome(int n){
int r=0,i=n;
while(i>0){
r=10*r+i%10;
i/=10;
}
return n==r;
}
}