Skip to content

Instantly share code, notes, and snippets.

@bostelk
Last active January 27, 2019 16:40
Show Gist options
  • Save bostelk/6a5350368b14ec1f9b606bae743b5dfe to your computer and use it in GitHub Desktop.
Save bostelk/6a5350368b14ec1f9b606bae743b5dfe to your computer and use it in GitHub Desktop.
Solution to @TechSparx's special math challenge!
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).
/*
Q:
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!
See: https://twitter.com/TechSparx/status/1089448076308299776
A:
2019
*/
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).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment