Skip to content

Instantly share code, notes, and snippets.

@tikipatel
Created May 4, 2017 21:20
Show Gist options
  • Save tikipatel/df230b1a6e3e5005993e8adc2de50e4c to your computer and use it in GitHub Desktop.
Save tikipatel/df230b1a6e3e5005993e8adc2de50e4c to your computer and use it in GitHub Desktop.
Numerical calculation of Apéry's Constant
import Foundation
func gcd(_ m: Int, _ n: Int) -> Int {
var a = 0
var b = max(m, n)
var r = min(m, n)
while r != 0 {
a = b
b = r
r = a % b
}
return b
}
let totalEntries: Int = 1_000_000
var totalCoprimes: Int = 0
for _ in 0..<totalEntries {
let a: Int = Int(arc4random())
let b: Int = Int(arc4random())
let c: Int = Int(arc4random())
let greatestCommonDivisor = gcd(a, gcd(b, c))
if greatestCommonDivisor == 1 {
totalCoprimes += 1
}
}
print(Double(totalEntries) / Double(totalCoprimes))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment