-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.c
76 lines (54 loc) · 1.17 KB
/
util.c
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
#include "util.h"
int isPrime(const unsigned long n){
for(unsigned long i=2; i<=n/2; i++)
if(!(n%i))
return 0;
return 1;
}
int isPrimeInt(const int n){
if(n<=0)
return 0;
for(int i=2; i<=n/2; i++)
if(!(n%i))
return 0;
return 1;
}
void nextPrime(unsigned long* n){
if((*n)==2)
(*n)++;
else{
do{
(*n)+=2;
} while(!isPrime(*n));
}
}
int numDivisors(const unsigned long n){
int count;
if(n!=1){
count = 0;
double end = sqrt(n);
for(unsigned long i=1; i<end; i++)
if(!(n%i))
count+=2;
int end_ = (int) end;
if(end_*end_ == n)
count++;
}
else
count = 1;
return count;
}
int sumPropDivisors(const int n){
int sum = 0;
if(n!=1){
double end = sqrt(n);
for(int i=1; i<end; i++)
if(!(n%i))
sum += i + (n/i);
int end_ = (int) end;
if(end_*end_ == n)
sum += end_;
sum -= n;
}
return sum;
}