Reversible numbers

21 Jun, 2022
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.
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.
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.
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++
Java
Python3
C#
// C++ program to check if a given// number is reversible or not#include <iostream>#include <cmath>using namespace std; // Function to check reversible numbervoid checkReversible (int n){    int rev = 0, rem;     // Calculate reverse of n    int flag = n;    while (flag)    {        rem = flag % 10;        rev *= 10;        rev += rem;        flag /= 10;    }     // Calculate sum of number    // and its reverse    int sum = rev + n;     // Check for reverse number    // reach digit must be odd    while (sum && (rem % 2 != 0))    {        rem = sum % 10;        sum /= 10;    }     if (sum == 0)        cout << "Reversible Number";    else        cout << "Non-Reversible Number";} // Driver functionint main(){    int n = 36;    checkReversible(n);    return 0;}
// Java program to check if a given// number is reversible or notimport java.io.*; class GFG {         // Function to check reversible number    static void checkReversible (int n)    {        int rev = 0, rem = 0;             // Calculate reverse of n        int flag = n;        while (flag>0)        {            rem = flag % 10;            rev *= 10;            rev += rem;            flag /= 10;        }             // Calculate sum of number        // and its reverse        int sum = rev + n;             // Check for reverse number        // reach digit must be odd        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");    }         // Driver function    public static void main (String[] args)    {        int n = 36;        checkReversible(n);             }}// This code is contributed by vt_m.
# Python3 program to check if a given# number is reversible or not # Function to check reversible numberdef checkReversible (n):         rev = 0       # Calculate reverse of n    flag = n         while (flag != 0):        rem = flag % 10        rev *= 10        rev += rem        flag //= 10         # Calculate sum of number    # and its reverse    sum = rev + n       # Check for reverse number    # reach digit must be odd    while (sum and ((rem % 2) != 0)):        rem = sum % 10        sum //= 10         if (sum == 0):        print("Reversible Number")    else:        print("Non-Reversible Number") # Driver Coden = 36 checkReversible(n) # This code is contributed by sanjoy_62
// C# program to check if a given// number is reversible or notusing System; class GFG {         // Function to check reversible number    static void checkReversible (int n)    {        int rev = 0, rem = 0;             // Calculate reverse of n        int flag = n;        while (flag > 0)        {            rem = flag % 10;            rev *= 10;            rev += rem;            flag /= 10;        }             // Calculate sum of number        // and its reverse        int sum = rev + n;             // Check for reverse number        // reach digit must be odd        while (sum > 0 && (rem % 2 != 0))        {            rem = sum % 10;            sum /= 10;        }             if (sum == 0)        Console.WriteLine("Reversible Number");        else        Console.WriteLine("Non-Reversible Number");    }         // Driver function    public static void Main ()    {        int n = 36;          checkReversible(n);             }} // This code is contributed by vt_m.
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++
Java
C#
// C++ program to find the count of// reversible numbers upto a given number n#include <iostream>#include <cmath>using namespace std; // Function to calculate the count of reversible numbervoid countReversible (int n){    int count = 0;     // Calculate counts of    // reversible number of 1 to n-digits    for ( int i = 1; i <= n; i++)    {        // All four possible cases and their formula        switch (i % 4)        {         // for i of form 2k        case 0:        case 2:            count += 20 * pow( 30, ( i / 2 - 1));            break;         // for i of form 4k + 3        case 3:            count += 100 * pow ( 500, i / 4 );            break;         // for i of form 4k + 1 no solution        case 1:            break;        }    }    cout << count;} // Driver functionint main(){    // count up-to 9 digit numbers (1 billion)    int n = 9;    countReversible(n);    return 0;}
// Java program to find the count of// reversible numbers upto a given number nimport java.io.*; class GFG {     // Function to calculate the count    // of reversible number    static void countReversible (int n)    {        int count = 0;             // Calculate counts of        // reversible number of 1 to n-digits        for ( int i = 1; i <= n; i++)        {            // All four possible cases            // and their formula            switch (i % 4)            {                 // for i of form 2k            case 0:            case 2:                count += 20 * Math.pow( 30, ( i / 2 - 1));                break;                 // for i of form 4k + 3            case 3:                count += 100 * Math.pow ( 500, i / 4 );                break;                 // for i of form 4k + 1 no solution            case 1:                break;            }        }        System.out.println(count);    }         // Driver function    public static void main (String[] args)    {        // count up-to 9 digit numbers (1 billion)        int n = 9;        countReversible(n);             }}// This code is contributed by vt_m.
// C# program to find the count of// reversible numbers upto a given number nusing System; class GFG {         // Function to calculate the    // count of reversible number    static void countReversible (int n)    {        int count = 0;             // Calculate counts of        // reversible number of 1 to n-digits        for ( int i = 1; i <= n; i++)        {            // All four possible cases            // and their formula            switch (i % 4)            {                 // for i of form 2k            case 0:            case 2:                count += 20 * (int)Math.Pow( 30, ( i / 2 - 1));                break;                 // for i of form 4k + 3            case 3:                count += 100 * (int)Math.Pow ( 500, i / 4 );                break;                 // for i of form 4k + 1 no solution            case 1:                break;            }        }        Console.WriteLine(count);    }         // Driver function    public static void Main ()    {        // count up-to 9 digit numbers (1 billion)        int n = 9;        countReversible(n);             }} // This code is contributed by vt_m.
Output: 
608720
Time complexity : O(nlogn) Auxiliary Space : O(1)
Reference : Project Euler 145: How many reversible numbers are there below one-billion? 
This article is contributed by Shivam Pradhan (anuj_charm). If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
sanjoy_62
polymatir3j
series
Mathematical
Mathematical
series
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.