Jan 28, 2019
I decided to solve this math challenge using Prolog because I have a fledgling interest in NLP and figured this was a good opportunity to learn a new language.
🎁 Special math challenge!
How many 2x2 matrices with all integer elements between 0 and 11 (inclusive) are there, such that the determinant is a (positive) prime number?
Remember: 1 is not prime! Solution posted tomorrow.
— Martin Roberts (@TechSparx) January 27, 2019
The solution is 2019:
X) :-
is_prime(
findall(Y,
1,X,Y), divisible(X,Y)),
(between(F),
F=[1,X];F=[X,1]).
(
X,Y) :-
divisible(0 is X rem Y.
% first 25 prime numbers.
:-
test_is_prime() X,(between(1,100,X),is_prime(X)),L),
findall(L=[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97].
/*
let matrix =
|A B|
|C D|
then,
determinate = A*D-B*C
*/
A,B,C,D,Result) :-
determinant(Result is A*D-B*C.
A,B,C,D) :-
determinant_is_prime(A,B,C,D,Result),
determinant(Result > 0,
Result).
is_prime(
Result, Count) :-
solution(
findall(A,B,C,D),
(
(0,11,A),
between(0,11,B),
between(0,11,C),
between(0,11,D),
between(A,B,C,D)
determinant_is_prime(,
)Result
,
)Result,Count). length(