Files
004_comission/ttng1234/Q2/step2.py
louiscklaw 49e275d85d update,
2025-01-31 20:12:48 +08:00

54 lines
2.1 KiB
Python

#!/usr/bin/env python
test_list = [ 21,9,17,10 ]
def fibonacci_sequence(test_number):
# test_number represent the number that sequence should stop at.
# Initialize the sequence with the first two numbers
sequence = [0, 1]
# keep_find setup to let while loop keep find the number sequence
# unless the last generated number (next_num) is greater than the number
# need to be tested.
keep_find = True
# initialize to true to run the while loop at the very first execution
while keep_find:
# after the very first execution, the keep_find may not keep true anymore.
# 1st, 2nd, 3rd, 4th
# 0, 1, 1, 2
# at the first execution, the next_num is the 3rd number
# at the first execution, the sequence[-1] is the 2rd number
# at the first execution, the sequence[-2] is the 1st number
# at the second execution, the next_num is the 4th number
# at the second execution, the sequence[-1] is the 3rd number
# at the second execution, the sequence[-2] is the 2nd number
next_num = sequence[-1] + sequence[-2]
# append the number to the sequence
# append means extend the list to with the value given
# [0,1].append(2) => [0,1,2]
# at the first execution the number appended is at 3rd place
# at the second execution the number appended is at 4th place
sequence.append(next_num)
# test if:
# the last number(next_num) calculated is already larger than the number need to be tested
keep_find = (next_num < test_number)
# test the number if it exist in fibonacci sequence
# return true if the next_num is equal to the test_number
# it is because there are two possibilities of next_num
# next_num > test_number => means the number is not in sequence
# next_num = test_number => means the number is in sequence
return next_num == test_number
def check_mathematical_series(n: int) -> str:
print(fibonacci_sequence(n))
for n in test_list:
21, 9, 17, 10
check_mathematical_series(n)