here's my code to print N elements of the fibonacci series ( here N means excluding the first two basic elements of the series, 0,1.
start:- write('Enter n '),nl, read(N), write('0 1'), %printing first two necessary numbers of the series fib(N,0,1). % so i'm reading N, the no of elements of the fibonacci series the user would like to %print, fib(N,A,B):- % A and B are the n-2 and n-1 that add up to give n, N>1, C = A+B, B = C, A = B, write(C),nl, X is N-1, fib(X,A,B). % i directly put N-1 as the first argument in previous versions..would that create problems?
also i tried to do it this way too:
fib(0, 0, A):-!. fib(1, 1, A):-!. fib(N, X, A):- N1 is N-1, N2 is N-2, fib(N1, X1,1), fib(N2, X2,0), X is X1+X2. display(X,A). display(X,A):- A==1, write(X),nl.
the A argument in display is a flag. it means, whenever the brancing happens from N-1 side of the recursion, the sum will be printed then only.. otherwise it will print like this: 1 2 1 3 1 5 etc