How to find nth fibonacci term in prolog?

时间:2018-02-03 09:47:44

标签: prolog fibonacci

Here's the code that i've written:

/* fib(X,Y) means xth term in fibonacci series is Y. */

fib(1,0).                     
fib(2,1).
fib(X,Y):-X1 is X-1,fib(X1,Y1),X2 is X-2,fib(X2,Y2),Y=Y1+Y2.

?-fib(6,X).
X = 1+0+1+(1+0)+(1+0+1) .

Why am I getting the result as a summation series? What corrections should I make?

1 个答案:

答案 0 :(得分:0)

您正在使用+号进行求和,当其应为“是”时。在序言中,正确的代码将是:

  "angularCompilerOptions": {
    "disableTypeScriptVersionCheck": true,
  },