SHARE
TWEET

Untitled

a guest Mar 22nd, 2019 82 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. m   n   sum i   Proses
  2. 2   5   -   -   -
  3. 2   5   0   -   sum = 0
  4. 2   5   0   0   i = 0
  5. 2   5   0   2   masuk loop, i = 2; i <= 5; i++
  6. 2   5   4   2   sum = sum + i * i = 0 + 2 * 2
  7. 2   5   4   3   i++
  8.                 apakah i <= 5? true
  9. 2   5   4   3   masuk loop, i = 3; i <= 5; i++
  10. 2   5   13  3   sum = sum + i * i = 4 + 3 * 3
  11. 2   5   13  4   i++
  12.                 apakah i <= 5? true
  13. 2   5   13  4   masuk loop, i = 4; i <= 5; i++
  14. 2   5   29  4   sum = sum + i * i = 13 + 4 * 4
  15. 2   5   29  5   i++
  16.                 apakah i <= 5? true
  17. 2   5   29  5   masuk loop, i = 5; i <= 5; i++
  18. 2   5   54  5   sum = sum + i * i = 29 + 5 * 5
  19. 2   5   54  6   i++
  20.                 apakah i <= 5? false
  21. sum = 54
  22.  
  23. function contohRekursif(m, n) {
  24.     if(m < n) {
  25.         return (m * m + contohRekursif(m + 1, n));
  26.     } else {
  27.         return (m * m);
  28.     }
  29. }
  30.  
  31.  
  32. m = 2, n = 5
  33.  
  34. contohRekursif(2, 5)
  35. m < n   2 < 5   (2 * 2 + contohRekursif(2 + 1, 5)); (4 + contohRekursif(3, 5));
  36.  
  37. contohRekursif(3, 5)
  38. m < n   3 < 5   (3 * 3 + contohRekursif( 3 + 1, 5)); (9 + contohRekursif(4, 5));
  39.  
  40. contohRekursif(4, 5)
  41. m < n   4 < 5   (4 * 4 + contohRekursif(4 + 1, 5)); (16 + contohRekursif(5, 5));
  42.  
  43. contohRekursif(5, 5)
  44. m < n   5 < 5   (5 * 5) = 25; return 25;
  45.  
  46. contohRekursif(4, 5)
  47. (16 + 25) = 41; return 41;
  48.  
  49. contohRekursif(3, 5)
  50. (9 + 41) = 50; return 50;
  51.  
  52. contohRekursif(2, 5)
  53. (4 + 50) = 54; return 54;
  54.  
  55.  
  56. 2, 5
  57.  
  58. 4 + contohRekursif(3,5);
  59. 4 + 9 + contohRekursif(4,5);
  60. 4 + 9 + 16 + contohRekursif (5,5);
  61. 4 + 9 + 16 + 25; <== balikin (5, 5), masuk ke (4, 5)
  62. 4 + 9 + 41; <== balikin (4, 5), masuk (3, 5)
  63. 4 + 50; <== balikin (3, 5), masuk (2, 5)
  64. 54; <== balikin hasil akhir
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top