You are given an integer N. Your task is to find the sum of the total number of factors from 1 to N modulo 2. A number X is called a factor of K if K mod X is 0 (the remainder when K is divided by X is 0).
Example:-N=3
Answer:- 1 ( 1 has 1 factor , 2 has 2 factors and 3 has 2 factors , so total 5 and 5 % 2 = 1).
The first line contains a single integer ‘T’ representing the number of test cases. Then each test case follows.
The first line of each test case contains an integer ‘N’.
Output Format :
For each test case, return the total number of factors of all the numbers from 1 to ‘N’ mod 2.
Note :
You are not required to print anything, it has already been taken care of. Just implement the function.
1 <= T <= 10^3
1 <= N <= 10^9
Time Limit: 1 sec
2
3
4
1
0
In the first test case, 1 has 1 factor, 2 has 2 factors and 3 has 2 factors, so total of 5 factors and 5 % 2 = 1
In the second test case, 1 has 1 factor, 2 has 2 factors and 3 has 2 factors, 4 has 3 factors, so a total of 8 and 8 % 2 = 0
1
10
1
Calculate the number of factors of all the numbers from 1 to N.
Find the factors of the numbers from 1 to N and sum them up and return the sum mod 2.
Algorithm:-
O(N*sqrt(N)), where N is the given input
We are iterating over every N numbers, and for every number, we are iterating sqrt(N) times, so the time complexity is O(N*sqrtN)).
O(1)
We are using constant space here, so the space complexity is O(1).