How to resolve the algorithm Abundant odd numbers step by step in the C programming language
How to resolve the algorithm Abundant odd numbers step by step in the C programming language
Table of Contents
Problem Statement
An Abundant number is a number n for which the sum of divisors σ(n) > 2n, or, equivalently, the sum of proper divisors (or aliquot sum) s(n) > n.
12 is abundant, it has the proper divisors 1,2,3,4 & 6 which sum to 16 ( > 12 or n); or alternately, has the sigma sum of 1,2,3,4,6 & 12 which sum to 28 ( > 24 or 2n).
Abundant numbers are common, though even abundant numbers seem to be much more common than odd abundant numbers. To make things more interesting, this task is specifically about finding odd abundant numbers.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Abundant odd numbers step by step in the C programming language
The provided C code finds and prints abundant odd numbers. An abundant number is a positive integer for which the sum of its proper divisors (the positive divisors of the number, excluding the number itself) is greater than the number itself.
Here's a breakdown of the code:
-
Function
sum_proper_divisors:- Input: Takes an unsigned integer
n. - Purpose: Calculates the sum of proper divisors of
nfor odd numbers only. It uses a loop with an increment of 2 to consider only odd numbers. - Implementation:
- Initializes
sumto 1, which accounts for the divisor 1. - Iterates from 3 to the square root of
nwith an increment of 2. - Checks if
nis divisible byi(usingn % i == 0). - If divisible, adds
itosum. - If
iis equal toj, which is the quotient ofndivided byi, it means thatiandjare not different divisors (e.g., 4 and 2), so only one of them is added to the sum.
- Initializes
- Returns the calculated sum of proper divisors.
- Input: Takes an unsigned integer
-
Main Function:
- Input: Accepts two command-line arguments (unused in this code).
- Purpose: Finds and prints abundant odd numbers.
- Implementation:
- Initializes
nto 1 andcto 0. - Enters a loop that continues until
creaches 25. - Inside the loop:
- Increments
nby 2 to consider only odd numbers. - Checks if
nis less than the sum of its proper divisors (usingn < sum_proper_divisors(n)). - If
nis abundant, printsnand incrementsc.
- Increments
- After printing the first 25 abundant odd numbers, the loop continues without printing to find the 1000th abundant odd number.
- Finally, the loop breaks when the first abundant odd number above one billion is found.
- Initializes
-
Output:
- The program prints the first 25 abundant odd numbers.
- It then prints the 1000th abundant odd number.
- Finally, it prints the first abundant odd number above one billion.
Source code in the c programming language
#include <stdio.h>
#include <math.h>
// The following function is for odd numbers ONLY
// Please use "for (unsigned i = 2, j; i*i <= n; i ++)" for even and odd numbers
unsigned sum_proper_divisors(const unsigned n) {
unsigned sum = 1;
for (unsigned i = 3, j; i < sqrt(n)+1; i += 2) if (n % i == 0) sum += i + (i == (j = n / i) ? 0 : j);
return sum;
}
int main(int argc, char const *argv[]) {
unsigned n, c;
for (n = 1, c = 0; c < 25; n += 2) if (n < sum_proper_divisors(n)) printf("%u: %u\n", ++c, n);
for ( ; c < 1000; n += 2) if (n < sum_proper_divisors(n)) c ++;
printf("\nThe one thousandth abundant odd number is: %u\n", n);
for (n = 1000000001 ;; n += 2) if (n < sum_proper_divisors(n)) break;
printf("The first abundant odd number above one billion is: %u\n", n);
return 0;
}
You may also check:How to resolve the algorithm Draw a clock step by step in the C programming language
You may also check:How to resolve the algorithm Mind boggling card trick step by step in the C programming language
You may also check:How to resolve the algorithm Count in octal step by step in the C programming language
You may also check:How to resolve the algorithm Primorial numbers step by step in the C programming language
You may also check:How to resolve the algorithm Multiple regression step by step in the C programming language