use unsigned longs and reformat to 8-width indents

This commit is contained in:
Rebecca Doth 2023-11-10 15:24:48 +01:00
parent 2e25c71560
commit 13cb4983b5
8 changed files with 140 additions and 138 deletions

3
.gitignore vendored
View File

@ -1,5 +1,4 @@
primes*
.cache
*.json
core
gmon.out

68
args.c
View File

@ -6,48 +6,52 @@
void init_args(_Args* args)
{
args->printing = 0;
args->printing = 0;
}
_Bool parse_args(int argc, char** argv, _Args* args)
{
_Bool ret = 1;
for(int i = 2; i < argc; ++i)
{
if(strlen(argv[i]) > 1)
_Bool ret = 1;
for(int i = 2; i < argc; ++i)
{
if(argv[i][0] == '-')
{
if(argv[i][1] == '-')
if(strlen(argv[i]) > 1)
{
if(strcmp(argv[i], "--print") == 0)
{
args->printing = 1;
continue;
} else {
printf("Unknwon option: %s\n", (argv[i] + 2));
ret = 0;
continue;
}
} else {
for(unsigned long j = 1; j < strlen(argv[i]); ++j)
{
switch(argv[i][j])
if(argv[i][0] == '-')
{
case 'p':
args->printing = 1;
continue;
default:
printf("Unknwon option: %c\n", argv[i][j]);
ret = 0;
continue;
if(argv[i][1] == '-')
{
if(strcmp(argv[i], "--print") == 0)
{
args->printing = 1;
continue;
}
else
{
printf("Unknwon option: %s\n", (argv[i] + 2));
ret = 0;
continue;
}
}
else
{
for(unsigned long j = 1; j < strlen(argv[i]); ++j)
{
switch(argv[i][j])
{
case 'p':
args->printing = 1;
continue;
default:
printf("Unknwon option: %c\n", argv[i][j]);
ret = 0;
continue;
}
}
}
}
}
}
}
}
}
return ret;
return ret;
}

2
args.h
View File

@ -2,7 +2,7 @@
#define _h_args
typedef struct Args {
_Bool printing;
_Bool printing;
} _Args;
void init_args(_Args* args);

View File

@ -1,17 +1,17 @@
#include "common.h"
ull isqrt(ull i)
unsigned long isqrt(unsigned long i)
{
ull l = 0;
ull r = i + 1;
while (l != r - 1)
{
ull m = (l + r) / 2;
if (m * m <= i)
l = m;
else
r = m;
}
return l;
unsigned long l = 0;
unsigned long r = i + 1;
while (l != r - 1)
{
unsigned long m = (l + r) / 2;
if (m * m <= i)
l = m;
else
r = m;
}
return l;
}

View File

@ -1,12 +1,10 @@
#ifndef _h_common
#define _h_common
typedef unsigned long long ull;
#define SIZE ( primes_max * sizeof(_Bool))
#define SIZEP (*primes_max * sizeof(_Bool))
ull isqrt(ull i);
unsigned long isqrt(unsigned long i);
#endif

View File

@ -8,20 +8,20 @@
#include "common.h"
#include "methods.h"
clock_t eratosthenes(_Bool** primes, ull* primes_max)
clock_t eratosthenes(_Bool** primes, unsigned long* primes_max)
{
clock_t begin = clock();
for(ull i = 2; i < isqrt(*primes_max); ++i)
{
if((*primes)[i])
clock_t begin = clock();
for(unsigned long i = 2; i < isqrt(*primes_max); ++i)
{
for(ull c = 2, j = 0; j < *primes_max; j = (i * c), ++c)
{
(*primes)[j] = 0;
}
if((*primes)[i])
{
for(unsigned long c = 2, j = 0; j < *primes_max; j = (i * c), ++c)
{
(*primes)[j] = 0;
}
}
}
}
return begin;
return begin;
}

153
main.c
View File

@ -13,91 +13,92 @@
void show_help(char* argv0)
{
fprintf(stderr,
"Usage: %s range [args]\n"
" Check primes from 0 to range (Maximum %llu)\n\n"
fprintf(stderr,
"Usage: %s range [args]\n"
" Check primes from 0 to range (Maximum %lu)\n\n"
"Args:"
" -p, --print Print primes found\n",
argv0,
ULLONG_MAX);
"Args:"
" -p, --print Print primes found\n",
argv0,
ULONG_MAX
);
}
int main(int argc, char** argv)
{
if(argc <= 1)
{
show_help(argv[0]);
return EXIT_FAILURE;
}
_Args args;
init_args(&args);
if(!parse_args(argc, argv, &args))
{
show_help(argv[0]);
return EXIT_FAILURE;
}
/* check primes up to primes_stop */
ull primes_max = strtoull(argv[1], NULL, 10);
if(primes_max == 0)
{
show_help(argv[0]);
return EXIT_FAILURE;
}
_Bool *primes = malloc(SIZE);
if(primes == NULL)
{
perror("malloc");
return EXIT_FAILURE;
}
// init array
for(ull i = 0; i < primes_max; ++i)
{
primes[i] = 1;
}
primes[0] = 0;
primes[1] = 0;
clock_t begin_primes = eratosthenes(&primes, &primes_max);
if(begin_primes == -1)
{
free(primes);
return EXIT_FAILURE;
}
clock_t end_primes = clock();
clock_t begin_print = clock();
if(args.printing)
{
for(ull primes_indx = 0; primes_indx < primes_max; ++primes_indx)
if(primes[primes_indx])
/* TODO: optimise printing */
printf("%llu\n", primes_indx);
}
clock_t end_print = clock();
ull prime_count = 0;
for(ull i = 0; i < primes_max; ++i)
{
if(primes[i])
if(argc <= 1)
{
++prime_count;
show_help(argv[0]);
return EXIT_FAILURE;
}
}
printf("Counted %llu primes\nCalculation took: %0.fms\n",
prime_count,
((double)(end_primes - begin_primes) / CLOCKS_PER_SEC) * (double)1000);
_Args args;
init_args(&args);
if(!parse_args(argc, argv, &args))
{
show_help(argv[0]);
return EXIT_FAILURE;
}
if(args.printing)
printf("Printing took %0.fms\n",
((double)(end_print - begin_print ) / CLOCKS_PER_SEC) * (double)1000);
unsigned long primes_max = strtoul(argv[1], NULL, 0);
if(primes_max == 0)
{
show_help(argv[0]);
return EXIT_FAILURE;
}
free(primes);
return EXIT_SUCCESS;
_Bool *primes = malloc(SIZE);
if(primes == NULL)
{
perror("malloc");
return EXIT_FAILURE;
}
for(unsigned long i = 0; i < primes_max; ++i)
{
primes[i] = 1;
}
primes[0] = 0;
primes[1] = 0;
clock_t begin_primes = eratosthenes(&primes, &primes_max);
if(begin_primes == -1)
{
free(primes);
return EXIT_FAILURE;
}
clock_t end_primes = clock();
clock_t begin_print = clock();
if(args.printing)
{
for(unsigned long primes_indx = 0; primes_indx < primes_max; ++primes_indx)
if(primes[primes_indx])
/* TODO: optimise printing */
printf("%lu\n", primes_indx);
}
clock_t end_print = clock();
unsigned long prime_count = 0;
for(unsigned long i = 0; i < primes_max; ++i)
{
if(primes[i])
{
++prime_count;
}
}
printf("Counted %lu primes\nCalculation took: %0.fms\n",
prime_count,
((double)(end_primes - begin_primes) / CLOCKS_PER_SEC) * (double)1000
);
if(args.printing)
printf("Printing took %0.fms\n",
((double)(end_print - begin_print ) / CLOCKS_PER_SEC) * (double)1000
);
free(primes);
return EXIT_SUCCESS;
}

View File

@ -5,7 +5,7 @@
#include "common.h"
clock_t eratosthenes(_Bool** primes, ull* array_max);
clock_t eratosthenes(_Bool**, unsigned long*);
#endif