Advertisement
cd62131

PrimeNumber

Jul 25th, 2014
425
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.15 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. int feltes(int n);
  5. int prime(int n);
  6. int main(void) {
  7.   int i, j, P[20], n, ans;
  8.   for (i = 0; i < 20; i++)
  9.     P[i] = 0;
  10.   puts("feltes:");
  11.   for (i = 2; i < 100; i++)
  12.     if (feltes(i)) printf("%d\n", i);
  13.   puts("prime:");
  14.   srand((unsigned) time(NULL));
  15.   for (i = 0; i < 20; i++) {
  16.     do {
  17.       n = (int) ((rand() / ((double) RAND_MAX + 1.)) * 100) + 1;
  18.       ans = prime(n);
  19.       if (ans != 1) continue;
  20.       for (j = 0; j < i; j++)
  21.         if (P[j] == n) goto cont;
  22.       P[i] = n;
  23.       break;
  24.       cont: continue;
  25.     } while (1);
  26.     printf("%d\n", P[i]);
  27.   }
  28.   return 0;
  29. }
  30. int feltes(int n) {
  31.   int i, j;
  32.   unsigned long long rui;
  33.   if (n < 2) return 0;
  34.   if (n == 2) return 1;
  35.   for (i = 2; i < n - 1; i++) {
  36.     rui = i;
  37.     for (j = 1; j < n - 1; j++) {
  38.       rui *= i;
  39.       if (rui < 0) return 0;
  40.     }
  41.     if (rui % n != 1) {
  42.       return 0;
  43.     } else {
  44.       return 1;
  45.     }
  46.   }
  47.   return 0;
  48. }
  49. int prime(int n) {
  50.   int i;
  51.   if (n < 2) return 0;
  52.   if (n == 2) return 1;
  53.   for (i = 2; i * i <= n; i++)
  54.     if (n % i == 0) return 0;
  55.   return 1;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement