Add tasks for all the new languages

This commit is contained in:
Tina Müller 2016-12-05 23:44:36 +01:00
parent 9dc3c2bb62
commit bba7bfd280
13208 changed files with 134745 additions and 0 deletions

View file

@ -0,0 +1,36 @@
// Iterative
func gcd(var a: Int, var b: Int) -> Int {
a = abs(a); b = abs(b)
if (b > a) { swap(&a, &b) }
while (b > 0) { (a, b) = (b, a % b) }
return a
}
// Recursive
func gcdr (var a: Int, var b: Int) -> Int {
a = abs(a); b = abs(b)
if (b > a) { swap(&a, &b) }
return gcd_rec(a,b)
}
private func gcd_rec(a: Int, b: Int) -> Int {
return b == 0 ? a : gcd_rec(b, a % b)
}
for (a,b) in [(1,1), (100, -10), (10, -100), (-36, -17), (27, 18), (30, -42)] {
println("Iterative: GCD of \(a) and \(b) is \(gcd(a, b))")
println("Recursive: GCD of \(a) and \(b) is \(gcdr(a, b))")
}