Write rules to implement a Prolog predicate fib (F0, F1, N, F) that means \"F is
ID: 3837195 • Letter: W
Question
Write rules to implement a Prolog predicate fib (F0, F1, N, F) that means "F is the Nth number in the Fibonacci sequence fib defined by fib(0) = F0, fib(1) = F1, and for N > 1, fib(N) = fib(N - 1) + fib(N - 2). For example, fib(1, 2, 6, F) will produce F = 21. Write rules to implement a Prolog predicate fibmemo(F0, F1, N, Fibs) that means "Fibs is the list of fib values (as defined in part a) starting with fib(N) and continuing down to fib(0) = F0. For example, fibmemo (1, 2, 6, Fibs) will produce Fibs = [21, 13, 8, 5, 3, 2, 1].Explanation / Answer
Recursive fibonacci series program which uses memorization concept.
def fibMemo():
total = {0:0, 1:1}
def fib(n):
if n not in total:
total[n] = fib(n-1) + fib(n-2)
return total[n]
return fib
fibans = fibMemo()
for i in range(1,6):
print fibans(i),
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.