Skip to content

Instantly share code, notes, and snippets.

@anabastos
Last active October 2, 2016 05:41
Show Gist options
  • Save anabastos/97691e369e8a5307167b41bb79288272 to your computer and use it in GitHub Desktop.
Save anabastos/97691e369e8a5307167b41bb79288272 to your computer and use it in GitHub Desktop.
Desafio Fermat
#include <stdio.h>
int ehPrimo(long number) {
int i;
for (i=2; i<number; i++) {
if (number % i == 0 && i != number) return 0;
}
return 1;
}
void fazerFermat(long e) {
long p, q;
long x = 1;
long 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;
if (ehPrimo(p)) printf("\n%ld", p); else fazerFermat(p);
if (ehPrimo(q)) printf("\n%ld", q); else fazerFermat(q);
}
int main () {
long n;
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;
}
fazerFermat(n);
printf("\n=%ld\n", n);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment