Open In App

Count of numbers of length N having prime numbers at odd indices and odd numbers at even indices

Improve
Improve
Like Article
Like
Save
Share
Report

Given a number N, the task is to calculate the count of numbers of length N having prime numbers at odd indices and odd numbers at even indices.

Example:

Input :  N = 1
Output:   5
Explanation : All valid numbers length 1 are 1, 3, 5, 7, 9, here we have only 1 odd index, therefore we have 5 valid numbers.

Input: N = 2
Output:  20 
Explanation: There are 20 valid numbers of length 2.

 

Approach :  The problem can be solved with the help of combinatorics. The digits at odd indices have 4 choices and the digits at even indices have 5 choices.
Follow the steps to solve the problem:

  • There are 5 choices for even indices (1, 3, 5, 7, 9 ) and 4 choices for odd indices (2, 3, 5, 7 ).
  • For a number of length N, there will be N/2 odd indices and (N/2 + N%2) even indices.
  • So, the number of ways to fill N/2 odd indices are  4 N/2.
  • And  the number of ways to fill even indices are  5(N/2 + N%2).
  • Hence, the total count of all the valid numbers will be 4N/2  * 5 (N/2 + N%2).

Below is the implementation of above approach: 

C++




// c++ program to Count of numbers of length
// N having prime numbers at odd indices and
// odd numbers at even indices
#include<bits/stdc++.h>
using namespace std;
// function to find total number of ways
int find_Numb_ways(int n)
{
    // No of odd indices in n-digit number
    int odd_indices = n/2;
   
    // No of even indices in n-digit number
    int even_indices = (n / 2) + (n % 2);
   
    //  No of ways of arranging prime number
    //  digits in odd indices
    int arr_odd = pow(4, odd_indices);
   
    //   No of ways of arranging odd number
    //  digits in even indices
    int arr_even = pow(5, even_indices);
   
    // returning the total number of ways
    return arr_odd * arr_even;
}
 
// drive code
int main()
{
    int n = 4;
    cout << find_Numb_ways(n) << endl;
    return 0;
}
 
// This code is contributed by kondamrohan02.


Java




// Java program to Count of numbers of length
// N having prime numbers at odd indices and
// odd numbers at even indices
import java.util.*;
 
class GFG
{
 
// function to find total number of ways
static int find_Numb_ways(int n)
{
   
    // No of odd indices in n-digit number
    int odd_indices = n/2;
   
    // No of even indices in n-digit number
    int even_indices = (n / 2) + (n % 2);
   
    //  No of ways of arranging prime number
    //  digits in odd indices
    int arr_odd = (int)Math.pow(4, odd_indices);
   
    //   No of ways of arranging odd number
    //  digits in even indices
    int arr_even = (int)Math.pow(5, even_indices);
   
    // returning the total number of ways
    return arr_odd * arr_even;
}
 
    // Driver Code
    public static void main(String[] args) {
        int n = 4;
     System.out.print(find_Numb_ways(n));
 
    }
}
 
// This code is contributed by code_hunt.


Python3




# python program for above approach
def count(N):
 
    # No of odd indices in N-digit number
    odd_indices = N//2
 
    # No of even indices in N-digit number
    even_indices = N//2 + N % 2
 
    # No of ways of arranging prime number
    # digits in odd indices
    arrange_odd = 4 ** odd_indices
 
    # No of ways of arranging odd number
    # digits in even indices
    arrange_even = 5 ** even_indices
 
    # returning the total number of ways
    return arrange_odd * arrange_even
 
 
# Driver code
if __name__ == "__main__":
 
    N = 4
    # calling the function
    print(count(N))


C#




// C# program to Count of numbers of length
// N having prime numbers at odd indices and
// odd numbers at even indices
using System;
using System.Collections.Generic;
 
class GFG{
 
// function to find total number of ways
static int find_Numb_ways(int n)
{
    // No of odd indices in n-digit number
    int odd_indices = n/2;
   
    // No of even indices in n-digit number
    int even_indices = (n / 2) + (n % 2);
   
    //  No of ways of arranging prime number
    //  digits in odd indices
    int arr_odd = (int)Math.Pow(4, odd_indices);
   
    //   No of ways of arranging odd number
    //  digits in even indices
    int arr_even = (int)Math.Pow(5, even_indices);
   
    // returning the total number of ways
    return arr_odd * arr_even;
}
 
// drive code
public static void Main()
{
    int n = 4;
    Console.Write(find_Numb_ways(n));
}
}
 
// This code is contributed by SURENDRA_GANGWAR.


Javascript




<script>
 
// Javascript program to Count of numbers of length
// N having prime numbers at odd indices and
// odd numbers at even indices
 
// function to find total number of ways
function find_Numb_ways(n)
{
    // No of odd indices in n-digit number
    var odd_indices = n/2;
 
    // No of even indices in n-digit number
    var even_indices = (n / 2) + (n % 2);
   
    //  No of ways of arranging prime number
    //  digits in odd indices
    var arr_odd = Math.pow(4, odd_indices);
   
    //   No of ways of arranging odd number
    //  digits in even indices
    var arr_even = Math.pow(5, even_indices);
   
    // returning the total number of ways
    return arr_odd * arr_even;
}
 
// drive code
    var n = 4;
    document.write(find_Numb_ways(n));
 
// This code is contributed by ipg2016107.
</script>


Output

400

Time Complexity: O(logn), because it is using inbuilt pow function
Auxiliary Space : O(1), (No additional space required)



Last Updated : 30 Aug, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads