-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathViewController.swift
More file actions
62 lines (51 loc) · 1.46 KB
/
ViewController.swift
File metadata and controls
62 lines (51 loc) · 1.46 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
//
// ViewController.swift
// Recursion
//
// Created by Alex Paul on 1/23/20.
// Copyright © 2020 Alex Paul. All rights reserved.
//
import UIKit
// use breakpoints in the function bodies to see the call stack as functions are being called and placed on the stack
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func performOperations(_ sender: UIButton) {
//recurse(7)
//countDownToZero(from: 20)
let factorialResult = factorial(4)
print(factorialResult)
}
// example 1
func recurse(_ n: Int) {
guard n > 0 else {
return
} // base case
print("hi")
recurse(n - 1) // recursive call - 6, 5, 4, 3, 2, 1,
}
// example 2
func countDownToZero(from num: Int) {
guard num >= 0 else { return }// base case
print(num) // 20, 19....
countDownToZero(from: num - 1) // recursive call
// countDownToZero(19)
// countDownToZero(18)
// countDownToZero(0)
}
// example - factorial
// formula for finding factorial is n * (n - 1)
// factorial is used to find the number of permutations of a given is this case number
func factorial(_ n: Int) -> Int {
guard n > 1 else {
return 1
} // base case
return n * factorial(n - 1) // recursive call
// 4 * factorial(3) -> 24
// 3 * factorial(2) -> 6
// 2 * factorial(1) -> 2
// factorial(1) -> 1
}
}