我的任务是:
给定函数 f 的以下定义,创建一个 Prolog 程序来计算所有 0 < i < 32 的 f(i)。
- f(0) = 0
- f(1) = 1
- f(n) = f(n-2) + 2*f(n-1)(n > 1)
到目前为止我的代码是:
problemThree(0, 0).
problemThree(1, 1).
problemThree(N, NF) :-
N > 1,
A is N - 2,
B is N - 1,
problemThree(A, AF),
problemThree(B, BF),
NF is AF + 2*BF.
Run Code Online (Sandbox Code Playgroud)
它正在工作,但需要很长时间才能显示 N > 20 的值。
请让我知道如何将值存储在列表中以使程序更快。