Count all palindrome which is square of a palindrome | C ++ implementation of the above approach ; check if a number is a palindrome ; Function to return required count of palindromes ; Upper limit ; count odd length palindromes ; if s = '1234' ; then , t = '1234321' ; count even length palindromes ; if s = '1234' ; then , t = '12344321' ; Return count of super - palindromes ; Driver Code ; function call to get required answer
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool ispalindrome ( int x ) { int ans = 0 ; int temp = x ; while ( temp > 0 ) { ans = 10 * ans + temp % 10 ; temp = temp / 10 ; } return ans == x ; } int SuperPalindromes ( int L , int R ) { int LIMIT = 100000 ; int ans = 0 ; for ( int i = 0 ; i < LIMIT ; i ++ ) { string s = to_string ( i ) ; string rs = s . substr ( 0 , s . size ( ) - 1 ) ; reverse ( rs . begin ( ) , rs . end ( ) ) ; string p = s + rs ; int p_sq = pow ( stoi ( p ) , 2 ) ; if ( p_sq > R ) break ; if ( p_sq >= L and ispalindrome ( p_sq ) ) ans = ans + 1 ; } for ( int i = 0 ; i < LIMIT ; i ++ ) { string s = to_string ( i ) ; string rs = s ; reverse ( rs . begin ( ) , rs . end ( ) ) ; string p = s + rs ; int p_sq = pow ( stoi ( p ) , 2 ) ; if ( p_sq > R ) break ; if ( p_sq >= L and ispalindrome ( p_sq ) ) ans = ans + 1 ; } return ans ; } int main ( ) { string L = "4" ; string R = "1000" ; printf ( " % d STRNEWLINE " , SuperPalindromes ( stoi ( L ) , stoi ( R ) ) ) ; return 0 ; }