-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy pathDeficient Numbers.cpp
More file actions
47 lines (42 loc) · 1 KB
/
Deficient Numbers.cpp
File metadata and controls
47 lines (42 loc) · 1 KB
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
// C++ program to implement an Optimized Solution
// to check Deficient Number
#include <bits/stdc++.h>
using namespace std;
// Function to calculate sum of divisors
int divisorsSum(int n)
{
int sum = 0; // Initialize sum of prime factors
// Note that this loop runs till square root of n
for (int i = 1; i <= sqrt(n); i++) {
if (n % i == 0) {
// If divisors are equal, take only one
// of them
if (n / i == i) {
sum = sum + i;
}
else // Otherwise take both
{
sum = sum + i;
sum = sum + (n / i);
}
}
}
return sum;
}
// Function to check Deficient Number
bool isDeficient(int n)
{
// Check if sum(n) < 2 * n
return (divisorsSum(n) < (2 * n));
}
/* Driver program to test above function */
int main()
{ int n ;
cin >> n;
for (int i = 0; i< n; i++){
if (isDeficient(i)){
cout << i << ' ';
}
}
return 0;
}