---
# System prepended metadata

title: Python Exercise 5
tags: [python2021]

---

# Python Exercise 5
###### tags: `python2021`

繳交格式：
以學號命名的zip壓縮檔

程式作業 (1~4) 取名為：**ex5-1.py, ex5-2.py, ex5-3.py**…，不符合者視為缺交。

---

# The Fibonacci sequence

The Fibonacci sequence is a mathematic sequence that is often associated with geometry and art. The name Fibonacci comes from a thought experiment proposed by a medieval mathematician named Leonardo Bonacci (1170-1240). This thought experiment resolves around the population of a group of rabbit that:

* Start with 1 pair of new-born rabbits.
* The rabbits will reach sexual maturity (性成熟) at the 3rd month after birth.
* Once reach sexual maturity, each pair of rabbits will produce 1 pair of rabbits every month.
* The rabbits are immortal (they will never die).

The number of rabbits by from 1st to 8th month (1st to 8th Fibonacci numbers) are shown below:



| Month                     | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
| --------                  | - | - | - | - | - | - | - | - |
| Number of Rabbit(in pair) | 1 | 1 | 2 | 3 | 5 | 8 | 13| 21|


## Exercise 5-1
Define a function that takes an input *n* and returns the *n*th Fibonacci number. Use the defined function to calculate the 10th, 50th, and 100th Fibonacci number and print them in the terminal.

Result:
![](https://drive.google.com/uc?export=view&id=1C4cBiIHOQF_bvuxSfnVPVttC6im0Tad8)

:::info
**Note**
To simplify the implementation, you may:
* manually define 1st and 2nd Fibonacci number in your function.
* assume *n* will always be greater than 3.
:::

## Exercise 5-2
Draw equilateral right triangles (等腰直角三角形) with the edge length of **8th**, **9th**, and **10th** Fibonacci number. Save the drawn triangles to ‘Fib.txt’.
The ‘Fib.txt’ should look something similar to the following graph (equilateral right triangles with edge length of 3rd, 4th, and 5th Fibonacci number):

![](https://drive.google.com/uc?export=view&id=1pJLFboCPzzL6eSJhA373F80RmXIhBi6y)

:::info
**Note**
* The triangles may point upward or downward. Both directions would count.
* You may reuse the function defined in Exercise 5-1 if you wish.

:::
:::success
**Hint**: Try to draw triangles with smaller number first, then move on to bigger number.
:::
