Last active
October 5, 2015 12:53
-
-
Save proxpero/0a49f893e607f6f91725 to your computer and use it in GitHub Desktop.
Solution to problem 3 from "5 Programming Problems, 1 Hour"
This file contains hidden or 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
//: Problem 3 | |
// "Write a function that computes the list of the first 100 Fibonacci numbers. By definition, the first two numbers in the Fibonacci sequence are 0 and 1, and each subsequent number is the sum of the previous two. As an example, here are the first 10 Fibonnaci numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, and 34." | |
// https://www.shiftedup.com/2015/05/07/five-programming-problems-every-software-engineer-should-be-able-to-solve-in-less-than-1-hour | |
// Xcode 7.0, Swift 2.0 | |
func fibonacci(count: Int) -> [Double] { | |
var n1: Double = 0 | |
var n2: Double = 1 | |
var list = [n1, n2] | |
while list.count < count { | |
let next = n1 + n2 | |
list.append(next) | |
n1 = n2 | |
n2 = next | |
} | |
return list | |
} | |
let test: Array<Double> = [0.0, 1.0, 1.0, 2.0, 3.0, 5.0, 8.0, 13.0, 21.0, 34.0] | |
assert(test == fibonacci(10)) | |
print(fibonacci(100)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment