Probability that a N digit number is palindrome

Given an integer N, the task is to find the probability that a number with a number of digits as N is a palindrome.
The number may have leading zeros.

Examples:

Input: N = 5
Output: 1 / 100



Input: N = 6
Output: 1 / 1000

Solution:

  • As leading zeroes are allowed total number of N digit number is 10N.
  • A number is a palindrome when first N/2 digits match with last N/2 digits in reverse order.
  • For even number of digits, we can pick first N/2 digits and then duplicate them to form the rest of N/2 digits so we can choose (N)/2 digits.
  • For an odd number of digits we can pick first (N-1)/2 digits and then duplicate them to form the rest of (N-1)/2 digits so we can choose (N+1)/2 digits.
  • So the probability that an N digit number is palindrome is 10ceil( N / 2 ) / 10N or 1 / 10floor( N / 2 )

Below is the implementation of the approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ code of above approach
#include <bits/stdc++.h>
using namespace std;
  
// Find the probability that a
// n digit number is palindrome
void solve(int n)
{
    int n_2 = n / 2;
  
    // Denominator
    string den;
    den = "1";
  
    // Assign 10^(floor(n/2)) to
    // denominator
    while (n_2--)
        den += '0';
  
    // Display the answer
    cout << 1 << "/" << den << "\n";
}
  
// Driver code
int main()
{
  
    int N = 5;
  
    solve(N);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code of above approach
import java.util.*;
  
class GFG 
{
  
// Find the probability that a
// n digit number is palindrome
static void solve(int n)
{
    int n_2 = n / 2;
  
    // Denominator
    String den;
    den = "1";
  
    // Assign 10^(floor(n/2)) to
    // denominator
    while (n_2-- > 0)
        den += '0';
  
    // Display the answer
    System.out.println(1 + "/" + den);
}
  
// Driver code
public static void main(String[] args) 
{
    int N = 5;
  
    solve(N);
}
  
// This code is contributed by Rajput-Ji

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code of above approach 
  
# Find the probability that a 
# n digit number is palindrome 
def solve(n) : 
  
    n_2 = n // 2
  
    # Denominator 
    den = "1"
  
    # Assign 10^(floor(n/2)) to 
    # denominator 
    while (n_2) : 
        den += '0'
          
        n_2 -= 1
          
    # Display the answer
    print(str(1) + "/" + str(den))
      
# Driver code 
if __name__ == "__main__"
  
    N = 5
  
    solve(N); 
  
# This code is contributed by AnkitRai01

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach
using System;
  
class GFG 
{
  
// Find the probability that a
// n digit number is palindrome
static void solve(int n)
{
    int n_2 = n / 2;
  
    // Denominator
    String den;
    den = "1";
  
    // Assign 10^(floor(n/2)) to
    // denominator
    while (n_2-- > 0)
        den += '0';
  
    // Display the answer
    Console.WriteLine(1 + "/" + den);
}
  
// Driver code
public static void Main(String[] args) 
{
    int N = 5;
  
    solve(N);
}
  
// This code is contributed by PrinciRaj1992

chevron_right


Output:

1/100


My Personal Notes arrow_drop_up

Second year Department of Information Technology Jadavpur University

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.