Skip to content

Instantly share code, notes, and snippets.

@yycking
Created October 19, 2022 04:16
Show Gist options
  • Save yycking/5ae50957b75cca2c5b61de61f6c8e7a9 to your computer and use it in GitHub Desktop.
Save yycking/5ae50957b75cca2c5b61de61f6c8e7a9 to your computer and use it in GitHub Desktop.
用 Prolog 判斷此數是否為質數
:- set_prolog_flag(verbose, silent).
:- initialization(main).
可整除(X, Y):-
0 is X mod Y.
有因數(X, Y):-
可整除(X, Y).
有因數(X, Y):-
X > Y+1,
可整除(X, Y+1).
有因數(X):-
有因數(X, 2).
是質數(X):-
X < 2,
write(X), write('不是質數').
是質數(2):-
write('2是質數').
是質數(X):-
有因數(X),
write(X), write('不是質數').
是質數(X):-
write(X), write('是質數').
main:-
X is 33,
是質數(X),
halt.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment