Last active
October 7, 2015 00:09
-
-
Save rcotrina94/de93d64b76dd3eef39d6 to your computer and use it in GitHub Desktop.
Visual Prolog: Verificar si un número es primo o no.
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
% Copyright | |
class main | |
open core | |
predicates | |
tiene_divisores : (integer, integer, integer) procedure (i, i,o). | |
es_primo : (integer, boolean) procedure (i,o). | |
predicates | |
run : core::runnable. | |
end class main |
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
% Copyright | |
implement main | |
open core | |
clauses | |
tiene_divisores(_Num, 1, 0):-!. | |
tiene_divisores(Num, Acc, Ndiv):- | |
tiene_divisores(Num, Acc-1, Ndiv2), | |
if (Num mod Acc) = 0 then | |
Ndiv = Ndiv2 + 1, ! | |
else | |
Ndiv = Ndiv2 | |
end if. | |
es_primo(0,true):- !. | |
es_primo(1,true):- !. | |
es_primo(2,true):- !. | |
es_primo(3,true):- !. | |
es_primo(N,Primo):- | |
tiene_divisores(N, N-1, Ndiv), | |
if Ndiv = 0 then | |
Primo = true | |
else | |
Primo = false | |
end if, !. | |
clauses | |
run() :- | |
% 1000 primeros números primos: | |
% => https://primes.utm.edu/lists/small/1000.txt | |
% Numero = 3, % Es primo | |
% Numero = 7918, % No es primo | |
Numero = 7919, % Es primo | |
es_primo(Numero, Es_primo), | |
if Es_primo = true then R = " es primo" else R =" NO es primo" end if, | |
stdio::write(Numero, R), | |
programControl::sleep(4000), | |
succeed. % place your own code here | |
end implement main | |
goal | |
console::runUtf8(main::run). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment