Created
May 4, 2017 21:20
-
-
Save tikipatel/df230b1a6e3e5005993e8adc2de50e4c to your computer and use it in GitHub Desktop.
Numerical calculation of Apéry's Constant
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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