Last active
January 6, 2017 12:12
-
-
Save Koitaro/822005 to your computer and use it in GitHub Desktop.
Prolog : Project Euler 1-9
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
| problem1(Answer) :- aggregate(sum(N),num(N),Answer). | |
| num(N) :- between(1,999,N), answer(N). | |
| answer(N) :- N rem 3 =:= 0, !. | |
| answer(N) :- N rem 5 =:= 0. |
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
| :- use_module(library(fibonacci), [fib/2]). | |
| problem2(Answer) :- aggregate(sum(N),fibEven(N),Answer). | |
| fibEven(X) :- | |
| between(1,inf,M), N is M*3, fib(N,X), | |
| (X < 4000000 -> true; !, false). |
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
| problem3(Answer) :- prime_factors(600851475143,L), max_list(L,Answer). | |
| prime_factors(N,X) :- prime_factors(N,2,X). | |
| prime_factors(N,P,[N]) :- N < P*P, !. | |
| prime_factors(N,P,[P|L]) :- N rem P =:= 0, !, N1 is N//P, prime_factors(N1,P,L). | |
| prime_factors(N,2,X) :- !, prime_factors(N,3,X). | |
| prime_factors(N,P,X) :- !, plus(P,2,P1), prime_factors(N,P1,X). |
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
| :- use_module(library(digits), [i2d/2]). | |
| problem4(Answer) :- solve(999,999,0,Answer). | |
| solve(99,_,Z,Z) :- !. | |
| solve(X,Y,Z,A) :- X*Y =< Z, !, X1 is X-1, solve(X1,X1,Z,A). | |
| solve(X,Y,Z,A) :- Y =:= 99, !, X1 is X-1, solve(X1,X1,Z,A). | |
| solve(X,Y,_,A) :- Z is X*Y, palindrome(Z), !, X1 is X-1, solve(X1,X1,Z,A). | |
| solve(X,Y,Z,A) :- Y1 is Y-1, solve(X,Y1,Z,A). | |
| palindrome(N) :- i2d(N,L), reverse(L,L). |
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
| problem5(Answer) :- solve(20,Answer). | |
| solve(1,1) :- !. | |
| solve(N,X) :- N1 is N-1, solve(N1,X1), X is N*X1//gcd(N,X1). |
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
| problem6(Answer) :- | |
| numlist(1,100,L), | |
| sumlist(L,X), square(X,X1), | |
| maplist(square,L,Y), sumlist(Y,Y1), | |
| Answer is X1-Y1. | |
| square(N,N*N). |
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
| isPrime(N,P) :- N < P*P, !. | |
| isPrime(N,P) :- N rem P =:= 0, !, false. | |
| isPrime(N,P) :- P1 is P+2, isPrime(N,P1). | |
| nextPrime(2,3) :- !. | |
| nextPrime(P,X) :- X is P+2, isPrime(X,3), !. | |
| nextPrime(P,X) :- P1 is P+2, nextPrime(P1,X). | |
| primes(10001,P,P) :- !. | |
| primes(N,P,X) :- N1 is N+1, nextPrime(P,P1), primes(N1,P1,X). | |
| problem7(X) :- primes(1,2,X). |
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
| :- use_module(library(digits),[i2d/2]). | |
| problem8(Answer) :- aggregate(max(N),solve(N),Answer). | |
| solve(X) :- findall(N,num(N),L), append(_,[A,B,C,D,E|_],L), X is A*B*C*D*E. | |
| num(N) :- data(Data), member(L,Data), i2d(L,L1), member(N,L1). | |
| data([73167176531330624919225119674426574742355349194934, | |
| 96983520312774506326239578318016984801869478851843, | |
| 85861560789112949495459501737958331952853208805511, | |
| 12540698747158523863050715693290963295227443043557, | |
| 66896648950445244523161731856403098711121722383113, | |
| 62229893423380308135336276614282806444486645238749, | |
| 30358907296290491560440772390713810515859307960866, | |
| 70172427121883998797908792274921901699720888093776, | |
| 65727333001053367881220235421809751254540594752243, | |
| 52584907711670556013604839586446706324415722155397, | |
| 53697817977846174064955149290862569321978468622482, | |
| 83972241375657056057490261407972968652414535100474, | |
| 82166370484403199890008895243450658541227588666881, | |
| 16427171479924442928230863465674813919123162824586, | |
| 17866458359124566529476545682848912883142607690042, | |
| 24219022671055626321111109370544217506941658960408, | |
| 07198403850962455444362981230987879927244284909188, | |
| 84580156166097919133875499200524063689912560717606, | |
| 05886116467109405077541002256983155200055935729725, | |
| 71636269561882670428252483600823257530420752963450]). |
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
| problem9(Answer) :- | |
| between(1,333,A), | |
| Bmin is A+1, Bmax is (1000-A)//2, | |
| between(Bmin,Bmax,B), | |
| C is 1000-A-B, | |
| A*A + B*B =:= C*C, | |
| Answer is A*B*C, !. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment