Calculating whether number is prime in Prolog
问题 I am trying to calculate if the input is a prime number but something goes wrong... here's my code: primeNumber(X):- prime_prime(A, 1). prime_prime(A, B):- R is A mod B, R =:= 1, R =:= A. prime_prime(X, B):- B < A, Next is B + 1, prime_prime(A, Next). It gives me false every time. Anyone got any clues or idea on what I am doing wrong? 回答1: See http://www.swi-prolog.org/pldoc/man?function=mod/2: +IntExpr1 mod +IntExpr2 Modulo, defined as Result = IntExpr1 - (IntExpr1 div IntExpr2) × IntExpr2,