Open In App

Count N-digits numbers made up of even and prime digits at odd and even positions respectively

Given a positive integer N, the task is to find the number of integers of N digits having even digits at odd indices and prime digits at even indices.

Examples:



Input: N = 2
Output: 20
Explanation:
Following are the possible number of 2-digits satisfying the given criteria {20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 50, 52, 54, 56, 58, 70, 72, 74, 76, 78}. Therefore, the count of such number is 20.

Input: N = 5
Output: 1600



Approach: The given problem can be solved using the concept of Permutations and Combinations by observing the fact that there are only 4 choices for the even positions as [2, 3, 5, 7] and 5 choices for the odd positions as [0, 2, 4, 6, 8]. Therefore, the count of N-digits numbers satisfying the given criteria is given by:

total count = 4P5Q, where P and Q is the number of even and odd positions respectively.

Efficient Approach:

Below is the implementation of the above approach:




// C++ program for the above approache
#include<bits/stdc++.h>
using namespace std;
 
int m = 1000000007;
 
// Function to find the value of x ^ y
int power(int x, int y)
{
     
    // Stores the value of x ^ y
    int res = 1;
 
    // Iterate until y is positive
    while (y > 0)
    {
         
        // If y is odd
        if ((y & 1) != 0)
            res = (res * x) % m;
 
        // Divide y by 2
        y = y >> 1;
 
        x = (x * x) % m;
    }
     
    // Return the value of x ^ y
    return res;
}
 
// Function to find the number of N-digit
// integers satisfying the given criteria
int countNDigitNumber(int N)
{
     
    // Count of even positions
    int ne = N / 2 + N % 2;
 
    // Count of odd positions
    int no = floor(N / 2);
 
    // Return the resultant count
    return power(4, ne) * power(5, no);
}
 
// Driver Code
int main()
{
    int N = 5;
    cout << countNDigitNumber(N) % m << endl;
}
 
// This code is contributed by SURENDRA_GANGWAR




// Java program for the above approach
import java.io.*;
class GFG {
 
static int m = 1000000007;
 
// Function to find the value of x ^ y
static int power(int x, int y)
{
     
    // Stores the value of x ^ y
    int res = 1;
 
    // Iterate until y is positive
    while (y > 0)
    {
         
        // If y is odd
        if ((y & 1) != 0)
            res = (res * x) % m;
 
        // Divide y by 2
        y = y >> 1;
 
        x = (x * x) % m;
    }
     
    // Return the value of x ^ y
    return res;
}
 
// Function to find the number of N-digit
// integers satisfying the given criteria
static int countNDigitNumber(int N)
{
     
    // Count of even positions
    int ne = N / 2 + N % 2;
 
    // Count of odd positions
    int no = (int)Math.floor(N / 2);
 
    // Return the resultant count
    return power(4, ne) * power(5, no);
}
 
// Driver Code
public static void main(String[] args)
{
    int N = 5;
    System.out.println(countNDigitNumber(N) % m);
}
}
 
// This code is contributed by sanjoy_62.




# Python program for the above approach
 
import math
m = 10**9 + 7
 
# Function to find the value of x ^ y
def power(x, y):
   
    # Stores the value of x ^ y
    res = 1
     
    # Iterate until y is positive
    while y > 0:
       
        # If y is odd
        if (y & 1) != 0:
            res = (res * x) % m
             
        # Divide y by 2
        y = y >> 1
         
        x = (x * x) % m
         
    # Return the value of x ^ y
    return res
 
# Function to find the number of N-digit
# integers satisfying the given criteria
def countNDigitNumber(n: int) -> None:
     
    # Count of even positions
    ne = N // 2 + N % 2
     
    # Count of odd positions
    no = N // 2
     
    # Return the resultant count
    return power(4, ne) * power(5, no)
 
# Driver Code
if __name__ == '__main__':
       
    N = 5
    print(countNDigitNumber(N) % m)




// C# program for the above approach
using System;
 
class GFG{
 
static int m = 1000000007;
 
// Function to find the value of x ^ y
static int power(int x, int y)
{
     
    // Stores the value of x ^ y
    int res = 1;
 
    // Iterate until y is positive
    while (y > 0)
    {
         
        // If y is odd
        if ((y & 1) != 0)
            res = (res * x) % m;
 
        // Divide y by 2
        y = y >> 1;
 
        x = (x * x) % m;
    }
     
    // Return the value of x ^ y
    return res;
}
 
// Function to find the number of N-digit
// integers satisfying the given criteria
static int countNDigitNumber(int N)
{
     
    // Count of even positions
    int ne = N / 2 + N % 2;
 
    // Count of odd positions
    int no = (int)Math.Floor((double)N / 2);
 
    // Return the resultant count
    return power(4, ne) * power(5, no);
}
 
 
// Driver Code
public static void Main()
{
    int N = 5;
    Console.Write(countNDigitNumber(N) % m);
}
}
 
// This code is contributed by splevel62.




   <script>
 
        // JavaScript program for the above approache
 
 
        var m = 10 ** 9 + 7
 
        // Function to find the value of x ^ y
        function power(x, y) {
 
            // Stores the value of x ^ y
            var res = 1
 
            // Iterate until y is positive
            while (y > 0) {
 
                // If y is odd
                if ((y & 1) != 0)
                    res = (res * x) % m
 
                // Divide y by 2
                y = y >> 1
 
                x = (x * x) % m
            }
            // Return the value of x ^ y
            return res
        }
        // Function to find the number of N-digit
        // integers satisfying the given criteria
        function countNDigitNumber(N) {
 
            // Count of even positions
            var ne = Math.floor(N / 2) + N % 2
 
            // Count of odd positions
            var no = Math.floor(N / 2)
 
            // Return the resultant count
            return power(4, ne) * power(5, no)
        }
        // Driver Code
 
 
        let N = 5
        document.write(countNDigitNumber(N) % m);
 
 
// This code is contributed by Potta Lokesh
    </script>

Output
1600








Time Complexity: O(log N)
Auxiliary Space: O(1)

Dynamic Approach:

Below is the implementation of the above approach:




#include <iostream>
#include <vector>
 
using namespace std;
 
// Function to check if a number is prime
bool isPrime(int num) {
    if (num <= 1)
        return false;
    for (int i = 2; i * i <= num; i++) {
        if (num % i == 0)
            return false;
    }
    return true;
}
 
// Function to count the number of integers
int countIntegers(int N) {
    vector<vector<int>> dp(N + 1, vector<int>(2));
 
    // Initialize the base cases
    dp[1][0] = 5;  // Even digits at odd indices
    dp[1][1] = 4;  // Prime digits at even indices
 
    for (int i = 2; i <= N; i++) {
        // Calculate the count of even digits at odd indices
        dp[i][0] = 5 * dp[i - 1][1];
 
        // Calculate the count of prime digits at even indices
        dp[i][1] = 4 * dp[i - 1][0];
    }
 
    // Return the total count of valid integers
    return dp[N][1];
}
 
int main() {
    int N = 5;
 
    int count = countIntegers(N);
    cout <<  count <<endl;
 
    return 0;
}
// This code is contributed by rudra1807raj




import java.util.*;
 
class Main {
    // Function to check if a number is prime
    static boolean isPrime(int num) {
        if (num <= 1)
            return false;
        for (int i = 2; i * i <= num; i++) {
            if (num % i == 0)
                return false;
        }
        return true;
    }
 
    // Function to count the number of integers
    static int countIntegers(int N) {
        int[][] dp = new int[N + 1][2];
 
        // Initialize the base cases
        dp[1][0] = 5// Even digits at odd indices
        dp[1][1] = 4// Prime digits at even indices
 
        for (int i = 2; i <= N; i++) {
            // Calculate the count of even digits at odd indices
            dp[i][0] = 5 * dp[i - 1][1];
 
            // Calculate the count of prime digits at even indices
            dp[i][1] = 4 * dp[i - 1][0];
        }
 
        // Return the total count of valid integers
        return dp[N][1];
    }
 
    public static void main(String[] args) {
        int N = 5;
 
        int count = countIntegers(N);
        System.out.println(count);
 
        // This Code Is Contributed By Shubham Tiwari.
    }
}




def isPrime(num):
    if num <= 1:
        return False
    for i in range(2, int(num**0.5) + 1):
        if num % i == 0:
            return False
    return True
 
def countIntegers(N):
    dp = [[0 for _ in range(2)] for _ in range(N + 1)]
 
    # Initialize the base cases
    dp[1][0] = 5  # Even digits at odd indices
    dp[1][1] = 4  # Prime digits at even indices
 
    for i in range(2, N + 1):
        # Calculate the count of even digits at odd indices
        dp[i][0] = 5 * dp[i - 1][1]
 
        # Calculate the count of prime digits at even indices
        dp[i][1] = 4 * dp[i - 1][0]
 
    # Return the total count of valid integers
    return dp[N][1]
 
if __name__ == "__main__":
    N = 5
    count = countIntegers(N)
    print(count)
 
# This Code Is Contributed By Shubham Tiwari




using System;
using System.Collections.Generic;
 
class Program {
    // Function to check if a number is prime
    static bool IsPrime(int num) {
        if (num <= 1)
            return false;
        for (int i = 2; i * i <= num; i++) {
            if (num % i == 0)
                return false;
        }
        return true;
    }
 
    // Function to count the number of integers
    static int CountIntegers(int N) {
        int[][] dp = new int[N + 1][];
        for (int i = 0; i <= N; i++) {
            dp[i] = new int[2];
        }
 
        // Initialize the base cases
        dp[1][0] = 5;  // Even digits at odd indices
        dp[1][1] = 4;  // Prime digits at even indices
 
        for (int i = 2; i <= N; i++) {
            // Calculate the count of even digits at odd indices
            dp[i][0] = 5 * dp[i - 1][1];
 
            // Calculate the count of prime digits at even indices
            dp[i][1] = 4 * dp[i - 1][0];
        }
 
        // Return the total count of valid integers
        return dp[N][1];
    }
 
    static void Main() {
        int N = 5;
        int count = CountIntegers(N);
        Console.WriteLine(count);
 
        // This Code Is Contributed By Shubham Tiwari
    }
}




// Function to check if a number is prime
function isPrime(num) {
    if (num <= 1)
        return false;
    for (let i = 2; i * i <= num; i++) {
        if (num % i === 0)
            return false;
    }
    return true;
}
 
// Function to count the number of integers
function countIntegers(N) {
    let dp = Array(N + 1).fill().map(() => Array(2).fill(0));
 
    // Initialize the base cases
    dp[1][0] = 5;  // Even digits at odd indices
    dp[1][1] = 4;  // Prime digits at even indices
 
    for (let i = 2; i <= N; i++) {
        // Calculate the count of even digits at odd indices
        dp[i][0] = 5 * dp[i - 1][1];
 
        // Calculate the count of prime digits at even indices
        dp[i][1] = 4 * dp[i - 1][0];
    }
 
    // Return the total count of valid integers
    return dp[N][1];
}
 
let N = 5;
let count = countIntegers(N);
console.log(count);
//This Code Is Contributed By Shubham Tiwari

Output
1600





Time Complexity: O(N).
Auxiliary Space: O(N).


Article Tags :