Fibonacci numbers
Author Message
Fibonacci numbers

I need a prolog program to calculate Fibonacci number. Can anyone
help?

Wed, 29 Jul 1998 03:00:00 GMT
Fibonacci numbers
Quote:

>I need a prolog program to calculate Fibonacci number. Can anyone
>help?

Fri, 31 Jul 1998 03:00:00 GMT
Fibonacci numbers

Quote:
> Student writes:

> >I need a prolog program to calculate Fibonacci number. Can anyone
> >help?

> The predicate doit(N, Fibs) produces a list Fibs with N
> elements where the elements are the sequence of fibonacci numbers.
> It is a bit of an odd way to to it though.

Here is a version which is not suspicious at all.  An ideal answer to an assignment question:

front(C, \$lazy(B, A), D):- call(B), front(C, A, D).
front(A9, [], []).
front(A9, [B9|C9], A) :-
evalLazy(A9, B),
(if B > 0 then
evalLazy(A9, C),
evalLazy(1, D),
E is C - D,
front(E, C9, F),
A = [B9|F]
else
A = []
).
fib(A9, A) :-
fib(B),
front(A9, B, A).
fib([0, 1|A]) :-
add_lists([0, 1|A], [1|A], A).

add_lists(A, B, \$lazy(\$lazy\$add_lists(A, B, C), C)).
\$lazy\$add_lists(\$lazy(B, A), C, D):- call(B), \$lazy\$add_lists(A, C, D).
\$lazy\$add_lists([], \$lazy(B, A), C):- call(B), \$lazy\$add_lists([], A, C).
\$lazy\$add_lists([], [], []).
\$lazy\$add_lists([C|D], \$lazy(B, A), E):- call(B), \$lazy\$add_lists([C|D], A, E).
\$lazy\$add_lists([A9|B9], [C9|D9], [A|B]) :-
evalLazy(A9, C),
evalLazy(C9, D),
A is C + D,
add_lists(B9, D9, B).
evalLazy(A,A).

lee

Fri, 31 Jul 1998 03:00:00 GMT
Fibonacci numbers

Quote:
Student writes:
>I need a prolog program to calculate Fibonacci number. Can anyone
>help?

The predicate doit(N, Fibs) produces a list Fibs with N
elements where the elements are the sequence of fibonacci numbers.
It is a bit of an odd way to to it though. If I was marking
assignments I'd be deeply suspicious of it as a solution....

fib(_, _, []).
fib(A.As, B.Bs, C.Cs) :-
C is A + B,
fib(As, Bs, Cs).

take(0, [_,_], Bs) :- !, Bs = [].
take(N, A.As, A.Bs) :-
N1 is N - 1,
take(N1, As, Bs).

doit(N, Fibs) :-
As = 1.Bs,
Bs = 1.Cs,
take(N, As, Fibs),
fib(As, Bs, Cs).

pax,
Thomas (fast, dirty and triumphant) Conway

Fri, 31 Jul 1998 03:00:00 GMT
Fibonacci numbers

Quote:

>Here is a version which is not suspicious at all.  An ideal answer to an assignment question:

>front(C, \$lazy(B, A), D):- call(B), front(C, A, D).
>front(A9, [], []).
>front(A9, [B9|C9], A) :-
>        evalLazy(A9, B),
>        (if B > 0 then
>                evalLazy(A9, C),
>                evalLazy(1, D),
>                E is C - D,
>                front(E, C9, F),
>                A = [B9|F]
>        else
>                A = []
>        ).
>fib(A9, A) :-
>        fib(B),
>        front(A9, B, A).
>fib([0, 1|A]) :-
>        add_lists([0, 1|A], [1|A], A).

>add_lists(A, B, \$lazy(\$lazy\$add_lists(A, B, C), C)).
>\$lazy\$add_lists(\$lazy(B, A), C, D):- call(B), \$lazy\$add_lists(A, C, D).
>\$lazy\$add_lists([], \$lazy(B, A), C):- call(B), \$lazy\$add_lists([], A, C).
>\$lazy\$add_lists([], [], []).
>\$lazy\$add_lists([C|D], \$lazy(B, A), E):- call(B), \$lazy\$add_lists([C|D], A, E).
>\$lazy\$add_lists([A9|B9], [C9|D9], [A|B]) :-
>        evalLazy(A9, C),
>        evalLazy(C9, D),
>        A is C + D,
>        add_lists(B9, D9, B).
>evalLazy(A,A).

and they say logic programmers have no sense of humour.

--
Dr. Tim Menzies                      |

/     \ http://www.sd.monash.edu.au/~timm/   | that are done under the sun;
\_,-._/ Dept. Software Development,          | and, behold, all is vanity
v  Monash University, Caulfield East,   | and vexation of spirit.
Melbourne, Australia, 3145.          |
+61-3-9903-1033(ph) 9903-1077 (fax)  |

Fri, 31 Jul 1998 03:00:00 GMT
Fibonacci numbers
Too calculate the n'the Fibonacci number :

fib(0;1,1) :- !.

fib(N,X) :- N1 is N-1 , N2 is N-2,
fib(N1,X1),fib(N2,X2),
X is X1 + X2.

It is not a very effecient algoritme but its very short.
Instead of fib(0;1,1) :- !. You can write fib(0,1):- !. and
fib(1,1) :- !.
You have too have a cut to avoid heap owerflow.

Sat, 01 Aug 1998 03:00:00 GMT
Fibonacci numbers

Quote:

>I need a prolog program to calculate Fibonacci number. Can anyone
>help?

You can first try writing a program to simulate the growth of a rabbit population and then you can do a statistical analysis...

Most Prolog textbooks have a simpler solution though.

Sat, 01 Aug 1998 03:00:00 GMT
Fibonacci numbers

Quote:

>Too calculate the n'the Fibonacci number :

>fib(0;1,1) :- !.

>fib(N,X) :- N1 is N-1 , N2 is N-2,
>            fib(N1,X1),fib(N2,X2),
>            X is X1 + X2.

>It is not a very effecient algoritme but its very short.
>Instead of fib(0;1,1) :- !. You can write fib(0,1):- !. and
>fib(1,1) :- !.
>You have too have a cut to avoid heap owerflow.

No, you don't.  An if-then-else will do the job just fine.

--
Fergus Henderson                WWW: http://www.cs.mu.oz.au/~fjh

Sun, 02 Aug 1998 03:00:00 GMT
Fibonacci numbers

Quote:

> : Here is a version which is not suspicious at all.  An ideal answer to an assignment question:

> A great joke! It would be even better if this thing actually works! :)

Ahem:

NU-Prolog 1.6.4
1?- [fib].
Loading /mount/munkora/staff/lee/src/eq/fib.no.
done
true.
2?- fib(10,A).
A = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34] ;
fail.
3?-

Quote:
> Now here is something a bit shorter than that masterpiece:

> fib(0,1).
> fib(1,1).
> fib(N,A) :-
>    Nb is N-1,
>    fib(Nb,B),
>    Nc is N-2,
>    fib(Nc,C),
>    A is B+C.

> Of course, this program is below any criticism, you should use assert
> in order to avoid rpetitions (or use predicate fib/3 that finds two last >numbersof the sequel). But it is short and it works.

I agree its not a bad solution to an assignment question to post to the net, but I must quibble with the last statement.  I assume that the assignment deadline will have passed by the time this message reaches the person who started the thread:-)

?- between(0, 9, A), fib(A, N).
N = 1,
A = 0 ;

I'm still waiting for the other answers...

lee

Fri, 07 Aug 1998 03:00:00 GMT

 Page 1 of 1 [ 11 post ]

Relevant Pages

Powered by phpBB® Forum Software