Project Euler - Problem 1


/ Published in: Prolog
Save to your folder(s)



Copy this code and paste it in your HTML
  1. sum(N, N, N).
  2. sum(Start, End, Total) :-
  3. Start < End,
  4. applicable(Start),
  5. Next is Start + 1,
  6. sum(Next, End, SemiTotal),
  7. Total is SemiTotal + Start, !.
  8. sum(Start, End, Total) :-
  9. Start < End,
  10. Next is Start + 1,
  11. sum(Next, End, Total).
  12.  
  13.  
  14. applicable(Number) :-
  15. divisible(Number, 3).
  16. applicable(Number) :-
  17. divisible(Number, 5).
  18.  
  19.  
  20. divisible(Number, 0) :-
  21. write('Error: division by 0').
  22. divisible(Number, Divisor) :-
  23. Number mod Divisor =:= 0.

URL: http://13tazer31.wordpress.com/2011/02/06/problem-1/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.