Given a natural number 'N', return the sum of all proper divisors of ‘N’.
‘X’ is a proper divisor of ‘Y’ if X < Y and Y % X = 0.
The first line contains a single integer ‘T’ denoting the number of test cases . Then each test case follows.
The first line of each testcase contains a single integer ‘N’.
Output Format :
For each test case print a single integer denoting the sum of all proper divisors of ‘N’.
Output for each test case will be printed in a separate line.
Note :
You are not required to print anything; it has already been taken care of. Just implement the function.
1 <= T <= 10
1 <= N <= 10^9
Time limit: 1 sec
2
6
1
6
0
For test case 1 :
1,2,3 are proper divisors of N=6. Therefore the sum is equal to 1+2+3 = 6
For test case 2 :
No proper divisor of N=1 exists, hence the sum is equal to 0.
2
5
10
1
8
Proper divisors of N are smaller than N, try a linear search!
For the given N, simply iterate numbers from 1 to N-1 and if the number is divisible by N, then simply add it to the final answer.
The steps are as follows:
O( N ), where ‘N’ is the natural number given in input.
Since we iterate over N - 1 numbers, Hence the time complexity is O(N)
O(1)
Since constant space used. Hence the space complexity is O(1).