Home > Archive > Prolog > February 2005 > Help!! Forward Fibonacci Series Progam
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Help!! Forward Fibonacci Series Progam
|
|
| J.K.W.Y. 2005-02-20, 3:57 pm |
| Hi, I'm stuck in my homework, could anyone please help me??
How to use Prolog to write forward Fibonacci series. It's something
like:-
fib_for(N, F, F1, N, F).
fib_for(C, F2, F1, N, F) :-
...
fib_for(C1, F3, F2, N, F).
The program will always be queried thus:
?fib_for(2, 1, 1, N, F). e.g. ?fib_for(2, 1, 1, 58, F).
where N is the element in the series required and F will hold the
computed result.
I only found some information on recursive Fibonacci but there's
nothing about forward Fibonacci. Are they similar?? Please help. Thank
you very much.
J.K.W.Y.
| |
| Markus Triska 2005-02-20, 8:57 pm |
| J.K.W.Y. wrote:
>
> The program will always be queried thus:
>
> ?fib_for(2, 1, 1, N, F). e.g. ?fib_for(2, 1, 1, 58, F).
It is often handy to introduce additional predicates, in this case
fib_for/2, to shorten your queries:
fib_for(N, F) :-
fib_for(0, 1, N, F).
fib_for(_N0, N1, 0, N1).
fib_for(N0, N1, N, F) :-
N > 0,
N2 is N0 + N1,
Nm1 is N - 1,
fib_for(N1, N2, Nm1, F).
The query "?- fib_for(12, F)." unifies F with the 13th number in the
sequence.
Best regards,
Markus.
|
|
|
|
|