Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,54 @@
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)")
}
}
}

View file

@ -0,0 +1,44 @@
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)