Added 2 new generator fucntions for the Tribonacci Series (#418)

* Added 2 new generator fucntions for the Tribonacci Series

* Order computer_science.py alphabetically

---------

Co-authored-by: Luke Weiler <lukew25073@gmail.com>
This commit is contained in:
ganand2021
2023-06-02 10:25:23 -04:00
committed by GitHub
parent 33865cb9de
commit c18a873046
2 changed files with 46 additions and 0 deletions

View File

@@ -129,4 +129,6 @@ gen_list = [
("line_equation_from_2_points", "algebra"), ("line_equation_from_2_points", "algebra"),
("orthogonal_projection", "algebra"), ("orthogonal_projection", "algebra"),
("area_of_trapezoid", "geometry"), ("area_of_trapezoid", "geometry"),
("tribonacci_series", "computer_science"),
("nth_tribonacci_number", "computer_science"),
] ]

View File

@@ -223,3 +223,47 @@ def nth_fibonacci_number(max_n=100):
(math.pow(gratio, n) - math.pow(-gratio, -n)) / (math.sqrt(5))) (math.pow(gratio, n) - math.pow(-gratio, -n)) / (math.sqrt(5)))
return problem, f'${solution}$' return problem, f'${solution}$'
def nth_tribonacci_number(min_length=1, max_length=80):
r"""nth Tribonacci number
| Ex. Problem | Ex. Solution |
| --- | --- |
| What is the 14th Tribonacci number? | $504$ |
"""
tribDict = {0:0, 1:0, 2:1}
def recTrib(i):
if i not in tribDict:
tribDict[i] = recTrib(i-1) + recTrib(i-2) + recTrib(i-3)
return tribDict[i]
n = random.randint(min_length, max_length)
problem = f"What is the {n}th Tribonacci number?"
solution = recTrib(n-1)
return problem, f'${solution}$'
def tribonacci_series(min_length=1, max_length=80):
r"""Fibonacci Series
| Ex. Problem | Ex. Solution |
| --- | --- |
| The Tribonacci Series of the first $8$ numbers is ? | $0, 0, 1, 1, 2, 4, 7, 13$ |
"""
tribDict = {0:0, 1:0, 2:1}
def createTribSeries(i):
tribSeries = []
for idx in range(i):
if idx not in tribDict:
tribDict[idx] = tribDict[idx-1] + tribDict[idx-2] + tribDict[idx-3]
tribSeries.append(tribDict[idx])
return tribSeries
n = random.randint(min_length, max_length)
tribSeries = createTribSeries(n)
problem = "The Tribonacci Series of the first ${n}$ numbers is ?"
solution = ', '.join(map(str, tribSeries))
return problem, f'${solution}$'