How to resolve the algorithm Amicable pairs step by step in the Swift programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Amicable pairs step by step in the Swift 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 Swift programming language
Source code in the swift programming language
import func Darwin.sqrt
func sqrt(x:Int) -> Int { return Int(sqrt(Double(x))) }
func properDivs(n: Int) -> [Int] {
if n == 1 { return [] }
var result = [Int]()
for div in filter (1...sqrt(n), { n % $0 == 0 }) {
result.append(div)
if n/div != div && n/div != n { result.append(n/div) }
}
return sorted(result)
}
func sumDivs(n:Int) -> Int {
struct Cache { static var sum = [Int:Int]() }
if let sum = Cache.sum[n] { return sum }
let sum = properDivs(n).reduce(0) { $0 + $1 }
Cache.sum[n] = sum
return sum
}
func amicable(n:Int, m:Int) -> Bool {
if n == m { return false }
if sumDivs(n) != m || sumDivs(m) != n { return false }
return true
}
var pairs = [(Int, Int)]()
for n in 1 ..< 20_000 {
for m in n+1 ... 20_000 {
if amicable(n, m) {
pairs.append(n, m)
println("\(n, m)")
}
}
}
import func Darwin.sqrt
func sqrt(x:Int) -> Int { return Int(sqrt(Double(x))) }
func sigma(n: Int) -> Int {
if n == 1 { return 0 } // definition of aliquot sum
var result = 1
let root = sqrt(n)
for var div = 2; div <= root; ++div {
if n % div == 0 {
result += div + n/div
}
}
if root*root == n { result -= root }
return (result)
}
func amicables (upTo: Int) -> () {
var aliquot = Array(count: upTo+1, repeatedValue: 0)
for i in 1 ... upTo { // fill lookup array
aliquot[i] = sigma(i)
}
for i in 1 ... upTo {
let a = aliquot[i]
if a > upTo {continue} //second part of pair out-of-bounds
if a == i {continue} //skip perfect numbers
if i == aliquot[a] {
print("\(i, a)")
aliquot[a] = upTo+1 //prevent second display of pair
}
}
}
amicables(20_000)
You may also check:How to resolve the algorithm Even or odd step by step in the Tcl programming language
You may also check:How to resolve the algorithm Keyboard input/Obtain a Y or N response step by step in the C++ programming language
You may also check:How to resolve the algorithm Self-describing numbers step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm Delete a file step by step in the Tcl programming language
You may also check:How to resolve the algorithm Sorting algorithms/Heapsort step by step in the Tcl programming language