Reversible numbers
Last Updated :
12 Sep, 2023
A number is said to be a reversible if sum of the number and its reverse had only odd digits. The problem is to find out if a number is reversible or not.
Examples:
Input: 36
Output: Reversible number
as 36 + 63 = 99 has only odd digits.
Input: 409
Output: Reversible number
as 409 + 904 = 1313 has only odd digits.
Input: 35
Output: Not Reversible number
as 35 + 53 = 88 has only odd digits
Naive Method
Calculate reverse of each number and add it to the number. If the resultant is reversible increment the value of count. Calculate this for every number from 1 to n.
Time complexity: O(10^n) as it should calculate reverse of each number.
Advance method
- 1 digit number: Any one digit number will add to itself, which always be an even number, And thus there are no solutions.
- 2 digits number: Both digits must be odd.
- If a+b > 10 ,then we have a carryover and thus the first digit of the result will have a different parity than the second digit.
- So, Solutions can only be formed where a+b < 10 and a + b is odd. So, total 20 such numbers are possible.
- 3 digits number:
- The middle digit needs to be added to itself. That means that the third digit must have a carryover and be odd.
- Since the third digit is odd the first digit is odd as well if the second digit does not have a carryover, which happens when the second digit is less than 5, which gives us 20 choices for the first/third digit set and 5 options for the middle.So, totals 100 pairs.
- 4 digits number: There are two pairs, say the inner and outer pair.
- If the inner pair has carryover then the outer pair must also have carryover.
- Otherwise, the two inner pairs will have different parity.
- If the inner pair has carryover then the outer pair will have different parity since the first digit will end up with a carry over which the last digit would not get.
- Therefore we have solutions only when none of the pairs have carry over.
- In total: For the outer pair, this gives us the 20 choices we have seen in the two digit case. And it gives us 30 cases for the inner pair since they can also contain a zero.
Or in total we get 20*30 = 600 solutions.
- 5, 9, 13.. digits number: No solution as 1-digit number.
- 6, 8, 10.. digits number: Same as 2-digits number i.e. if n = 2*k then total solution = 20*30^(k-1).
- 7, 11, 15.. digits number: Same as 3-digits number i.e if n = 4k + 3 then total solution = 100*500^(k).
Generalizing the Solution:
- All even numbered digits(2, 4, 6, 8..) have the same formula so we can generalize
that for some integer k such that n = 2k we have 20*30^(k-1) solutions
which represents the outer pair along with all the inner pairs.
- For n (3, 7, 11..) of form 4k + 3 (k is an integer), we have that the middle digit and
the outer pair gives us 5 and 20 options, as in the case of 3 digit number.
Then we have sets of internal pairs which gives us 20 and 25 solutions.
So that means we can generalize it to 20*5*(20*25)^(k) = 100*500^(k).
- For n of form 4k+ 1 which means 1, 5, 9.. none of these have any solutions.
Program To check if a number is reversible or not
C++
#include <iostream>
#include <cmath>
using namespace std;
void checkReversible ( int n)
{
int rev = 0, rem;
int flag = n;
while (flag)
{
rem = flag % 10;
rev *= 10;
rev += rem;
flag /= 10;
}
int sum = rev + n;
while (sum && (rem % 2 != 0))
{
rem = sum % 10;
sum /= 10;
}
if (sum == 0)
cout << "Reversible Number" ;
else
cout << "Non-Reversible Number" ;
}
int main()
{
int n = 36;
checkReversible(n);
return 0;
}
|
Java
import java.io.*;
class GFG {
static void checkReversible ( int n)
{
int rev = 0 , rem = 0 ;
int flag = n;
while (flag> 0 )
{
rem = flag % 10 ;
rev *= 10 ;
rev += rem;
flag /= 10 ;
}
int sum = rev + n;
while (sum > 0 && (rem % 2 != 0 ))
{
rem = sum % 10 ;
sum /= 10 ;
}
if (sum == 0 )
System.out.println( "Reversible Number" );
else
System.out.println( "Non-Reversible Number" );
}
public static void main (String[] args)
{
int n = 36 ;
checkReversible(n);
}
}
|
Python3
def checkReversible (n):
rev = 0
flag = n
while (flag ! = 0 ):
rem = flag % 10
rev * = 10
rev + = rem
flag / / = 10
sum = rev + n
while ( sum and ((rem % 2 ) ! = 0 )):
rem = sum % 10
sum / / = 10
if ( sum = = 0 ):
print ( "Reversible Number" )
else :
print ( "Non-Reversible Number" )
n = 36
checkReversible(n)
|
C#
using System;
class GFG {
static void checkReversible ( int n)
{
int rev = 0, rem = 0;
int flag = n;
while (flag > 0)
{
rem = flag % 10;
rev *= 10;
rev += rem;
flag /= 10;
}
int sum = rev + n;
while (sum > 0 && (rem % 2 != 0))
{
rem = sum % 10;
sum /= 10;
}
if (sum == 0)
Console.WriteLine( "Reversible Number" );
else
Console.WriteLine( "Non-Reversible Number" );
}
public static void Main ()
{
int n = 36;
checkReversible(n);
}
}
|
Javascript
function checkReversible (n)
{
var rev = 0;
var flag = n;
while (flag != 0)
{
rem = flag % 10
rev *= 10
rev += rem
flag = Math.floor(flag / 10);
}
var sum = rev + n;
while (sum && ((rem % 2) != 0))
{
rem = sum % 10
sum = Math.floor(sum / 10);
}
if (sum == 0)
console.log( "Reversible Number" )
else
console.log( "Non-Reversible Number" )
}
let n = 36
checkReversible(n);
|
Output:
Reversible Number
Time complexity: O(log N) where N is no of digits of number n
Program To count total reversible numbers upto n
C++
#include <iostream>
#include <cmath>
using namespace std;
void countReversible ( int n)
{
int count = 0;
for ( int i = 1; i <= n; i++)
{
switch (i % 4)
{
case 0:
case 2:
count += 20 * pow ( 30, ( i / 2 - 1));
break ;
case 3:
count += 100 * pow ( 500, i / 4 );
break ;
case 1:
break ;
}
}
cout << count;
}
int main()
{
int n = 9;
countReversible(n);
return 0;
}
|
Java
import java.io.*;
class GFG {
static void countReversible ( int n)
{
int count = 0 ;
for ( int i = 1 ; i <= n; i++)
{
switch (i % 4 )
{
case 0 :
case 2 :
count += 20 * Math.pow( 30 , ( i / 2 - 1 ));
break ;
case 3 :
count += 100 * Math.pow ( 500 , i / 4 );
break ;
case 1 :
break ;
}
}
System.out.println(count);
}
public static void main (String[] args)
{
int n = 9 ;
countReversible(n);
}
}
|
Python3
def countReversible (n):
count = 0 ;
for i in range ( 1 , n + 1 ):
case = i % 4
if case = = 0 or case = = 2 :
count + = 20 * pow ( 30 , ( i / / 2 - 1 ));
elif case = = 3 :
count + = 100 * pow ( 500 , (i / / 4 ) );
elif case = = 1 :
pass ;
print (count);
n = 9 ;
countReversible(n);
|
C#
using System;
class GFG {
static void countReversible ( int n)
{
int count = 0;
for ( int i = 1; i <= n; i++)
{
switch (i % 4)
{
case 0:
case 2:
count += 20 * ( int )Math.Pow( 30, ( i / 2 - 1));
break ;
case 3:
count += 100 * ( int )Math.Pow ( 500, i / 4 );
break ;
case 1:
break ;
}
}
Console.WriteLine(count);
}
public static void Main ()
{
int n = 9;
countReversible(n);
}
}
|
Javascript
function countReversible (n)
{
let count = 0;
for (let i = 1; i <= n; i++)
{
switch (i % 4)
{
case 0:
case 2:
count += 20 * Math.pow( 30, Math.floor( i / 2 - 1));
break ;
case 3:
count += 100 * Math.pow ( 500, Math.floor(i / 4) );
break ;
case 1:
break ;
}
}
console.log (count);
}
let n = 9;
countReversible(n);
|
Output:
608720
Time complexity : O(nlogn)
Auxiliary Space : O(1)
Reference : Project Euler 145: How many reversible numbers are there below one-billion?
Share your thoughts in the comments
Please Login to comment...