iT邦幫忙

2024 iThome 鐵人賽

DAY 14
0

What is recursion?

Recursion is a process where a function calls itself in order to solve a problem, typically with a base case to stop the function from calling itself indefinitely.

所謂的遞迴,就是函數在未滿某些條件下持續呼叫函數本身以解決問題,當達成基本條件則會停止,以阻止無限呼叫自己

Featurs of recursion

Here's some featurs of recursion

  • Recursion is generally slower than iteration due to function call overhead and increase memory usage, so it is not ideal for replacing simple loops.
  • Recursion is well-suited for problems that can be broken into smaller, similar subproblems or chunks.
  • Recursion excels at solving problems involving multiple branching paths, such as traversing trees or exploring decision trees.

遞迴的特色

  • 遞迴耗用的記憶體量遠多於 simple loops,所以不要用遞迴來取代 simple loops
  • 適合處理可以細分為更小且與主問題類似的問題
  • 適合用在在探索或處理『多個分支』的問題

So..what is decision tree?

A decision tree is a tree-like model used for decision-making, particularly useful in complex or uncertain situations.

It split data into branches on feature values, leading to outcomes or decisions at the leaf nodes.
This model is powerful for both classification and regression tasks in machine learning.

Here's a simple decision tree for example:

Calculate Factorial 計算出階層

Write a function to calculate factorial of the number.
寫個 function 計算出 n!

calculate 8!
所謂的 8! 就是 8x7x6x5x...x1

提示:使用 recursion


Answer:

function getFactorial(number){
    if(number===1){
        return 1;
    }
    return number*getFactorial(number-1);
}

getFactorial(8); // 40320

相關資源

Master Recursion: One-Branch & Two-Branch Techniques
https://youtu.be/LmZGI60c31g?si=ThUXGzcyAp64QNRc
regression
https://web.ntnu.edu.tw/~algo/Regression.html


上一篇
Coding Practice: Get The Max Sum Of Continuous Element-day13
下一篇
Coding Practice:Fibonacci sequence & Honai Tower-day15
系列文
演算法與資料結構入門:30天基礎學習之旅30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言