-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContents.swift
45 lines (33 loc) · 1.11 KB
/
Contents.swift
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
import UIKit
/*
Fibonacci Sequence. The Fibonacci Sequence is the series of numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ... The next number is found by adding up the two numbers before it.
*/
//test code area start
let numberOfStep = 10
//test code area end
func fibonacciSequenceNumberOf(steps : Int) ->[Int]{
var sequence = [0,1]
if steps == 0 {
return []
}
if steps <= 1{
return sequence
}
for _ in 0..<steps{
let first = sequence[sequence.count - 2]
let last = sequence.last!
sequence.append(first + last)
}
return sequence
}
func recursionForFibonacciSequenceNumberOf(_ steps : Int,first : Int ,second:Int) -> [Int]{
if steps == 0{
return []
}
return [first + second] + recursionForFibonacciSequenceNumberOf(steps - 1, first: second, second: first+second)
}
print("######Fibonacci Sequence Number Of Steps######")
print(fibonacciSequenceNumberOf(steps: numberOfStep))
print("######Recursion For Fibonacci Sequence Number Of Steps######")
print(recursionForFibonacciSequenceNumberOf(numberOfStep,first: 0,second: 1)
)