How to resolve the algorithm Proper divisors step by step in the Go programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Proper divisors step by step in the Go programming language
Table of Contents
Problem Statement
The proper divisors of a positive integer N are those numbers, other than N itself, that divide N without remainder. For N > 1 they will always include 1, but for N == 1 there are no proper divisors.
The proper divisors of 6 are 1, 2, and 3. The proper divisors of 100 are 1, 2, 4, 5, 10, 20, 25, and 50.
Show all output here.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Proper divisors step by step in the Go programming language
Explanation:
This Go program is designed to find and count the proper divisors of numbers up to a specified limit. Here's a detailed breakdown:
1. listProperDivisors
Function:
- It takes an integer
limit
as input. - Iterates through numbers from 1 to
limit
. - For each number
i
, it printsi
and its proper divisors. Proper divisors are divisors ofi
that are less thani
. - If
i
is 1 (not a proper divisor), it indicates that it has no proper divisors.
2. countProperDivisors
Function:
- It takes an integer
n
as input. - Counts the number of proper divisors of
n
and returns the count. - It iterates through numbers from 1 to half of
n
(inclusive), checking if each one divides evenly inton
.
3. main
Function:
-
Finding Proper Divisors:
- Calls the
listProperDivisors
function with a limit of 10 to print the proper divisors of numbers up to 10.
- Calls the
-
Finding Numbers with the Most Proper Divisors:
- Initializes variables to keep track of the maximum number of proper divisors (
maxCount
) and the numbers with the maximum proper divisors (most
). - Iterates through numbers from 2 to 20,000.
- For each number
n
, it uses thecountProperDivisors
function to count its proper divisors. - If the count matches the current maximum, it adds
n
to the list of numbers with the maximum count. - If the count is greater than the maximum, it updates the maximum count and replaces the list of numbers with a single entry for
n
.
- Initializes variables to keep track of the maximum number of proper divisors (
-
Printing the Results:
- Prints the numbers with the maximum number of proper divisors and the maximum count.
- Iterates through and prints each number in the
most
list, which contains the numbers with the maximum proper divisors.
Source code in the go programming language
package main
import (
"fmt"
"strconv"
)
func listProperDivisors(limit int) {
if limit < 1 {
return
}
width := len(strconv.Itoa(limit))
for i := 1; i <= limit; i++ {
fmt.Printf("%*d -> ", width, i)
if i == 1 {
fmt.Println("(None)")
continue
}
for j := 1; j <= i/2; j++ {
if i%j == 0 {
fmt.Printf(" %d", j)
}
}
fmt.Println()
}
}
func countProperDivisors(n int) int {
if n < 2 {
return 0
}
count := 0
for i := 1; i <= n/2; i++ {
if n%i == 0 {
count++
}
}
return count
}
func main() {
fmt.Println("The proper divisors of the following numbers are :\n")
listProperDivisors(10)
fmt.Println()
maxCount := 0
most := []int{1}
for n := 2; n <= 20000; n++ {
count := countProperDivisors(n)
if count == maxCount {
most = append(most, n)
} else if count > maxCount {
maxCount = count
most = most[0:1]
most[0] = n
}
}
fmt.Print("The following number(s) <= 20000 have the most proper divisors, ")
fmt.Println("namely", maxCount, "\b\n")
for _, n := range most {
fmt.Println(n)
}
}
You may also check:How to resolve the algorithm Compile-time calculation step by step in the AppleScript programming language
You may also check:How to resolve the algorithm Roots of unity step by step in the J programming language
You may also check:How to resolve the algorithm Split a character string based on change of character step by step in the Standard ML programming language
You may also check:How to resolve the algorithm Execute HQ9+ step by step in the Factor programming language
You may also check:How to resolve the algorithm File extension is in extensions list step by step in the Go programming language