A prime number is a number that has only two factors, 1 and the number itself. Ninja came across a problem in which he has a number ‘N’, and Ninja has to find whether ‘N’ can be formed with the sum of ‘K’ prime numbers.
The first line of the input contains a single integer 'T', representing the number of test cases.
The first line of each test case contains 2 space-separated integers, representing the integers ‘N’ and ‘K’.
Output Format :
For each test case, print the 1, if the number can be formed with the sum of ‘K’ prime numbers, else print 0.
Print the output of each test case in a separate line.
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, K <= 3*10^3
Time Limit: 1 sec
2
10 2
2 2
1
0
For the first test case :
10 can be formed by sum 5 two times.
For the second test :
We have no prime number smaller than 2 and thus 2 can not be formed by the sum of 2 prime numbers.
2
10 3
7 3
1
1
Try to find all the possibilities of forming a sum.
The basic idea is to check for all the possible combinations of forming ‘N’ by the sum of ‘K’ prime numbers.
Here is the algorithm :
isPrime(N)
O(N^(N*sqrt(N))), where ‘N’ is the given number.
We run a loop from 2 to ‘N’ and for each ‘N’ we check whether that number is prime or not which takes sqrt of time. We call a recursive function to count the prime numbers in range 1 to ‘N’ which can not be more than ‘N’. Therefore, the overall time complexity will be O(N^(N*sqrt(N))).
O(N), where ‘N’ is the given number.
The recursion stack can contain the count of prime numbers in range 1 to ‘N’ which cannot be more than ‘N’. Therefore, the overall space complexity will be O(N).