How to resolve the algorithm Amicable pairs step by step in the Go programming language
How to resolve the algorithm Amicable pairs step by step in the Go programming language
Table of Contents
Problem Statement
Two integers
N
{\displaystyle N}
and
M
{\displaystyle M}
are said to be amicable pairs if
N ≠ M
{\displaystyle N\neq M}
and the sum of the proper divisors of
N
{\displaystyle N}
(
s u m
(
p r o p D i v s
( N ) )
{\displaystyle \mathrm {sum} (\mathrm {propDivs} (N))}
)
= M
{\displaystyle =M}
as well as
s u m
(
p r o p D i v s
( M ) )
N
{\displaystyle \mathrm {sum} (\mathrm {propDivs} (M))=N}
.
1184 and 1210 are an amicable pair, with proper divisors:
Calculate and show here the Amicable pairs below 20,000; (there are eight).
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Amicable pairs step by step in the Go programming language
The provided Go program finds and prints amicable pairs below 20,000. Here's a detailed explanation:
-
amicable pairs: Two numbers are called amicable pairs if the sum of the proper divisors (positive divisors excluding the number itself) of one number is equal to the other number, and vice versa. For example, (220, 284) is an amicable pair because the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55, and 110, and they sum up to 284. Similarly, the proper divisors of 284 are 1, 2, 4, 71, and 142, and they sum up to 220.
-
pfacSum function: This function takes an integer
i
as input and returns the sum of its proper divisors. It iterates from1
toi/2
and checks ifp
dividesi
. If it does, it addsp
to the sum. This effectively sums up the proper divisors ofi
. -
main function:
- Array Initialization: The
main
function initializes an arraya
of size 20,000. This array will store the sum of proper divisors for numbers from 1 to 19,999. - Populating the Array: It uses a loop to populate the array
a
with the sum of proper divisors for each number from 1 to 19,999. ThepfacSum
function is used to calculate the sum of proper divisors. - Printing Amicable Pairs: After populating the array, the program enters another loop to search for amicable pairs. It iterates from 2 to 19,999 (excluding 1). For each number
n
, it retrieves the sum of proper divisors from the arraya
. If this summ
is greater thann
(to avoid printing the same pair twice), less than 20,000 (to stay within the array bounds), and equal ton
's sum of proper divisors stored ina[m]
, then(n, m)
is an amicable pair. The program then prints these amicable pairs.
- Array Initialization: The
In summary, this program efficiently finds and prints amicable pairs below 20,000. It uses an array to store the sum of proper divisors for each number from 1 to 19,999 to quickly determine amicable pairs.
Source code in the go programming language
package main
import "fmt"
func pfacSum(i int) int {
sum := 0
for p := 1; p <= i/2; p++ {
if i%p == 0 {
sum += p
}
}
return sum
}
func main() {
var a[20000]int
for i := 1; i < 20000; i++ {
a[i] = pfacSum(i)
}
fmt.Println("The amicable pairs below 20,000 are:")
for n := 2; n < 19999; n++ {
m := a[n]
if m > n && m < 20000 && n == a[m] {
fmt.Printf(" %5d and %5d\n", n, m)
}
}
}
You may also check:How to resolve the algorithm Munchausen numbers step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Empty program step by step in the Egel programming language
You may also check:How to resolve the algorithm Add a variable to a class instance at runtime step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm Exceptions/Catch an exception thrown in a nested call step by step in the Seed7 programming language
You may also check:How to resolve the algorithm MD4 step by step in the Kotlin programming language