mirror of
https://github.com/TheAlgorithms/C
synced 2024-11-21 21:11:57 +03:00
cleanup some codes for global variables and clang-tidy specs
This commit is contained in:
parent
08415b0f16
commit
d19a3a7bc8
@ -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)
|
||||
@ -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)
|
||||
@ -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;
|
||||
@ -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");
|
||||
@ -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 */
|
||||
@ -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));
|
||||
|
||||
|
@ -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
|
||||
@ -30,7 +28,9 @@ char get_perfect_number(unsigned long N)
|
||||
sum += i;
|
||||
unsigned long tmp = N / i;
|
||||
if (tmp != i)
|
||||
{
|
||||
sum += tmp;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
@ -74,6 +76,7 @@ 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
|
||||
@ -81,15 +84,20 @@ char is_sum_of_abundant(unsigned long 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",
|
||||
@ -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);
|
||||
|
@ -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).
|
||||
@ -42,7 +40,9 @@ char get_perfect_number(unsigned long N)
|
||||
sum += i;
|
||||
unsigned long tmp = N / i;
|
||||
if (tmp != i)
|
||||
{
|
||||
sum += tmp;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
@ -90,6 +92,7 @@ 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
|
||||
@ -97,15 +100,20 @@ char is_sum_of_abundant(unsigned long 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
|
||||
@ -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
|
||||
@ -172,7 +182,9 @@ int main(int argc, char **argv)
|
||||
|
||||
printf("... %5lu: %8lu\r", i, sum);
|
||||
if (i % 100 == 0)
|
||||
{
|
||||
fflush(stdout);
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef DEBUG
|
||||
|
@ -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]);
|
||||
}
|
||||
|
||||
@ -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++;
|
||||
}
|
||||
@ -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++;
|
||||
|
@ -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
|
||||
@ -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;
|
||||
}
|
||||
|
||||
@ -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;
|
||||
}
|
||||
@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -119,7 +125,7 @@ uint64_t sigma(uint64_t N)
|
||||
s = sigma2(i);
|
||||
sum += s;
|
||||
}
|
||||
return sum % MOD;
|
||||
return sum % MOD_LIMIT;
|
||||
}
|
||||
|
||||
/** Main function */
|
||||
@ -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");
|
||||
|
Loading…
Reference in New Issue
Block a user