-
Notifications
You must be signed in to change notification settings - Fork 1
/
Sum Of Divisors.cpp
78 lines (70 loc) · 1.25 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
66
67
68
69
70
71
72
73
74
75
76
77
78
#include<stdio.h>
#include<math.h>
using namespace std;
#define MAX 100000
int flag[MAX];
int prime[MAX];
int prime_no;
void seive()
{
int i,j;
for(i=0;i<MAX;i++) flag[i] = 1;
int lim = sqrt(MAX)+1;
for(i=2;i<=lim;i++)
{
if(flag[i]==1)
{
for(j=i+i;j<MAX;j+=i)
{
flag[j] = 0;
}
}
}
prime_no = 0;
for(i=2;i<MAX;i++)
{
if(flag[i]==1)
{
prime[prime_no] = i;
prime_no++;
}
}
}
int sum_of_divisor(int N)
{
int i,val,count,sum,p,s;
val = sqrt(N)+1;
sum = 1;
for(i=0;prime[i]<val;i++)
{
if(N%prime[i]==0)
{
p=1;
while(N%prime[i]==0)
{
N/=prime[i];
p=p*prime[i];
}
p=p*prime[i];
s=(p-1)/(prime[i]-1);
sum=sum*s;
}
}
if(N>1)
{
p=N*N;
s=(p-1)/(N-1);
sum=sum*s;
}
return sum;
}
int main()
{
seive();
int n;
while(scanf("%d",&n)==1)
{
printf("Sum of divisors of %d is %d\n",n,sum_of_divisor(n));
}
return 0;
}