Skip to content

Commit

Permalink
cleanup some codes for global variables and clang-tidy specs
Browse files Browse the repository at this point in the history
  • Loading branch information
kvedala committed Jul 13, 2020
1 parent 08415b0 commit d19a3a7
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 20 deletions.
32 changes: 24 additions & 8 deletions project_euler/problem_13/sol1.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,23 @@ int get_number(FILE *fp, char *buffer, uint8_t *out_int)
long L = strlen(buffer);

for (int i = 0; i < L; i++)
{
if (buffer[i] < 0x30 || buffer[i] > 0x39)
{
perror("found inavlid character in the number!");
return -1;
}
else
{
out_int[L - i - 1] = buffer[i] - 0x30;
}
}

return 0;
}

/**
* Function to add arbitraty length decimal integers stored in an array.
* Function to add arbitrary length decimal integers stored in an array.
* a + b = c = new b
*/
int add_numbers(uint8_t *a, uint8_t *b, uint8_t N)
Expand All @@ -49,21 +53,25 @@ int add_numbers(uint8_t *a, uint8_t *b, uint8_t N)
for (int i = 0; i < N; i++)
{
// printf("\t%d + %d + %d ", a[i], b[i], carry);
c[i] = carry + a[i] + b[i];
if (c[i] > 9) /* check for carry */
c[i] = carry + a[i] + b[i]; // NOLINT // This is a known false-positive
if (c[i] > 9) /* check for carry */
{
carry = 1;
c[i] -= 10;
}
else
{
carry = 0;
}
// printf("= %d, %d\n", carry, c[i]);
}

for (int i = N; i < N + 10; i++)
{
if (carry == 0)
{
break;
}
// printf("\t0 + %d + %d ", b[i], carry);
c[i] = carry + c[i];
if (c[i] > 9)
Expand All @@ -72,7 +80,9 @@ int add_numbers(uint8_t *a, uint8_t *b, uint8_t N)
c[i] -= 10;
}
else
{
carry = 0;
}
// printf("= %d, %d\n", carry, c[i]);
}
return 0;
Expand All @@ -89,9 +99,13 @@ int print_number(uint8_t *number, uint8_t N, int8_t num_digits_to_print)

/* if end_pos < 0, print all digits */
if (num_digits_to_print < 0)
{
end_pos = 0;
}
else if (num_digits_to_print <= start_pos)
{
end_pos = start_pos - num_digits_to_print + 1;
}
else
{
fprintf(stderr, "invalid number of digits argumet!\n");
Expand All @@ -105,14 +119,14 @@ int print_number(uint8_t *number, uint8_t N, int8_t num_digits_to_print)
return 0;
}

/** number of digits of the large number */
#define N 10
/** number of digits in output number */
#define N2 (N + 10)

/** Main function */
int main(void)
{
/* number of digits of the large number */
const int N = 10;
/* number of digits in output number */
const int N2 = N + 10;

// const char N = 50, N2 = N+10; /* length of numbers */
char txt_buffer[N + 5]; /* temporary buffer */
uint8_t number[N]; /* array to store digits of a large number */
Expand All @@ -134,7 +148,9 @@ int main(void)
{
count++;
if (get_number(fp, txt_buffer, number) != 0)
{
break;
}
add_numbers(number, sum, N);
} while (!feof(fp));

Expand Down
16 changes: 14 additions & 2 deletions project_euler/problem_23/sol1.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
#include <omp.h>
#endif

unsigned long MAX_N = 28123; /**< upper limit of numbers to check */

/**
* Returns:
* -1 if N is deficient
Expand All @@ -30,7 +28,9 @@ char get_perfect_number(unsigned long N)
sum += i;
unsigned long tmp = N / i;
if (tmp != i)
{
sum += tmp;
}
}
}

Expand All @@ -56,7 +56,9 @@ unsigned long get_next_abundant(unsigned long N)
{
unsigned long i;
for (i = N + 1; !is_abundant(i); i++)
{
;
}
return i;
}

Expand All @@ -74,22 +76,28 @@ char is_sum_of_abundant(unsigned long N)
*/
for (unsigned long i = get_next_abundant(1); i <= (N >> 1);
i = get_next_abundant(i))
{
if (is_abundant(N - i))
{
#ifdef DEBUG
printf("\t%4lu + %4lu = %4lu\n", i, N - i, N);
#endif
return 1;
}
}
return 0;
}

/** Main function */
int main(int argc, char **argv)
{
unsigned long MAX_N = 28123; /* upper limit of numbers to check */

unsigned long sum = 0;
if (argc == 2)
{
MAX_N = strtoul(argv[1], NULL, 10);
}

#ifdef _OPENMP
printf("Using OpenMP parallleization with %d threads\n",
Expand All @@ -107,13 +115,17 @@ int main(int argc, char **argv)
{
clock_t start_time = clock();
if (!is_sum_of_abundant(i))
{
sum += i;
}
clock_t end_time = clock();
total_duration += (double)(end_time - start_time) / CLOCKS_PER_SEC;

printf("... %5lu: %8lu\r", i, sum);
if (i % 100 == 0)
{
fflush(stdout);
}
}

printf("Time taken: %.4g s\n", total_duration);
Expand Down
16 changes: 14 additions & 2 deletions project_euler/problem_23/sol2.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
#include <omp.h>
#endif

long MAX_N = 28123; /**< Limit of numbers to check */

/**
* This is the global array to be used to store a flag to identify
* if a particular number is abundant (1) or not (0).
Expand All @@ -42,7 +40,9 @@ char get_perfect_number(unsigned long N)
sum += i;
unsigned long tmp = N / i;
if (tmp != i)
{
sum += tmp;
}
}
}

Expand Down Expand Up @@ -72,7 +72,9 @@ unsigned long get_next_abundant(unsigned long N)
unsigned long i;
/* keep checking successive numbers till an abundant number is found */
for (i = N + 1; !is_abundant(i); ++i)
{
;
}
return i;
}

Expand All @@ -90,22 +92,28 @@ char is_sum_of_abundant(unsigned long N)
*/
for (unsigned long i = get_next_abundant(1); i <= (N >> 1);
i = get_next_abundant(i))
{
if (is_abundant(N - i))
{
#ifdef DEBUG
printf("\t%4lu + %4lu = %4lu\n", i, N - i, N);
#endif
return 1;
}
}
return 0;
}

/** Main function */
int main(int argc, char **argv)
{
long MAX_N = 28123; /* Limit of numbers to check */

unsigned long sum = 0;
if (argc == 2)
{
MAX_N = strtoul(argv[1], NULL, 10);
}

/* byte array to store flags to identify abundant numbers
* the flags are identified by bits
Expand Down Expand Up @@ -160,10 +168,12 @@ int main(int argc, char **argv)
{
clock_t start_time1 = clock();
if (!is_sum_of_abundant(i))
{
// #ifdef _OPENMP
// #pragma omp critical
// #endif
sum += i;
}
clock_t end_time1 = clock();
#ifdef _OPENMP
#pragma omp critical
Expand All @@ -172,7 +182,9 @@ int main(int argc, char **argv)

printf("... %5lu: %8lu\r", i, sum);
if (i % 100 == 0)
{
fflush(stdout);
}
}

#ifdef DEBUG
Expand Down
6 changes: 6 additions & 0 deletions project_euler/problem_25/sol1.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ unsigned int add_numbers(unsigned char *a, unsigned char *b, unsigned char *c,
c[i] -= 10;
}
else
{
carry = 0;
}
// printf("= %d, %d\n", carry, c[i]);
}

Expand All @@ -47,7 +49,9 @@ unsigned int add_numbers(unsigned char *a, unsigned char *b, unsigned char *c,
c[i] -= 10;
}
else
{
carry = 0;
}
// printf("= %d, %d\n", carry, c[i]);
i++;
}
Expand Down Expand Up @@ -103,7 +107,9 @@ int main(int argc, char *argv[])
// putchar('\n');

if (digit_count == MAX_DIGITS)
{
break;
}
memcpy(fn, fn1, MAX_DIGITS);
memcpy(fn1, sum, MAX_DIGITS);
index++;
Expand Down
24 changes: 16 additions & 8 deletions project_euler/problem_401/sol1.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
#include <omp.h>
#endif

#define MOD (uint64_t)1e9 /**< modulo limit */
#define MAX_L 5000 /**< chunk size of array allocation */
#define MOD_LIMIT (uint64_t)1e9 /**< modulo limit */
#define MAX_LENGTH 5000 /**< chunk size of array allocation */

/**
* Check if a number is present in given array
Expand All @@ -29,8 +29,12 @@ char is_in(uint64_t N, uint64_t *D, uint64_t L)
{
uint64_t i;
for (i = 0; i < L; i++)
{
if (D[i] == N)
{
return 1;
}
}
return 0;
}

Expand Down Expand Up @@ -73,8 +77,10 @@ uint64_t get_divisors(uint64_t N, uint64_t *D)
}
}

if (num == MAX_L) // limit of array reached, allocate more space
D = (uint64_t *)realloc(D, MAX_L * sizeof(uint64_t) << 1);
if (num == MAX_LENGTH)
{ // limit of array reached, allocate more space
D = (uint64_t *)realloc(D, MAX_LENGTH * sizeof(uint64_t) << 1);
}
}
return num;
}
Expand All @@ -88,17 +94,17 @@ uint64_t sigma2(uint64_t N)
{
uint64_t sum = 0, L;
int64_t i;
uint64_t *D = (uint64_t *)malloc(MAX_L * sizeof(uint64_t));
uint64_t *D = (uint64_t *)malloc(MAX_LENGTH * sizeof(uint64_t));

L = get_divisors(N, D);
for (i = 1; i < L; i++)
{
uint64_t DD = (D[i] * D[i]) % MOD;
uint64_t DD = (D[i] * D[i]) % MOD_LIMIT;
sum += DD;
}

free(D);
return sum % MOD;
return sum % MOD_LIMIT;
}

/**
Expand All @@ -119,7 +125,7 @@ uint64_t sigma(uint64_t N)
s = sigma2(i);
sum += s;
}
return sum % MOD;
return sum % MOD_LIMIT;
}

/** Main function */
Expand All @@ -128,7 +134,9 @@ int main(int argc, char **argv)
uint64_t N = 1000;

if (argc == 2)
{
N = strtoll(argv[1], NULL, 10);
}
else if (argc > 2)
{
fprintf(stderr, "Wrong number of input arguments!\n");
Expand Down

0 comments on commit d19a3a7

Please sign in to comment.