-
Notifications
You must be signed in to change notification settings - Fork 118
/
abundantNums.c
133 lines (106 loc) · 3.59 KB
/
abundantNums.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <stdio.h>
/**
* printAbundantNumbers()
* Calculates and prints all abundant numbers between a start and stop value.
*
* printDeficientNumbers()
* Calculates and prints all deficient numbers between a start and stop value.
*
* returnIfAbundant()
* Will return an int 1 (true) if a passed number is abundant and an int 0
* (false) if a passed number is not abundant.
*
* A number is abundant if sum(m) > 2m, where sum(m) is the summation of all
* of m's divisors. A deficient is opposite of an abundant number:
* sum(m) < 2m
*/
// Functions declarations
//
// Get numbers from user functions
void getStartStop(int*, int*);
void getCheckNum(int*);
// Abundant number functions
void printAbundantNumbers(int, int);
int returnIfAbundant();
// Deficient number functions
void printDeficientNumbers(int, int);
int main() {
// Initialize variables for print function
int start, stop;
// Function to get user's range of numbers
getStartStop(&start, &stop);
// Prints abundant numbers in the range start - stop
printAbundantNumbers(start, stop);
// Prints deficient numbers in the range start - stop
printDeficientNumbers(start, stop);
// Initialize variable for Abundance check
int checkAbundance;
// Function to get user's number
getCheckNum(&checkAbundance);
// Returns 1 or 0 based on abundance
int isAbundant = returnIfAbundant(checkAbundance);
// Prints a 1 if the passed number is abundant
printf("%d\n", isAbundant);
return 0;
}
/**
* This will print all abundant numbers between
* start and stop. A number is abundant if
* sum(m) > 2m, where sum(m) is the summation of all
* the divisors of m.
*/
void printAbundantNumbers(int startNum, int stopNum) {
while (startNum <= stopNum) {
int divisors = 0;
for (int i = 1; i < startNum + 1; ++i) // Iterate all possible divisors.
divisors += (startNum % i == 0) ? i : 0; // Checks if i is a divisor and sums it.
if (divisors > (2 * startNum)) // Prints if abundance is met.
printf("%d is abundant.\n", startNum);
++startNum;
}
}
/**
* Returns a 1 if the passed num is abundant.
* Returns a 0 if the passed num is not abundant.
*/
int returnIfAbundant(int num) {
int divisors = 0;
for (int i = 1; i < num + 1; ++i) // Iterate all possible divisors.
divisors += (num % i == 0) ? i : 0; // Checks if i is a divisor and sums it.
return divisors > (2 * num);
}
/**
* This will print all deficient numbers between
* start and stop. A number is deficient if
* sum(m) < 2m, where sum(m) is the summation of all
* the divisors of m. (Note: It's the opposite of an
* abundant number)
*/
void printDeficientNumbers(int startNum, int stopNum) {
while (startNum <= stopNum) {
int divisors = 0;
for (int i = 1; i < startNum + 1; ++i) // Iterate all possible divisors.
divisors += (startNum % i == 0) ? i : 0; // Checks if i is a divisor and sums it.
if (divisors < (2 * startNum)) // Prints if deficiency is met.
printf("%d is deficient.\n", startNum);
++startNum;
}
}
/**
* Prompts user for the starting and stoping number
* to check for abundance in a range of numbers
*/
void getStartStop(int* pStart, int* pStop) {
printf("Enter your starting number: ");
scanf("%d", &*pStart);
printf("Enter your stopping number: ");
scanf("%d", &*pStop);
}
/**
* Prompts the user to enter a number they want
* checked for abundance
*/
void getCheckNum(int* pCheckNum) {
printf("Enter the number to check for abundance: ");
scanf("%d", &*pCheckNum);
}