Skip to content
Snippets Groups Projects
Commit 815d5c9e authored by nimrod's avatar nimrod
Browse files

Replace long with int in the C version to improve performance.

parent eca72eb6
No related branches found
No related tags found
No related merge requests found
...@@ -2,11 +2,11 @@ ...@@ -2,11 +2,11 @@
#include <stdlib.h> #include <stdlib.h>
#include <math.h> #include <math.h>
int isPrime(unsigned long x) int isPrime(unsigned int x)
{ {
if (sqrt(x) - (unsigned long)sqrt(x) == 0) if (sqrt(x) - (unsigned int)sqrt(x) == 0)
return 0; return 0;
for (unsigned long i = 2; i < ceil(sqrt(x)); i++) for (unsigned int i = 2; i < ceil(sqrt(x)); i++)
if (x % i == 0) if (x % i == 0)
return 0; return 0;
return 1; return 1;
...@@ -14,13 +14,13 @@ int isPrime(unsigned long x) ...@@ -14,13 +14,13 @@ int isPrime(unsigned long x)
int main( int argc, char** argv) int main( int argc, char** argv)
{ {
unsigned long max = atol(argv[1]); unsigned int max = atol(argv[1]);
unsigned long *a = malloc(sizeof(unsigned long) * max); unsigned int *a = malloc(sizeof(unsigned int) * max);
unsigned long x = 2; unsigned int x = 2;
for (unsigned long i = 0; i < max; x++) for (unsigned int i = 0; i < max; x++)
if (isPrime(x)) if (isPrime(x))
a[i++] = x; a[i++] = x;
for (unsigned long i = 0; i < max; i++) for (unsigned int i = 0; i < max; i++)
printf("%lu\n", (unsigned long)a[i]); printf("%u\n", (unsigned int)a[i]);
return 0; return 0;
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment