Project Euler - Problem 3


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



Copy this code and paste it in your HTML
  1. largest_prime_factor(Number, Prime) :-
  2. N is round(sqrt(Number)),
  3. largest_prime_factor(Number, N, Prime).
  4.  
  5. largest_prime_factor(Number, N, N) :-
  6. divisible(Number, N),
  7. prime(N), !.
  8. largest_prime_factor(Number, N, Prime) :-
  9. Next is N-1,
  10. largest_prime_factor(Number, Next, Prime).
  11.  
  12.  
  13.  
  14. divisible(Number, 0) :-
  15. write('Error: division by 0').
  16. divisible(Number, Divisor) :-
  17. Number mod Divisor =:= 0.
  18.  
  19.  
  20.  
  21. prime(Number) :-
  22. N is round(sqrt(Number)),
  23. prime(Number, 1, N).
  24.  
  25. prime(Number, N, N) :- !.
  26. prime(Number, Divisor, End) :-
  27. Divisor < End,
  28. NextDivisor is Divisor + 1,
  29. \+ divisible(Number, NextDivisor),
  30. prime(Number, NextDivisor, End).

URL: http://13tazer31.wordpress.com/2011/02/06/project-euler-problem-3/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.