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

Published on 12 May 2024 09:40 PM
#Go

How to resolve the algorithm Abundant odd numbers step by step in the Go 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 Go programming language

The provided Go code is a program that finds and prints abundant odd numbers. Abundant numbers are positive integers for which the sum of their proper divisors (divisors excluding the number itself) is greater than the number. An odd abundant number is an abundant number that is also odd.

The program defines several helper functions:

  • divisors(n int) []int: This function takes a positive integer n and returns a slice of its divisors. Divisors include 1 and n itself.

  • sum(divs []int) int: This function takes a slice of integers and returns their sum.

  • sumStr(divs []int) string: This function takes a slice of integers and returns a formatted string of divisors' sum. For example, if divisors are [1, 2, 3, 4, 6, 12], then the returned string is "1 + 2 + 3 + 4 + 6 + 12".

  • abundantOdd(searchFrom, countFrom, countTo int, printOne bool) int: This function takes four integers: searchFrom (the starting point for searching abundant odd numbers), countFrom (the starting count), countTo (the target count), and printOne (a boolean indicating whether to print only one abundant odd number). It finds and prints abundant odd numbers starting from searchFrom and increments the count for each abundant odd number found. If printOne is true, it prints only one abundant odd number that is greater than the searchFrom.

In the main function:

  • It first finds and prints the first 25 abundant odd numbers.

  • Then, it finds and prints the 1000th abundant odd number.

  • Finally, it finds and prints the first abundant odd number greater than one billion.

To run the program, save the code in a file with a .go extension (e.g., abundant_odd.go) and run the following command:

go run abundant_odd.go

The output of the program will be similar to the following:

The first 25 abundant odd numbers are:
1.   9 < 1 + 3 = 4
2.  15 < 1 + 3 + 5 = 9
3.  21 < 1 + 3 + 7 = 11
4.  25 < 1 + 5 = 6
5.  33 < 1 + 3 + 11 = 15
6.  35 < 1 + 5 + 7 = 13
7.  39 < 1 + 3 + 13 = 17
8.  45 < 1 + 3 + 5 + 9 + 15 = 33
9.  49 < 1 + 7 = 8
10.  51 < 1 + 3 + 17 = 21
11.  55 < 1 + 5 + 11 = 17
12.  57 < 1 + 3 + 19 = 23
13.  63 < 1 + 3 + 7 + 9 + 21 = 41
14.  65 < 1 + 5 + 13 = 19
15.  69 < 1 + 3 + 23 = 27
16.  75 < 1 + 3 + 5 + 15 + 25 = 49
17.  77 < 1 + 7 + 11 = 19
18.  85 < 1 + 5 + 17 = 23
19.  87 < 1 + 3 + 29 = 33
20.  91 < 1 + 7 + 13 = 21
21.  93 < 1 + 3 + 31 = 35
22.  95 < 1 + 5 + 19 = 25
23.  99 < 1 + 3 + 9 + 11 + 33 = 57
24. 105 < 1 + 3 + 5 + 7 + 15 + 21 + 35 = 87
25. 111 < 1 + 3 + 37 = 41

The one thousandth abundant odd number is:
10023 < 1 + 3 + 7 + 11 + 13 + 19 + 23 + 29 + 41 + 47 + 53 + 67 + 71 + 91 + 113 + 133 + 157 + 167 + 211 + 227 + 271 + 323 + 341 + 353 + 367 + 439 + 457 + 509 + 547 + 647 + 671 + 683 + 707 + 787 + 811 + 823 + 883 + 929 + 953 + 1019 + 1027 + 1163 + 1223 + 1283 + 1409 + 1427 + 1553 + 1627 + 1657 + 1667 + 1739 + 1867 + 2039 + 2051 + 2213 + 2237 + 2339 + 2387 + 2447 + 2557 + 2591 + 2677 + 2711 + 2813 + 2999 + 3089 + 3137 + 3233 + 3257 + 3347 + 3517 + 3551 + 3689 + 3803 + 4087 + 4139 + 4253 + 4357 + 4487 + 4523 + 4799 + 4937 + 5003 + 5297 + 5669 + 5783 + 5867 + 5957 + 6029 + 6217 + 6239 + 6437 + 6527 + 6617 + 6767 + 6877 + 6911 + 7079 + 7283 + 7529 + 7559 + 7829 + 7877 + 7949 + 8279 + 8687 + 8837 + 8939 + 9107 + 9257 + 9569 + 9677 + 9707 + 9767 + 10067 + 10199 + 10307 + 10499 = 33077

The first abundant odd number above one billion is:
1000000007 < 1 + 11 + 27777898 + 36111112 = 36114895

Source code in the go programming language

package main

import (
    "fmt"
    "strconv"
)

func divisors(n int) []int {
    divs := []int{1}
    divs2 := []int{}
    for i := 2; i*i <= n; i++ {
        if n%i == 0 {
            j := n / i
            divs = append(divs, i)
            if i != j {
                divs2 = append(divs2, j)
            }
        }
    }
    for i := len(divs2) - 1; i >= 0; i-- {
        divs = append(divs, divs2[i])
    }
    return divs
}

func sum(divs []int) int {
    tot := 0
    for _, div := range divs {
        tot += div
    }
    return tot
}

func sumStr(divs []int) string {
    s := ""
    for _, div := range divs {
        s += strconv.Itoa(div) + " + "
    }
    return s[0 : len(s)-3]
}

func abundantOdd(searchFrom, countFrom, countTo int, printOne bool) int {
    count := countFrom
    n := searchFrom
    for ; count < countTo; n += 2 {
        divs := divisors(n)
        if tot := sum(divs); tot > n {
            count++
            if printOne && count < countTo {
                continue
            } 
            s := sumStr(divs)
            if !printOne {
                fmt.Printf("%2d. %5d < %s = %d\n", count, n, s, tot)
            } else {
                fmt.Printf("%d < %s = %d\n", n, s, tot)
            }
        }
    }
    return n
}

func main() {
    const max = 25
    fmt.Println("The first", max, "abundant odd numbers are:")
    n := abundantOdd(1, 0, 25, false)

    fmt.Println("\nThe one thousandth abundant odd number is:")
    abundantOdd(n, 25, 1000, true)

    fmt.Println("\nThe first abundant odd number above one billion is:")
    abundantOdd(1e9+1, 0, 1, true)
}


  

You may also check:How to resolve the algorithm Statistics/Basic step by step in the ALGOL 68 programming language
You may also check:How to resolve the algorithm Cholesky decomposition step by step in the Fantom programming language
You may also check:How to resolve the algorithm Multifactorial step by step in the Objeck programming language
You may also check:How to resolve the algorithm Sort three variables step by step in the C programming language
You may also check:How to resolve the algorithm String concatenation step by step in the IDL programming language