Skip to content

Instantly share code, notes, and snippets.

@anabastos
Created October 3, 2016 01:14
Show Gist options
  • Save anabastos/1f5dc5e86c840a12ac0290d5cc9734e9 to your computer and use it in GitHub Desktop.
Save anabastos/1f5dc5e86c840a12ac0290d5cc9734e9 to your computer and use it in GitHub Desktop.
Fermat usando goto q fico horrivel
#include <stdio.h>
int main () {
long n, pPrimo, qPrimo, p, q, e, x, y;
int i, j;
printf ("-----------");
printf ("\nF e R m A t");
printf ("\n-----------");
printf ("\nDigite um numero impar: "); scanf ("%ld", &n);
printf ("---------");
printf ("\n|%ld|\n---------", n);
if (n<1) {
printf ("\nTem que ser maior que 0\n");
return 1;
}
if (n%2==0) {
printf ("\nNao e impar\n");
return 1;
}
goto FAZERFERMAT;
FAZERFERMAT:
e = (-1) * n;
x = 1;
y = 1;
//e *= (-1);
while (e != 0) if (e < 0) { e += y; y += 2; }
else { e -= x; x += 2; }
x = (x-1)/2;
y = (y-1)/2;
q = y + x;
p = y - x;
goto CHECAPRIMOQ;
FIM:
printf("\n--------\n");
return 0;
CHECAPRIMOP:
pPrimo = 1;
for (i=2; i<p; i++) {
if (p % i == 0 && i != p) {
pPrimo = 0;
}
}
if (pPrimo == 1) {
printf("\n%ld", p);
}
goto CHECAPRIMOS;
CHECAPRIMOQ:
qPrimo = 1;
for (j=2; j<q; j++) {
if (q % j == 0 && j != q) {
qPrimo = 0;
}
}
if (qPrimo == 1) {
printf("\n%ld", q);
}
goto CHECAPRIMOP;
CHECAPRIMOS:
if (pPrimo == 0) {
n=p;
goto FAZERFERMAT;
}
else {
if (qPrimo == 0) {
n=q;
goto FAZERFERMAT;
} else {
goto FIM;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment