Given an integer N, the task is to find the number of permutations of first N positive integers such that prime numbers are at prime indices (for 1-based indexing).
Note: Since, the number of ways may be very large, return the answer modulo 109 + 7.
Examples:
Input: N = 3
Output: 2
Explanation:
Possible permutation of first 3 positive integers, such that prime numbers are at prime indices are: {1, 2, 3}, {1, 3, 2}
Input: N = 5
Output: 12
Explanation:
Some of the possible permutation of first 5 positive integers, such that prime numbers are at prime indices are: {1, 2, 3, 4}, {1, 3, 2, 4}, {4, 2, 3, 1}, {4, 3, 2, 1}
Approach: There are K number of primes from 1 to N and there is exactly K number of prime indexes from index 1 to N. So the number of permutations for prime numbers is K!. Similarly, the number of permutations for non-prime numbers is (N-K)!. So the total number of permutations is K!*(N-K)!
For example:
Given test case: [1,2,3,4,5].
2, 3 and 5 are fixed on prime index slots,
we can only swap them around.
There are 3 x 2 x 1 = 3! ways
[[2,3,5], [2,5,3], [3,2,5],
[3,5,2], [5,2,3], [5,3,2]],
For Non-prime numbers - {1,4}
[[1,4], [4,1]]
So the total is 3!*2!
Below is the implementation of the above approach:
C++
// C++ implementation to find the // permutation of first N positive // integers such that prime numbers // are at the prime indices#define mod 1000000007#include<bits/stdc++.h>using namespace std;int fact(int n){ int ans = 1; while(n > 0) { ans = ans * n; n--; } return ans;}// Function to check that// a number is prime or not bool isPrime(int n){ if(n <= 1) return false; // Loop to check that // number is divisible by any // other number or not except 1 for(int i = 2; i< sqrt(n) + 1; i++) { if(n % i == 0) return false; else return true; }} // Function to find the permutationsvoid findPermutations(int n){ int prime = 0; // Loop to find the // number of prime numbers for(int j = 1; j < n + 1; j++) { if(isPrime(j)) prime += 1; } // Permutation of N // positive integers int W = fact(prime) * fact(n - prime); cout << (W % mod);}// Driver Codeint main(){ int n = 7; findPermutations(n);}// This code is contributed by Bhupendra_Singh |
Java
// Java implementation to find the // permutation of first N positive // integers such that prime numbers // are at the prime indicesimport java.io.*;class GFG{ static int mod = 1000000007;static int fact(int n){ int ans = 1; while(n > 0) { ans = ans * n; n--; } return ans;} // Function to check that// a number is prime or not static boolean isPrime(int n){ if(n <= 1) return false; // Loop to check that // number is divisible by any // other number or not except 1 for(int i = 2; i< Math.sqrt(n) + 1; i++) { if(n % i == 0) return false; else return true; } return true;} // Function to find the permutationsstatic void findPermutations(int n){ int prime = 0; // Loop to find the // number of prime numbers for(int j = 1; j < n + 1; j++) { if(isPrime(j)) prime += 1; } // Permutation of N // positive integers int W = fact(prime) * fact(n - prime); System.out.println(W % mod);} // Driver Codepublic static void main (String[] args){ int n = 7; findPermutations(n);}}// This code is contributed by shubhamsingh10 |
Python3
# Python3 implementation to find the # permutation of first N positive # integers such that prime numbers # are at the prime indicesimport math# Function to check that# a number is prime or not def isPrime(n): if n <= 1: return False # Loop to check that # number is divisible by any # other number or not except 1 for i in range(2, int(n**0.5)+1): if n % i == 0: return False else: return True # Constant value for moduloCONST = int(math.pow(10, 9))+7# Function to find the permutationsdef findPermutations(n): prime = 0 # Loop to find the # number of prime numbers for j in range (1, n + 1): if isPrime(j): prime+= 1 # Permutation of N # positive integers W = math.factorial(prime)*\ math.factorial(n-prime) print (W % CONST)# Driver Codeif __name__ == "__main__": n = 7 # Function Call findPermutations(n) |
C#
// C# implementation to find the // permutation of first N positive // integers such that prime numbers // are at the prime indicesusing System;class GFG{ static int mod = 1000000007;static int fact(int n){ int ans = 1; while(n > 0) { ans = ans * n; n--; } return ans;} // Function to check that// a number is prime or not static bool isPrime(int n){ if(n <= 1) return false; // Loop to check that // number is divisible by any // other number or not except 1 for(int i = 2; i < Math.Sqrt(n) + 1; i++) { if(n % i == 0) return false; else return true; } return true;} // Function to find the permutationsstatic void findPermutations(int n){ int prime = 0; // Loop to find the // number of prime numbers for(int j = 1; j < n + 1; j++) { if(isPrime(j)) prime += 1; } // Permutation of N // positive integers int W = fact(prime) * fact(n - prime); Console.Write(W % mod);} // Driver Codepublic static void Main(){ int n = 7; findPermutations(n);}}// This code is contributed by Code_Mech |
144
Recommended Posts:
- Permutation of first N positive integers such that prime numbers are at prime indices | Set 2
- Number of valid indices in the permutation of first N natural numbers
- Find a permutation such that number of indices for which gcd(p[i], i) > 1 is exactly K
- Lexicographically Smallest Permutation of length N such that for exactly K indices, a[i] > a[i] + 1
- Lexicographically smallest permutation of size A having B integers exceeding all preceeding integers
- Generate permutation of 1 to N such that absolute difference of consecutive numbers give K distinct integers
- Count of digits after concatenation of first N positive integers
- Find the number of sub arrays in the permutation of first N natural numbers such that their median is M
- Find the permutation of first N natural numbers such that sum of i % Pi is maximum possible
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Find two co-prime integers such that the first divides A and the second divides B
- Minimum number of changes such that elements are first Negative and then Positive
- Minimum number of given operations required to convert a permutation into an identity permutation
- Minimum number of adjacent swaps required to convert a permutation to another permutation by given condition
- Count 'd' digit positive integers with 0 as a digit
- Count positive integers with 0 as a digit and maximum 'd' digits
- Number of arrays of size N whose elements are positive integers and sum is K
- Ways to write N as sum of two or more positive integers | Set-2
- Find n positive integers that satisfy the given equations
- Check whether product of integers from a to b is positive , negative or zero
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.


