Created
October 19, 2022 04:16
-
-
Save yycking/5ae50957b75cca2c5b61de61f6c8e7a9 to your computer and use it in GitHub Desktop.
用 Prolog 判斷此數是否為質數
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
:- 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