How to resolve the algorithm Abundant odd numbers step by step in the Processing programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Abundant odd numbers step by step in the Processing 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 Processing programming language
Source code in the processing programming language
void setup() {
println("First 25 abundant odd numbers: ");
int abundant = 0;
int i = 1;
while (abundant < 25) {
int sigma_sum = sigma(i);
if (sigma_sum > 2 * i) {
abundant++;
println(i + " Sigma sum: " + sigma_sum);
}
i += 2;
}
println("Thousandth abundant odd number: ");
while (abundant < 1000) {
int sigma_sum = sigma(i);
if (sigma_sum > 2 * i) {
abundant++;
if (abundant == 1000) {
println(i + " Sigma sum: " + sigma_sum);
}
}
i += 2;
}
println("First abundant odd number greater than 10^9: ");
i = int(pow(10, 9)) + 1;
while (!(sigma(i) > 2 * i)) {
i += 2;
}
println(i + " Sigma sum: " + sigma(i));
}
int sigma(int n) {
int sum = 0;
for (int i = 1; i < sqrt(n); i++) {
if (n % i == 0) {
sum += i + n / i;
}
}
if (sqrt(n) % 1 == 0) {
sum += sqrt(n);
}
return sum;
}
You may also check:How to resolve the algorithm Sockets step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm Sorting algorithms/Bogosort step by step in the Clojure programming language
You may also check:How to resolve the algorithm Multi-base primes step by step in the Factor programming language
You may also check:How to resolve the algorithm Sierpinski carpet step by step in the C programming language
You may also check:How to resolve the algorithm The sieve of Sundaram step by step in the AppleScript programming language