Skip to content

Instantly share code, notes, and snippets.

/prime.c Secret

Created June 6, 2015 18:46
Show Gist options
  • Save anonymous/ebc64d49abc5f1ee4e81 to your computer and use it in GitHub Desktop.
Save anonymous/ebc64d49abc5f1ee4e81 to your computer and use it in GitHub Desktop.
prime test
// compilation e.g. with gcc a.c -lm
#include "stdio.h"
#include "math.h"
#include "stdint.h"
#define true 1
#define false 0
#define bool int
typedef uint32_t u32;
int main () {
u32 nb = 1;
u32 nb_test = 2;
bool not_prime = true;
if ((nb % 2) == 0) { // If the number is even, add 1 to make it odd
nb++;
}
for (nb = nb; true; nb += 2) {
u32 square_root = sqrt(nb) + 1;
not_prime = true;
for (nb_test = 2; nb_test < square_root; nb_test++) {
if (nb % nb_test == 0) {
not_prime = false;
break;
}
}
if (not_prime) {
printf("%d\n", nb);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment