Count numbers from a range whose cube is a Palindrome | C ++ program of the above approach ; Function to check if n is a pallindrome number or not ; Temporarily store n ; Stores reverse of n ; Iterate until temp reduces to 0 ; Extract the last digit ; Add to the start ; Remove the last digit ; If the number and its reverse are equal ; Otherwise ; Function to precompute and store the count of numbers whose cube is a palindrome number ; Iterate upto 10 ^ 4 ; Check if i * i * i is a pallindrome or not ; Convert arr [ ] to prefix sum array ; Driver Code ; Given queries ; Using inclusion - exclusion principle , count required numbers
#include <bits/stdc++.h> NEW_LINE using namespace std ; int arr [ 10005 ] ; int isPalindrome ( int n ) { int temp = n ; int res = 0 ; while ( temp != 0 ) { int rem = temp % 10 ; res = res * 10 + rem ; temp /= 10 ; } if ( res == n ) { return 1 ; } else return 0 ; } void precompute ( ) { for ( int i = 1 ; i <= 10000 ; i ++ ) { if ( isPalindrome ( i * i * i ) ) arr [ i ] = 1 ; else arr [ i ] = 0 ; } for ( int i = 1 ; i <= 10000 ; i ++ ) { arr [ i ] = arr [ i ] + arr [ i - 1 ] ; } } int main ( ) { vector < pair < int , int > > Q = { { 2 , 7 } , { 10 , 25 } } ; precompute ( ) ; for ( auto it : Q ) { cout << arr [ it . second ] - arr [ it . first - 1 ] << " STRNEWLINE " ; } return 0 ; }