/ Published in: Prolog
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
largest_prime_factor(Number, Prime) :- N is round(sqrt(Number)), largest_prime_factor(Number, N, Prime). largest_prime_factor(Number, N, N) :- divisible(Number, N), prime(N), !. largest_prime_factor(Number, N, Prime) :- Next is N-1, largest_prime_factor(Number, Next, Prime). divisible(Number, 0) :- write('Error: division by 0'). divisible(Number, Divisor) :- Number mod Divisor =:= 0. prime(Number) :- N is round(sqrt(Number)), prime(Number, 1, N). prime(Number, N, N) :- !. prime(Number, Divisor, End) :- Divisor < End, NextDivisor is Divisor + 1, \+ divisible(Number, NextDivisor), prime(Number, NextDivisor, End).
URL: http://13tazer31.wordpress.com/2011/02/06/project-euler-problem-3/