-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignment
More file actions
69 lines (47 loc) · 2.25 KB
/
Copy pathAssignment
File metadata and controls
69 lines (47 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// 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
var i = 2
var nextFib = 0
if includesZero == true {
self.values = [0,1]
}
else {
self.values = [1,1]
}
while nextFib < maxNumber {
nextFib = self.values[i-2] + self.values[i-1]
if nextFib <= maxNumber {
self.values.append(nextFib)
}
i=i+1
}
//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 i = 2
var nextFib = 0
if includesZero == true {
self.values = [0,1]
}
else {
self.values = [1,1]
}
while i < numberOfItemsInSequence {
nextFib = self.values[i-2] + self.values[i-1]
self.values.append(nextFib)
i=i+1
}
//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:10, includesZero: false)
let anotherSequence = FibonacciSequence(numberOfItemsInSequence: 10, includesZero: false)