Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Prolog 0.39 KB | None | 0 0
  1. fib2(0,0). % N=0, F=0 (as per definition)
  2. fib2(1,1). % N=0, F=1 (as per definition)
  3. fib2(N, F) :- N > 1, fib2(N, 1, 0, F). % Call fib2, N=1 => F=1, N=0 => F=0.
  4. fib2(2, F1, F2, F) :- F is F1 + F2. % At N=2, F = F1+F2
  5. fib2(N, F1, F2, F) :- N < 1, F is F1;
  6.                       F3 is F1 + F2, % Calculate this Fibonacci
  7.               N1 is N - 1, % Calculate Previous Fibonacci
  8.               fib2(N1,F3,F1,F). %
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement