How to resolve the algorithm Abundant odd numbers step by step in the Julia programming language
How to resolve the algorithm Abundant odd numbers step by step in the Julia 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 Julia programming language
The provided Julia code deals with abundant numbers, which are positive integers for which the sum of their proper divisors (the factors excluding the number itself) exceeds the number itself. The code defines functions for finding proper divisors, checking if a number is abundant, finding abundant odd numbers, and printing the results.
Function propfact(n):
- This function calculates the proper divisors of an integer
n
and returns them in a sorted vector. - It uses the
factor
function to obtain the prime factorization ofn
, then constructs a vector of proper divisors by raising each prime factor to powers from 1 to its exponent. - It finally sorts the vector of proper divisors in ascending order.
isabundant(n):
- This function checks if a given integer
n
is abundant by summing its proper divisors (using thepropfact
function) and comparing it ton
. - It returns
true
if the sum of proper divisors is greater thann
, andfalse
otherwise.
prettyprintfactors(n):
- This function takes an integer
n
and prints information about its proper divisors. - It calls the
propfact
function to get the proper divisors, calculates their sum, and then prints a formatted string containing this information.
oddabundantsfrom(startingint, needed, nprint):
- This function finds and prints abundant odd numbers starting from a given
startingint
, and continues until it findsneeded
such numbers. - It increments the integer
n
by 2 in each iteration to consider only odd numbers. - It checks if
n
is abundant usingisabundant
and increments a countercount
if it is. - If
nprint
is greater than 0, it prints the factors ofn
at the specified count (i.e., thenprint
-th abundant odd number found). - The function continues until
count
reachesneeded
.
Usage in the Code:
- The code first prints the first 25 abundant odd numbers starting from 2.
- Then, it prints the 1000th abundant odd number.
- Finally, it prints the first abundant odd number greater than one billion.
Source code in the julia programming language
using Primes
function propfact(n)
f = [one(n)]
for (p, x) in factor(n)
f = reduce(vcat, [f*p^i for i in 1:x], init=f)
end
pop!(f)
sort(f)
end
isabundant(n) = sum(propfact(n)) > n
prettyprintfactors(n) = (a = propfact(n); println("$n has proper divisors $a, these sum to $(sum(a))."))
function oddabundantsfrom(startingint, needed, nprint=0)
n = isodd(startingint) ? startingint : startingint + 1
count = one(n)
while count <= needed
if isabundant(n)
if nprint == 0
prettyprintfactors(n)
elseif nprint == count
prettyprintfactors(n)
end
count += 1
end
n += 2
end
end
println("First 25 abundant odd numbers:")
oddabundantsfrom(2, 25)
println("The thousandth abundant odd number:")
oddabundantsfrom(2, 1001, 1000)
println("The first abundant odd number greater than one billion:")
oddabundantsfrom(1000000000, 1)
You may also check:How to resolve the algorithm Special variables step by step in the BBC BASIC programming language
You may also check:How to resolve the algorithm Walk a directory/Non-recursively step by step in the Racket programming language
You may also check:How to resolve the algorithm Left factorials step by step in the Seed7 programming language
You may also check:How to resolve the algorithm Evaluate binomial coefficients step by step in the Oz programming language
You may also check:How to resolve the algorithm Paraffins step by step in the Perl programming language