2023-09-06 Program, Algorithm Recursion
2023-09-06 Program, Algorithm Recursion
• Homework: 35%
• Midterm: 30%
• Final: 40%
2
Data Type
• The programming language ‘C’ was developed in the early
1970s by Dennis Ritchie at Bell Laboratories
– C was initially developed for writing system software
• Data type determines the set of values that a data item can
take and the operations that can be performed on the item
– Note that C does not provide any data type for storing text
• Text is made up of individual characters
• “char” is supposed to store characters not numbers?
In the memory, characters are stored in their ASCII codes
‘A’ has the ASCII code of 65 3
ASCII Codes
• In C language
– ‘5’ == int 53
– ‘5’-’0’==int 53 – int 48 == int 5
– If char c = ‘B’+32, then c==‘b’
4
Program
• A program contains one or more functions,
where a function is defined as a group of
statements that perform a well-defined task
– A program should undoubtedly give correct
results, but along with that it should also run
efficiently
5
image source: https://www.bilibili.com/video/BV1Po4y1m7DG/
Algorithm.
• The typical definition of algorithm is “a formally defined
procedure for performing some calculation”
– In general terms, an algorithm provides a blueprint to write a
program to solve a particular problem
– A program does not have to satisfy the fourth condition
(明確性)
(有限性)
(有效性:明確且可行) 6
Algorithm..
• A complex algorithm is often divided into smaller units called
modules
– This process of dividing an algorithm into modules is called
modularization (模組化)
7
Algorithm & Program
8
Function
• Let us analyze the reasons why segmenting a program into
manageable chunks is an important aspect of programming
– Dividing the program into separate well-defined functions
facilitates each function to be written and tested separately
9
Recursion Function.
• The recursion mechanisms are extremely powerful, because
they often can express a complex process very clearly
– Indirect Recursion
• The function may call other functions that again invoke the calling
function
– Tail Recursion
• The function may call itself at the end of the function
• A special case of direct recursion
10
Recursion Function..
Direct Recursion Indirect Recursion Tail Recursion
calling cycle
11
Recursion Function…
• Let’s make a comparison
Recursion Non-Recursion
Time-consuming Time-saving
12
Examples – 1
• Please write down a recursive program to do factorial
0! = 1
1! = 1
2! = 1 × 2
3! = 1 × 2 × 3
𝑛𝑛! = 1 × 2 × 3 × ⋯ × 𝑛𝑛
13
Examples – 2.
• Please (1) write a recursive program, 𝐹𝐹𝐹𝐹𝐹𝐹( 𝑖𝑖𝑖𝑖𝑖𝑖 𝑎𝑎 ), to calculate
the Fibonacci number; (2) how many function calls do you
need when we want to calculate 𝐹𝐹𝐹𝐹𝐹𝐹(5)?
14
Examples – 2..
• Please write a recursive program, 𝐹𝐹𝐹𝐹𝐹𝐹( 𝑖𝑖𝑖𝑖𝑖𝑖 𝑎𝑎 ), to calculate
the Fibonacci number; (2) how many function calls do you
need to do when we want to calculate 𝐹𝐹𝐹𝐹𝐹𝐹(5)?
0, 𝑖𝑖𝑖𝑖 𝑎𝑎 = 0
𝐹𝐹𝐹𝐹𝐹𝐹(𝑎𝑎) = � 1, 𝑖𝑖𝑖𝑖 𝑎𝑎 = 1
𝐹𝐹𝐹𝐹𝐹𝐹 𝑎𝑎 − 1 + 𝐹𝐹𝐹𝐹𝐹𝐹 𝑎𝑎 − 2 , 𝑜𝑜𝑜𝑜𝑜𝑜𝑜𝑜𝑜𝑜𝑜𝑜𝑜𝑜𝑜𝑜𝑜
15
Examples – 2…
• Please (1) write a recursive program, 𝐹𝐹𝐹𝐹𝐹𝐹( 𝑖𝑖𝑖𝑖𝑖𝑖 𝑎𝑎 ), to calculate
the Fibonacci number; (2) how many function calls do you
need to do when we want to calculate 𝐹𝐹𝐹𝐹𝐹𝐹(5)?
𝐹𝐹𝐹𝐹𝐹𝐹(1) 𝐹𝐹𝐹𝐹𝐹𝐹(0)
𝐹𝐹𝐹𝐹𝐹𝐹(2) 𝐹𝐹𝐹𝐹𝐹𝐹(1)
𝐹𝐹𝐹𝐹𝐹𝐹(0)
𝐹𝐹𝐹𝐹𝐹𝐹(1) 𝐹𝐹𝐹𝐹𝐹𝐹(0)
16
Examples – 3.
• Given an Ackerman’s function 𝐴𝐴(𝑚𝑚, 𝑛𝑛), please calculate
𝐴𝐴(1,2).
𝑛𝑛 + 1, 𝑖𝑖𝑖𝑖 𝑚𝑚 = 0
𝐴𝐴 𝑚𝑚, 𝑛𝑛 = � 𝐴𝐴 𝑚𝑚 − 1,1 , 𝑖𝑖𝑖𝑖 𝑛𝑛 = 0
𝐴𝐴 𝑚𝑚 − 1, 𝐴𝐴 𝑚𝑚, 𝑛𝑛 − 1 , 𝑜𝑜𝑜𝑜𝑜𝑜𝑜𝑜𝑜𝑜𝑜𝑜𝑜𝑜𝑜𝑜𝑜
𝐴𝐴 1,2 = 𝐴𝐴 0, 𝐴𝐴 1,1
𝐴𝐴 1,1 = 𝐴𝐴 0, 𝐴𝐴 1,0
𝐴𝐴 1,0 = 𝐴𝐴 0,1
𝐴𝐴 0,1 = 2
17
Examples – 3..
• Given an Ackerman’s function 𝐴𝐴(𝑚𝑚, 𝑛𝑛), please calculate
𝐴𝐴(1,2).
𝑛𝑛 + 1, 𝑖𝑖𝑖𝑖 𝑚𝑚 = 0
𝐴𝐴 𝑚𝑚, 𝑛𝑛 = � 𝐴𝐴 𝑚𝑚 − 1,1 , 𝑖𝑖𝑖𝑖 𝑛𝑛 = 0
𝐴𝐴 𝑚𝑚 − 1, 𝐴𝐴 𝑚𝑚, 𝑛𝑛 − 1 , 𝑜𝑜𝑜𝑜𝑜𝑜𝑜𝑜𝑜𝑜𝑜𝑜𝑜𝑜𝑜𝑜𝑜
𝐴𝐴 1,0 = 𝐴𝐴 0,1 = 2
𝐴𝐴 0,1 = 2
18
Summary
• Algorithm + Data Structure = Program
19
Questions?
Thank you
for your
listening!
20