-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathSum_of_divisors.cpp
65 lines (56 loc) · 1.33 KB
/
Sum_of_divisors.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
Sum of all the possible divisors of a natural number
You will be given a natural number and your task is
to find all possible divisors of the given number and
return their sum as the output.
*/
#include <bits/stdc++.h>
using namespace std;
//function to sum up all the possible divisors of a natural number
int divisorSum(int number)
{
int ans = 0;
for(int pass = 2; pass <= sqrt(number); pass++)
{
//for i is a divisor then remainder must be 0
if(number % pass == 0)
{
//if the divisors are same we count only one of
//them in sum
if(pass == (number / pass))
{
ans = ans + pass;
}
//otherwise we count both for sum
else
{
ans = ans + (pass + (number / pass));
}
}
}
return (ans + 1);
}
//driver code
int main()
{
int number;
cout << "Enter a natural number: ";
cin >> number;
cout << "Sum of all possible divisors of " << number << " is: " << divisorSum(number);
return 0;
}
/*
EXAMPLE:-
Example 1:-
Input--
Enter a natural number: 40
Output--
Sum of all possible divisors of 40 is: 90
Example 2:-
Input--
Enter a natural number: 1
Output--
Sum of all possible divisors of 1 is: 1
TIME COMPLEXITY--> O(sqrt(N))
SPACE COMPLEXITY--> O(1)
*/