(Prolog) Project Euler Problem 2

  1. fibonacci(_,Current,End,0) :-
  2.         Current > End.
  3. fibonacci(Previous, Current, End, Total) :-
  4.         divisible(Current, 2),
  5.         Next is Current + Previous,
  6.         fibonacci(Current, Next, End, Sum),
  7.         Total is Sum + Current, !.
  8. fibonacci(Previous, Current, End, Total) :-
  9.         Next is Current + Previous,
  10.         fibonacci(Current, Next, End, Total).
  11.  
  12. divisible(Number, 0) :-
  13.         write(‘Error: division by 0′).         
  14. divisible(Number, Divisor) :-
  15.         Number mod Divisor =:= 0.
This entry was posted in Prolog and tagged , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Why ask?