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:
is_prime(X) :-
findall(
Y,
(between(1,X,Y), divisible(X,Y)),
F),
(F=[1,X];F=[X,1]).
divisible(X,Y) :-
0 is X rem Y.
% first 25 prime numbers.
test_is_prime() :-
findall(X,(between(1,100,X),is_prime(X)),L),
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
*/
determinant(A,B,C,D,Result) :-
Result is A*D-B*C.
determinant_is_prime(A,B,C,D) :-
determinant(A,B,C,D,Result),
Result > 0,
is_prime(Result).
solution(Result, Count) :-
findall(
(A,B,C,D),
(
between(0,11,A),
between(0,11,B),
between(0,11,C),
between(0,11,D),
determinant_is_prime(A,B,C,D)
),
Result
),
length(Result,Count).