Recursion II Factorial (iterative) long long fact(int n) { long - - PowerPoint PPT Presentation

recursion ii factorial iterative
SMART_READER_LITE
LIVE PREVIEW

Recursion II Factorial (iterative) long long fact(int n) { long - - PowerPoint PPT Presentation

Recursion II Factorial (iterative) long long fact(int n) { long long answer = 1; for (int x = 1; x <= n; x++) answer *= n; return answer; } Factorial fact(1) = 1 fact(2) = 1 * 2 fact(3) = 1 * 2 * 3 fact(4) = 1 * 2 * 3 * 4


slide-1
SLIDE 1

Recursion II

slide-2
SLIDE 2

Factorial (iterative)

long long fact(int n) { long long answer = 1; for (int x = 1; x <= n; x++) answer *= n; return answer; }

slide-3
SLIDE 3

Factorial

  • fact(1) = 1
  • fact(2) = 1 * 2
  • fact(3) = 1 * 2 * 3
  • fact(4) = 1 * 2 * 3 * 4
  • fact(5) = 1 * 2 * 3 * 4 * 5
slide-4
SLIDE 4
  • Let's look at this problem a different way:
  • fact(1) = 1
  • fact(2) = 1 * 2
  • fact(3) = 1 * 2 * 3
  • fact(4) = 1 * 2 * 3 * 4
  • fact(5) = 1 * 2 * 3 * 4 * 5
slide-5
SLIDE 5
  • Let's look at this problem a different way:
  • fact(1) = 1
  • fact(2) = 1 * 2
  • fact(3) = 1 * 2 * 3
  • fact(4) = 1 * 2 * 3 * 4
  • fact(5) = fact(4) * 5
slide-6
SLIDE 6
  • Let's look at this problem a different way:
  • fact(1) = 1
  • fact(2) = 1 * 2
  • fact(3) = 1 * 2 * 3
  • fact(4) = 1 * 2 * 3 * 4
  • fact(5) = fact(4) * 5
slide-7
SLIDE 7
  • Let's look at this problem a different way:
  • fact(1) = 1
  • fact(2) = 1 * 2
  • fact(3) = 1 * 2 * 3
  • fact(4) = fact(3) * 4
  • fact(5) = fact(4) * 5
slide-8
SLIDE 8
  • Let's look at this problem a different way:
  • fact(1) = 1
  • fact(2) = 1 * 2
  • fact(3) = 1 * 2 * 3
  • fact(4) = fact(3) * 4
  • fact(5) = fact(4) * 5
slide-9
SLIDE 9
  • Let's look at this problem a different way:
  • fact(1) = 1
  • fact(2) = 1 * 2
  • fact(3) = fact(2) * 3
  • fact(4) = fact(3) * 4
  • fact(5) = fact(4) * 5
slide-10
SLIDE 10
  • Let's look at this problem a different way:
  • fact(1) = 1
  • fact(2) = 1 * 2
  • fact(3) = fact(2) * 3
  • fact(4) = fact(3) * 4
  • fact(5) = fact(4) * 5
slide-11
SLIDE 11
  • Let's look at this problem a different way:
  • fact(1) = 1
  • fact(2) = fact(1) * 2
  • fact(3) = fact(2) * 3
  • fact(4) = fact(3) * 4
  • fact(5) = fact(4) * 5
slide-12
SLIDE 12
  • Let's look at this problem a different way:
  • fact(1) = 1
  • fact(2) = fact(1) * 2
  • fact(3) = fact(2) * 3
  • fact(4) = fact(3) * 4
  • fact(5) = fact(4) * 5
slide-13
SLIDE 13
  • Let's look at this problem a different way:
  • fact(1) = 1
  • fact(2) = fact(1) * 2
  • fact(3) = fact(2) * 3
  • fact(4) = fact(3) * 4
  • fact(5) = fact(4) * 5
  • General formula:
  • fact(n) = 1

[ for n = 1 ]

  • fact(n) = fact(n-1) * n [ for n > 1 ]
slide-14
SLIDE 14

Factorial (recursive)

long long fact(n) { if (n == 1) return 1; else return fact(n-1) * n; }

slide-15
SLIDE 15

Uppercase (iterative)

string uc(string s) { string answer = “”; for (int x = 0; x < s.size(); x++) answer += toupper(s); return answer; }

long long fact(int n) { long long answer = 1; for (int x = 1; x <= n; x++) answer *= n; return answer; }

slide-16
SLIDE 16

Uppercase

uc("d") = "D" uc("cd") = "CD" uc("bcd") = "BCD" uc("abcd") = "ABCD"

slide-17
SLIDE 17

Uppercase

(toup stands for toupper) uc("d") = toup('d') uc("cd") = toup('c') + toup('d') uc("bcd") = toup('b') + toup('c') + toup('d') uc("abcd") = toup('a') + toup('b') + toup('c') + toup('d')

slide-18
SLIDE 18

Uppercase

(toup stands for toupper) uc("d") = toup('d') uc("cd") = toup('c') + toup('d') uc("bcd") = toup('b') + toup('c') + toup('d') uc("abcd") = toup('a') + toup('b') + toup('c') + toup('d')

slide-19
SLIDE 19

Uppercase

(toup stands for toupper) uc("d") = toup('d') uc("cd") = toup('c') + toup('d') uc("bcd") = toup('b') + toup('c') + toup('d') uc("abcd") = toup('a') + uc("bcd")

slide-20
SLIDE 20

Uppercase

(toup stands for toupper) uc("d") = toup('d') uc("cd") = toup('c') + toup('d') uc("bcd") = toup('b') + toup('c') + toup('d') uc("abcd") = toup('a') + uc("bcd")

slide-21
SLIDE 21

Uppercase

(toup stands for toupper) uc("d") = toup('d') uc("cd") = toup('c') + toup('d') uc("bcd") = toup('b') + uc("cd") uc("abcd") = toup('a') + uc("bcd")

slide-22
SLIDE 22

Uppercase

(toup stands for toupper) uc("d") = toup('d') uc("cd") = toup('c') + toup('d') uc("bcd") = toup('b') + uc("cd") uc("abcd") = toup('a') + uc("bcd")

slide-23
SLIDE 23

Uppercase

(toup stands for toupper) uc("d") = toup('d') uc("cd") = toup('c') + uc("d") uc("bcd") = toup('b') + uc("cd") uc("abcd") = toup('a') + uc("bcd")

slide-24
SLIDE 24

Uppercase

(toup stands for toupper) uc("d") = toup('d') uc("cd") = toup('c') + uc("d") uc("bcd") = toup('b') + uc("cd") uc("abcd") = toup('a') + uc("bcd") General rule: uc(s) = toup(s) [ if s is one letter long ] uc(s) = toup(s[0]) + uc(rest of s) [ otherwise ]

slide-25
SLIDE 25

Let's look at the C++ solution

slide-26
SLIDE 26

What does this do?

void weird(int n) { if (n == 0) return; else { cout << n << endl; weird(n - 1); } }

slide-27
SLIDE 27

Tower of Hanoi