Count all palindrome which is square of a palindrome
Last Updated :
07 Sep, 2022
Given two positive integers L and R (represented as strings) where . The task is to find the total number of super-palindromes in the inclusive range [L, R]. A palindrome is called super-palindrome if it is a palindrome and also square of a palindrome.
Examples:
Input: L = "4", R = "1000"
Output: 4
Explanation: 4, 9, 121, and 484 are super-palindromes.
Input : L = "100000", R = "10000000000"
Output : 11
Approach: Lets say is a super-palindrome. Now since R is a palindrome, the first half of the digits of R can be used to determine R up-to two possibilities. Let i be the first half of the digits in R. For eg. if i = 123, then R = 12321 or R = 123321. Thus we can iterate through these all these digits. Also each possibility can have either odd or even number of digits in R. Thus we iterate through each i upto 105 and create the associated palindrome R, and check whether R2 is a palindrome.
Also, we will handle the odd and even palindromes separately, and break whenever out palindrome goes beyond R. Now since , and and (on Concatenation), where i‘ is reverse of i (in both ways), so our LIMIT will not be greater than .
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
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\n" , SuperPalindromes(stoi(L),
stoi(R)));
return 0;
}
|
Java
import java.lang.*;
class GFG
{
public static boolean ispalindrome( int x)
{
int ans = 0 ;
int temp = x;
while (temp > 0 )
{
ans = 10 * ans + temp % 10 ;
temp = temp / 10 ;
}
return ans == x;
}
public static int SuperPalindromes( int L,
int R)
{
int LIMIT = 100000 ;
int ans = 0 ;
for ( int i = 0 ;i < LIMIT; i++)
{
String s = Integer.toString(i);
StringBuilder rs = new StringBuilder();
rs.append(s.substring( 0 ,
Math.max( 1 , s.length() - 1 )));
String srs = rs.reverse().toString();
String p = s + srs;
int p_sq = ( int )(Math.pow(
Integer.parseInt(p), 2 ));
if (p_sq > R)
{
break ;
}
if (p_sq >= L && ispalindrome(p_sq))
{
ans = ans + 1 ;
}
}
for ( int i = 0 ;i < LIMIT; i++)
{
String s = Integer.toString(i);
StringBuilder rs = new StringBuilder();
rs.append(s);
rs = rs.reverse();
String p = s + rs;
int p_sq = ( int )(Math.pow(
Integer.parseInt(p), 2 ));
if (p_sq > R)
{
break ;
}
if (p_sq >= L && ispalindrome(p_sq))
{
ans = ans + 1 ;
}
}
return ans;
}
public static void main(String [] args)
{
String L = "4" ;
String R = "1000" ;
System.out.println(SuperPalindromes(
Integer.parseInt(L), Integer.parseInt(R)));
}
}
|
Python3
def ispalindrome(x):
ans, temp = 0 , x
while temp > 0 :
ans = 10 * ans + temp % 10
temp = temp / / 10
return ans = = x
def SuperPalindromes(L, R):
L, R = int (L), int (R)
LIMIT = 100000
ans = 0
for i in range (LIMIT):
s = str (i)
p = s + s[ - 2 :: - 1 ]
p_sq = int (p) * * 2
if p_sq > R:
break
if p_sq > = L and ispalindrome(p_sq):
ans = ans + 1
for i in range (LIMIT):
s = str (i)
p = s + s[:: - 1 ]
p_sq = int (p) * * 2
if p_sq > R:
break
if p_sq > = L and ispalindrome(p_sq):
ans = ans + 1
return ans
L = "4"
R = "1000"
print (SuperPalindromes(L, R))
|
C#
using System;
class GFG
{
static bool ispalindrome( int x)
{
int ans = 0;
int temp = x;
while (temp > 0)
{
ans = 10 * ans + temp % 10;
temp = temp / 10;
}
return ans == x;
}
static string Reverse( string s )
{
char [] charArray = s.ToCharArray();
Array.Reverse( charArray );
return new string ( charArray );
}
static int SuperPalindromes( int L, int R)
{
int LIMIT = 100000;
int ans = 0;
for ( int i = 0 ;i < LIMIT; i++)
{
string s = i.ToString();
string rs = s.Substring(0,
Math.Max(1, s.Length - 1));
rs = Reverse(rs);
string p = s + rs;
int p_sq = ( int )(Math.Pow(
Int32.Parse(p), 2));
if (p_sq > R)
{
break ;
}
if (p_sq >= L && ispalindrome(p_sq))
{
ans = ans + 1;
}
}
for ( int i = 0 ;i < LIMIT; i++)
{
string s = i.ToString();
string rs = Reverse(s);
string p = s + rs;
int p_sq = ( int )(Math.Pow(
Int32.Parse(p), 2));
if (p_sq > R)
{
break ;
}
if (p_sq >= L && ispalindrome(p_sq))
{
ans = ans + 1;
}
}
return ans;
}
public static void Main()
{
string L = "4" ;
String R = "1000" ;
Console.WriteLine(SuperPalindromes(
Int32.Parse(L), Int32.Parse(R)));
}
}
|
PHP
<?php
function ispalindrome( $x )
{
$ans = 0;
$temp = $x ;
while ( $temp > 0)
{
$ans = (10 * $ans ) +
( $temp % 10);
$temp = (int)( $temp / 10);
}
return $ans == $x ;
}
function SuperPalindromes( $L , $R )
{
$L = (int) $L ;
$R = (int) $R ;
$LIMIT = 100000;
$ans = 0;
for ( $i = 0 ; $i < $LIMIT ; $i ++)
{
$s = (string) $i ;
$rs = substr ( $s , 0, strlen ( $s ) - 1);
$p = $s . strrev ( $rs );
$p_sq = (int) $p ** 2;
if ( $p_sq > $R )
{
break ;
}
if ( $p_sq >= $L and ispalindrome( $p_sq ))
{
$ans = $ans + 1;
}
}
for ( $i = 0 ; $i < $LIMIT ; $i ++)
{
$s = (string) $i ;
$p = $s . strrev ( $s );
$p_sq = (int) $p ** 2;
if ( $p_sq > $R )
{
break ;
}
if ( $p_sq >= $L and ispalindrome( $p_sq ))
{
$ans = $ans + 1;
}
}
return $ans ;
}
$L = "4" ;
$R = "1000" ;
echo SuperPalindromes( $L , $R );
?>
|
Javascript
<script>
function ispalindrome(x){
let ans = 0,temp = x
while (temp > 0){
ans = 10 * ans + temp % 10
temp = Math.floor(temp / 10)
}
return ans == x
}
function SuperPalindromes(L, R)
{
L = parseInt(L),R = parseInt(R)
let LIMIT = 100000
let ans = 0
for (let i = 0; i < LIMIT; i++)
{
let s = i.toString()
let rs = s.substring(0,s.length-1)
rs = rs.split( '' ).reverse().join( '' )
let p = s + rs
let p_sq = Math.pow(parseInt(p),2)
if (p_sq > R)
break
if (p_sq >= L && ispalindrome(p_sq))
ans = ans + 1
}
for (let i = 0; i < LIMIT; i++)
{
let s = i.toString()
let rs = s
rs = rs.split( '' ).reverse().join( '' )
let p = s + rs
let p_sq = Math.pow(parseInt(p),2)
if (p_sq > R)
break
if (p_sq >= L && ispalindrome(p_sq))
ans = ans + 1
}
return ans
}
let L = "4"
let R = "1000"
document.write(SuperPalindromes(L, R))
</script>
|
Complexity Analysis:
- Time Complexity: O(N*log(N)) where N is upper limit and the log(N) term comes from checking if a candidate is palindrome.
- Auxiliary Space: O(LIMIT)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...