Created
April 14, 2018 18:00
-
-
Save muirandy/83c1441bbc767f3ecad8cdc47ab6fe59 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Written using GnuProlog.
Load with: ['fib'].
Run with: fib(5, What).