recursion ii factorial iterative

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


  1. Recursion II

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

  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

  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

  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

  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

  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

  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

  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

  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

  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

  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

  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 ]

  14. Factorial (recursive) long long fact(n) { if (n == 1) return 1; else return fact(n-1) * n; }

  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; }

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

  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')

  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')

  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")

  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")

  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")

  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")

  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")

  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 ]

  25. Let's look at the C++ solution

  26. What does this do? void weird( int n) { if (n == 0) return ; else { cout << n << endl; weird(n - 1); } }

  27. Tower of Hanoi

Recommend


More recommend