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

6th Feb 2018, 3:53 PM
Animesh Kumar
Animesh Kumar - avatar
3 Respostas
+ 10
you can better use playground to post codes 😃
6th Feb 2018, 4:14 PM
Vukan
Vukan - avatar
+ 2
proposed 10 days ago...at least try finding new ones !
6th Feb 2018, 4:59 PM
VcC
VcC - avatar
- 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; } }
6th Feb 2018, 3:54 PM
Animesh Kumar
Animesh Kumar - avatar