Skip to content

Instantly share code, notes, and snippets.

@muirandy
Created April 14, 2018 18:00
Show Gist options
  • Save muirandy/83c1441bbc767f3ecad8cdc47ab6fe59 to your computer and use it in GitHub Desktop.
Save muirandy/83c1441bbc767f3ecad8cdc47ab6fe59 to your computer and use it in GitHub Desktop.
fib(1, 1).
fib(2, 1).
fib(X, Y) :- myFib(X, Y, [1,1]).
count(0, []).
count(Count, [Head|Tail]) :- count(TailCount, Tail), Count is TailCount + 1.
head(H, [Head|Tail]) :- H is Head.
myFib(P, R, V) :- count(C, V), C = P, head(R, V).
myFib(P, R, V) :- addToSequence(V, V2), count(C, V2), \+(C = P), P2 is P - 1, myFib(P2, R, V2).
addToSequence([Head1|[Head2|Tail]], [R, Head1]) :- R is Head1 + Head2.
@muirandy
Copy link
Author

Written using GnuProlog.
Load with: ['fib'].
Run with: fib(5, What).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment