You are given an integer, ‘N’. Your task is to determine the total number of different permutations of the numbers from 1 to ‘N’, such that in every such permutation, all prime numbers within the range, 1 to ‘N’ are present at prime indices(1-indexed), and composite numbers at composite indices.
Note:1. The permutation [1,4,3,2] is not a valid permutation because 2 is a prime number and thus it should be placed at a prime index but in this permutation, it is placed at index 4(1-indexed). Also, at index 2, a prime number should have been placed as 2 is a prime number but here, 4 is placed which is not a prime number. So, this is an invalid permutation.
2. The permutation [1,3,2,4] is a valid permutation because 2 and 3 are prime numbers, so index 2 and index 3 are prime indices. In this permutation, prime numbers are placed at prime indices only and composite numbers are placed at composite indices so this is a valid permutation.
The first line contains an integer ‘T’, which denotes the number of test cases to be run. Then, the T test cases follow.
The first line of each test case contains a single integer, ‘N’.
Output Format:
For each test case, return a single integer, denoting the total number of required permutations. Since, the answer may be very large, print the answer modulo 10^9 + 7.
Note:
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N < 100
Time Limit: 1 sec
1
3
2
There are only two different permutations of the numbers from 1 to 3, in which the prime numbers are present at prime indices. These two permutations are:
1, 2, 3
1, 3, 2
There are only two prime numbers in the range of 1 to 3. These two numbers are 2 and 3. So the prime indices will be 2 and 3(1-indexed). We can observe in all the above-mentioned permutations, that at index 2 and 3, there are only prime numbers. There does not exist any other permutation, where all this condition is satisfied. Hence, we print 2 as our answer.
1
4
4
There are only four different permutations of the numbers from 1 to 4, in which the prime numbers are present at prime indices. These four permutations are:
1, 2, 3, 4
1, 3, 2, 4
4, 2, 3, 1
4, 3, 2, 1
There are only two prime numbers in the range of 1 to 4. These two numbers are 2 and 3. So the prime indices will be 2 and 3(1-indexed). We can observe in all the above-mentioned permutations, that at index 2 and 3, there are only prime numbers. There does not exist any other permutation, where all this condition is satisfied. Hence, we print 4 as our answer.
Count all prime numbers in the given range
Approach:
The approach is to find out the total number of prime numbers in the range, 1 to ‘N’. Let this number be ‘X’. So, the total number of prime indices will also be ‘X’ and the total number of different ways in which the prime numbers can be placed at prime indices will be ‘X!’. The total number of composite numbers will be ‘N-X’. So, the total number of different ways in which the composite numbers can be placed at prime indices will be ‘(N-X)!’.
Steps:
O(N * sqrt(N) ), where 'N' is the upper bound of the range in which numbers are to be considered.
We are going through all the numbers in the range 1 to N, one by one, and checking if the number is prime or not. This checking of a single number takes O(sqrt(N)) time. So, the overall time complexity will become O(N * sqrt(N) ).
O(1).
We are not using any extra space.