How to resolve the algorithm Abundant odd numbers step by step in the V (Vlang) programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Abundant odd numbers step by step in the V (Vlang) 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 V (Vlang) programming language
Source code in the v programming language
fn divisors(n i64) []i64 {
mut divs := [i64(1)]
mut divs2 := []i64{}
for i := 2; i*i <= n; i++ {
if n%i == 0 {
j := n / i
divs << i
if i != j {
divs2 << j
}
}
}
for i := divs2.len - 1; i >= 0; i-- {
divs << divs2[i]
}
return divs
}
fn sum(divs []i64) i64 {
mut tot := i64(0)
for div in divs {
tot += div
}
return tot
}
fn sum_str(divs []i64) string {
mut s := ""
for div in divs {
s += "${u8(div)} + "
}
return s[0..s.len-3]
}
fn abundant_odd(search_from i64, count_from int, count_to int, print_one bool) i64 {
mut count := count_from
mut n := search_from
for ; count < count_to; n += 2 {
divs := divisors(n)
tot := sum(divs)
if tot > n {
count++
if print_one && count < count_to {
continue
}
s := sum_str(divs)
if !print_one {
println("${count:2}. ${n:5} < $s = $tot")
} else {
println("$n < $s = $tot")
}
}
}
return n
}
const max = 25
fn main() {
println("The first $max abundant odd numbers are:")
n := abundant_odd(1, 0, 25, false)
println("\nThe one thousandth abundant odd number is:")
abundant_odd(n, 25, 1000, true)
println("\nThe first abundant odd number above one billion is:")
abundant_odd(1_000_000_001, 0, 1, true)
}
You may also check:How to resolve the algorithm Random number generator (included) step by step in the Seed7 programming language
You may also check:How to resolve the algorithm Sort an outline at every level step by step in the Julia programming language
You may also check:How to resolve the algorithm Soundex step by step in the Picat programming language
You may also check:How to resolve the algorithm Anagrams/Deranged anagrams step by step in the FutureBasic programming language
You may also check:How to resolve the algorithm Zero to the zero power step by step in the Plain English programming language