Skip to content

Instantly share code, notes, and snippets.

@tim-peterson
Created October 28, 2014 00:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tim-peterson/84851cad8b91804a24e1 to your computer and use it in GitHub Desktop.
Save tim-peterson/84851cad8b91804a24e1 to your computer and use it in GitHub Desktop.
Fibonacci sequence.playground Tim Peterson
// Thinkful Playground
// Thinkful.com
// Fibonacci Sequence
// By definition, the first two numbers in the Fibonacci sequence are 1 and 1, or 0 and 1, depending on the chosen starting point of the sequence, and each subsequent number is the sum of the previous two.
import UIKit
class FibonacciSequence {
let includesZero: Bool
let values: [Int]
init(maxNumber: Int, includesZero: Bool) {
self.includesZero = includesZero
if includesZero==true{
var initValue=1
}
else{
var initValue=0
}
var arr = [initValue]
for i in initValue...maxNumber {
var nextValue=initValue+i
arr.append(nextValue)
}
self.values = arr
//TODO: Create an array which contains the numbers in the Fibonacci sequence, but don't add any numbers to the array which exceed the maxNumber. For example, if the maxNumber is 10 then the array should contain [0,1,1,2,3,5,8] because the next number is 13 which is higher than the maxNumber. If includesZero is false then you should not include the number 0 in the sequence.
}
init(numberOfItemsInSequence: Int, includesZero: Bool) {
self.includesZero = includesZero
var initValue: Array
if includesZero==true{
var initValue=1
}
else{
var initValue=0
}
var arr = [initValue]
for i in 1...numberOfItemsInSequence {
var nextValue=initValue+i
arr.append(nextValue)
}
self.values = arr
//TODO: Create an array which contains the numbers in the Fibonacci sequence, and the array should contain this many items: numberOfItemsInSequence. For example, if numberOfItemsInSequence is 10 then the array should contain [0,1,1,2,3,5,8,13,21,34] if inlcudesZero is true, or [1,1,2,3,5,8,13,21,34,55] if includesZero is false.
}
}
let fibanocciSequence = FibonacciSequence(maxNumber:12345, includesZero: true)
let anotherSequence = FibonacciSequence(numberOfItemsInSequence: 13, includesZero: true)
fibanocciSequence
anotherSequence
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment