3.4 KiB
Q2. 25%, Checking mathematical series number
Fibonacci Sequence
Write a Python function named check_mathematical_series
that takes a positive integer n as input and checks whether the given number belongs to any of the following important mathematical series: Fibonacci sequence
, Triangular sequence
, Square sequence
.
The function should determine the series type and return a string indicating the result.
The possible return values are:
- "Fibonacci" if the number belongs to the Fibonacci sequence.
- "Triangular" if the number belongs to the Triangular sequence.
- "Square" if the number belongs to the Square sequence.
- "None" if the number does not belong to any of the above sequences.
For this question, consider the following definitions for the series:
- Fibonacci sequence: A series of numbers in which each number (after the first two) is the sum of the two preceding ones. The sequence starts with 0 and 1. For example, the Fibonacci sequence begins as follows: 0, 1, 1, 2, 3, 5, 8, 13, 21, …
def fibonacci_sequence(n):
# Check if the input is valid
if n <= 0:
return []
elif n == 1:
return [0]
elif n == 2:
return [0, 1]
# Initialize the sequence with the first two numbers
sequence = [0, 1]
# Generate the Fibonacci sequence
for i in range(2, n):
next_num = sequence[-1] + sequence[-2]
sequence.append(next_num)
return sequence
- Triangular sequence: A sequence of numbers in which each term represents the total number of dots required to form a triangle with that many dots on each side. The nth term of the triangular sequence is given by the formula (n * (n + 1)) / 2. For example, the triangular sequence begins as follows: 1, 3, 6, 10, 15, …
def triangular_sequence(n):
# Check if the input is valid
if n <= 0:
return []
# Initialize the sequence with the first number
sequence = [1]
# Generate the Triangular sequence
for i in range(2, n+1):
next_num = sequence[-1] + i
sequence.append(next_num)
return sequence
Square number sequence: A sequence of numbers that are the squares of consecutive integers. For example, the square number sequence begins as follows: 1, 4, 9, 16, 25, 36, ... Write the check_mathematical_series function and test it with the following values of n: - 21 - 9 - 17 - 10
def square_number_sequence(n):
# Check if the input is valid
if n <= 0:
return []
# Initialize the sequence with the first number
sequence = [1]
# Generate the Triangular sequence
for i in range(2, n+1):
next_num = sequence[-1] + i
sequence.append(next_num)
return sequence
Print the result for each value of n, indicating the series types to which it belongs.
Using commas (,) if it belongs to more than 1 series.
"None" if it doesn't belong to any of the series.
Your function should have the following signature:
def check_mathematical_series(n: int) -> str:
Your code here
Example output:
n = 21: Fibonacci, Triangular
n = 9: Square
n = 17: None
n = 10: Triangular
Note: You can assume that the given n will be a positive integer
Q: 有冇話係邊到學python最好? 我睇完data camp都唔識 🙃🙃 覺得自己好蠢。
A: 你方唔方便比份 notes 我望一望? 同埋你係咪理科生?