How to resolve the algorithm Abundant odd numbers step by step in the C programming language

Published on 7 June 2024 03:52 AM
#C

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:

  1. Function sum_proper_divisors:

    • Input: Takes an unsigned integer n.
    • Purpose: Calculates the sum of proper divisors of n for odd numbers only. It uses a loop with an increment of 2 to consider only odd numbers.
    • Implementation:
      • Initializes sum to 1, which accounts for the divisor 1.
      • Iterates from 3 to the square root of n with an increment of 2.
      • Checks if n is divisible by i (using n % i == 0).
      • If divisible, adds i to sum.
      • If i is equal to j, which is the quotient of n divided by i, it means that i and j are not different divisors (e.g., 4 and 2), so only one of them is added to the sum.
    • Returns the calculated sum of proper divisors.
  2. Main Function:

    • Input: Accepts two command-line arguments (unused in this code).
    • Purpose: Finds and prints abundant odd numbers.
    • Implementation:
      • Initializes n to 1 and c to 0.
      • Enters a loop that continues until c reaches 25.
      • Inside the loop:
        • Increments n by 2 to consider only odd numbers.
        • Checks if n is less than the sum of its proper divisors (using n < sum_proper_divisors(n)).
        • If n is abundant, prints n and increments c.
      • 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.
  3. 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