mirror of
https://github.moeyy.xyz/https://github.com/TheAlgorithms/C.git
synced 2023-10-11 15:56:24 +08:00
documented remaining project euler programs
This commit is contained in:
parent
0c4be6aaba
commit
d902f3595f
@ -1,6 +1,6 @@
|
||||
/**
|
||||
* \file
|
||||
* \brief [Problem 11](https://projecteuler.net/problem=11) solution
|
||||
* \brief [Problem 12](https://projecteuler.net/problem=12) solution
|
||||
*/
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
|
@ -1,11 +1,15 @@
|
||||
/**
|
||||
* \file
|
||||
* \brief [Problem 13](https://projecteuler.net/problem=13) solution
|
||||
*/
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
/* Function to read the number from a file and store it in array.
|
||||
index 0 of output buffer => units place
|
||||
index 1 of output buffer => tens place and so on
|
||||
/** Function to read the number from a file and store it in array.
|
||||
\n index 0 of output buffer => units place
|
||||
\n index 1 of output buffer => tens place and so on
|
||||
i.e., index i => 10^i th place
|
||||
*/
|
||||
int get_number(FILE *fp, char *buffer, uint8_t *out_int)
|
||||
@ -73,6 +77,7 @@ int add_numbers(uint8_t *a, uint8_t *b, uint8_t N)
|
||||
return 0;
|
||||
}
|
||||
|
||||
/** Function to print a long number */
|
||||
int print_number(uint8_t *number, uint8_t N, int8_t num_digits_to_print)
|
||||
{
|
||||
uint8_t start_pos = N - 1;
|
||||
@ -101,9 +106,12 @@ 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)
|
||||
{
|
||||
// const char N = 50, N2 = N+10; /* length of numbers */
|
||||
@ -139,4 +147,4 @@ int main(void)
|
||||
|
||||
fclose(fp); /* close file */
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,20 @@
|
||||
/**
|
||||
* \file
|
||||
* \brief [Problem 14](https://projecteuler.net/problem=14) solution
|
||||
*
|
||||
* Since the computational values for each iteration step are independent,
|
||||
* we can compute them in parallel. However, the maximum values should be
|
||||
* updated in synchrony so that we do not get into a "race condition".
|
||||
*
|
||||
* To compile with supporintg gcc or clang, the flag "-fopenmp" should be
|
||||
* passes while with Microsoft C compiler, the flag "/fopenmp" should be
|
||||
* used. If you are using the provided CMAKE compilation, it will automatically
|
||||
* detect OPENMP and compile with it for you.
|
||||
*
|
||||
* Automatically detects for OPENMP using the _OPENMP macro.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#ifdef _OPENMP
|
||||
#include <omp.h>
|
||||
#endif
|
||||
@ -25,6 +39,7 @@ long long collatz(long long start_num)
|
||||
return length;
|
||||
}
|
||||
|
||||
/** Main function */
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
long long max_len = 0, max_len_num = 0;
|
||||
@ -37,17 +52,6 @@ int main(int argc, char **argv)
|
||||
printf("Maximum number: %lld\n", MAX_NUM);
|
||||
}
|
||||
|
||||
/**
|
||||
* Since the computational values for each iteration step are independent,
|
||||
* we can compute them in parallel. However, the maximum values should be
|
||||
* updated in synchrony so that we do not get into a "race condition".
|
||||
*
|
||||
* To compile with supporintg gcc or clang, the flag "-fopenmp" should be
|
||||
*passes while with Microsoft C compiler, the flag "/fopenmp" should be
|
||||
*used.
|
||||
*
|
||||
* Automatically detects for OPENMP using the _OPENMP macro.
|
||||
**/
|
||||
long long i;
|
||||
#ifdef _OPENMP
|
||||
#pragma omp parallel for shared(max_len, max_len_num) schedule(guided)
|
||||
@ -71,4 +75,4 @@ int main(int argc, char **argv)
|
||||
printf("Start: %3lld: \tLength: %5lld\n", max_len_num, max_len);
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
@ -1,3 +1,7 @@
|
||||
/**
|
||||
* \file
|
||||
* \brief [Problem 15](https://projecteuler.net/problem=15) solution
|
||||
*/
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@ -21,6 +25,7 @@ unsigned long long number_of_paths(int N)
|
||||
return path;
|
||||
}
|
||||
|
||||
/** Main function */
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int N = 20;
|
||||
@ -32,4 +37,4 @@ int main(int argc, char **argv)
|
||||
number_of_paths(N));
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
@ -1,8 +1,13 @@
|
||||
/**
|
||||
* \file
|
||||
* \brief [Problem 16](https://projecteuler.net/problem=16) solution
|
||||
*/
|
||||
#include <math.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
/** Main function */
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
const double tmp = log(10) / log(2); /* required to get number of digits */
|
||||
@ -54,4 +59,4 @@ int main(int argc, char **argv)
|
||||
|
||||
free(digits);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
@ -1,9 +1,13 @@
|
||||
/**
|
||||
* \file
|
||||
* \brief [Problem 19](https://projecteuler.net/problem=19) solution
|
||||
*/
|
||||
#include <stdio.h>
|
||||
|
||||
/**
|
||||
* returns number of days in a month.
|
||||
* Month is identified by an integer -
|
||||
* 0 = Jan and 11 = December
|
||||
* Month is identified by an integer -\n
|
||||
* > 0 = Jan and 11 = December\n
|
||||
* For February, adjust for leap year outside the function.
|
||||
**/
|
||||
char get_month_days(short month)
|
||||
@ -42,6 +46,7 @@ char is_leap_year(short year)
|
||||
}
|
||||
|
||||
#ifdef DEBUG
|
||||
/** Function to convert integer month to string */
|
||||
const char *day_string(int day)
|
||||
{
|
||||
switch (day)
|
||||
@ -66,6 +71,7 @@ const char *day_string(int day)
|
||||
}
|
||||
#endif
|
||||
|
||||
/** Main function */
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int count_sundays = 0;
|
||||
@ -120,4 +126,4 @@ int main(int argc, char **argv)
|
||||
count_sundays);
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
@ -1,3 +1,10 @@
|
||||
/**
|
||||
* \file
|
||||
* \brief [Problem 20](https://projecteuler.net/problem=20) solution
|
||||
*
|
||||
* Implementation uses a custom `big_int` structure that can store arbitrarilty
|
||||
* large integer numbers.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
@ -14,6 +21,7 @@ typedef struct _big_int
|
||||
} big_int;
|
||||
|
||||
#ifdef DEBUG
|
||||
/** print a digit from large integer */
|
||||
void print_digit(const big_int *my_int)
|
||||
{
|
||||
printf("\tValue : %d\n\tNext : %p\n\tPrev : %p\n", my_int->value,
|
||||
@ -82,6 +90,7 @@ char remove_digits(big_int *digit, int N)
|
||||
return remove_digits(digit->next_digit, 0);
|
||||
}
|
||||
|
||||
/** Main function */
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
unsigned int N = 5;
|
||||
|
@ -1,34 +1,29 @@
|
||||
/**
|
||||
* \file
|
||||
* \brief [Problem 21](https://projecteuler.net/problem=21) solution
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#ifdef _OPENMP
|
||||
#include <omp.h>
|
||||
#endif
|
||||
|
||||
/**
|
||||
* function to return the sum of proper divisors of N
|
||||
**/
|
||||
unsigned int sum_of_divisors(unsigned int N)
|
||||
unsigned long sum_of_divisors(unsigned int N)
|
||||
{
|
||||
unsigned int sum = 1; /* 1 is always a divisor */
|
||||
|
||||
unsigned long sum = 1 + N; /* 1 and itself are always a divisor */
|
||||
/* divisors are symmertically distributed about the square-root */
|
||||
for (unsigned int i = 2; (i * i) <= N; i++)
|
||||
for (unsigned int i = 2; i * i < N; i++)
|
||||
{
|
||||
if (N % i != 0)
|
||||
/* i is not a proper divisor */
|
||||
if ((N % i) != 0)
|
||||
/* i is not a divisor of N */
|
||||
continue;
|
||||
|
||||
// #ifdef DEBUG
|
||||
// printf("%4d, %4d,", i, N / i);
|
||||
// #endif
|
||||
|
||||
sum += i;
|
||||
|
||||
if (i * i == N)
|
||||
continue;
|
||||
|
||||
sum += N / i;
|
||||
sum += i + (N / i);
|
||||
}
|
||||
// #ifdef DEBUG
|
||||
// printf("\nSum of divisors of %4d: %4d\n", N, sum);
|
||||
@ -36,6 +31,7 @@ unsigned int sum_of_divisors(unsigned int N)
|
||||
return sum;
|
||||
}
|
||||
|
||||
/** Main function */
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
unsigned long sum = 0;
|
||||
@ -43,7 +39,7 @@ int main(int argc, char **argv)
|
||||
if (argc == 2)
|
||||
MAX_N = atoi(argv[1]);
|
||||
|
||||
/**
|
||||
/**<
|
||||
* We use an array of flags to check if a number at the index was:
|
||||
* not-processed = 0
|
||||
* is amicable = 1
|
||||
@ -53,9 +49,6 @@ int main(int argc, char **argv)
|
||||
|
||||
clock_t start_time = clock();
|
||||
int i;
|
||||
#ifdef _OPENMP
|
||||
#pragma omp for schedule(runtime)
|
||||
#endif
|
||||
/* there are no such numbers till 10. Lets search from there on */
|
||||
for (i = 10; i < MAX_N; i++)
|
||||
{
|
||||
|
@ -1,3 +1,7 @@
|
||||
/**
|
||||
* \file
|
||||
* \brief [Problem 22](https://projecteuler.net/problem=22) solution
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
@ -6,8 +10,8 @@
|
||||
#include <omp.h>
|
||||
#endif
|
||||
|
||||
#define MAX_NAMES 6000 /* Maximum number of names to store */
|
||||
#define MAX_NAME_LEN 20 /* Maximum length of each name */
|
||||
#define MAX_NAMES 6000 /**< Maximum number of names to store */
|
||||
#define MAX_NAME_LEN 20 /**< Maximum length of each name */
|
||||
|
||||
/**
|
||||
* Alphabetical sorting using 'shell sort' algorithm
|
||||
@ -63,6 +67,7 @@ void lazy_sort(char data[][MAX_NAME_LEN], int LEN)
|
||||
#endif
|
||||
}
|
||||
|
||||
/** Main function */
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
unsigned long COUNT = 0;
|
||||
@ -80,9 +85,9 @@ int main(int argc, char **argv)
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
/*
|
||||
* Loops to get total number of rows and columns in the file
|
||||
**/
|
||||
*/
|
||||
do
|
||||
{
|
||||
int ret = fscanf(fp, "\"%[^\",]\",", names[COUNT++]);
|
||||
|
@ -1,3 +1,7 @@
|
||||
/**
|
||||
* \file
|
||||
* \brief [Problem 23](https://projecteuler.net/problem=23) solution
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
@ -5,7 +9,7 @@
|
||||
#include <omp.h>
|
||||
#endif
|
||||
|
||||
unsigned long MAX_N = 28123;
|
||||
unsigned long MAX_N = 28123; /**< upper limit of numbers to check */
|
||||
|
||||
/**
|
||||
* Returns:
|
||||
@ -58,8 +62,8 @@ unsigned long get_next_abundant(unsigned long N)
|
||||
/**
|
||||
* check if a given number can be represented as a sum
|
||||
* of two abundant numbers.
|
||||
* 1 - if yes
|
||||
* 0 - if not
|
||||
* \returns 1 - if yes
|
||||
* \returns 0 - if not
|
||||
**/
|
||||
char is_sum_of_abundant(unsigned long N)
|
||||
{
|
||||
@ -79,6 +83,7 @@ char is_sum_of_abundant(unsigned long N)
|
||||
return 0;
|
||||
}
|
||||
|
||||
/** Main function */
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
unsigned long sum = 0;
|
||||
|
@ -1,4 +1,11 @@
|
||||
|
||||
/**
|
||||
* \file
|
||||
* \brief [Problem 23](https://projecteuler.net/problem=23) solution -
|
||||
* optimization using look-up array
|
||||
*
|
||||
* Optimization applied - compute & store abundant numbers once
|
||||
* into a look-up array.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
@ -6,12 +13,7 @@
|
||||
#include <omp.h>
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Optimization 1 - compute & store abundant numbers once
|
||||
* into a look-up array
|
||||
**/
|
||||
|
||||
long MAX_N = 28123;
|
||||
long MAX_N = 28123; /**< Limit of numbers to check */
|
||||
|
||||
/**
|
||||
* This is the global array to be used to store a flag to identify
|
||||
@ -77,8 +79,8 @@ unsigned long get_next_abundant(unsigned long N)
|
||||
/**
|
||||
* check if a given number can be represented as a sum
|
||||
* of two abundant numbers.
|
||||
* 1 - if yes
|
||||
* 0 - if not
|
||||
* \returns 1 - if yes
|
||||
* \returns 0 - if not
|
||||
**/
|
||||
char is_sum_of_abundant(unsigned long N)
|
||||
{
|
||||
@ -98,6 +100,7 @@ char is_sum_of_abundant(unsigned long N)
|
||||
return 0;
|
||||
}
|
||||
|
||||
/** Main function */
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
unsigned long sum = 0;
|
||||
|
@ -1,13 +1,18 @@
|
||||
/**
|
||||
* \file
|
||||
* \brief [Problem 25](https://projecteuler.net/problem=25) solution implemented
|
||||
* using arbitrarily large numbers represented as arrays
|
||||
*/
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
#define MAX_DIGITS 1000
|
||||
#define MAX_DIGITS 1000 /**< maximum number of digits */
|
||||
|
||||
/**
|
||||
* Function to add arbitraty length decimal integers stored in an array.
|
||||
* Function to add arbitraty length decimal integers stored in an array.\n
|
||||
* a + b = c = new b
|
||||
**/
|
||||
unsigned int add_numbers(unsigned char *a, unsigned char *b, unsigned char *c,
|
||||
@ -48,6 +53,7 @@ unsigned int add_numbers(unsigned char *a, unsigned char *b, unsigned char *c,
|
||||
return i;
|
||||
}
|
||||
|
||||
/** Print a large number */
|
||||
int print_number(unsigned char *number, int N)
|
||||
{
|
||||
int start_pos = N - 1;
|
||||
@ -62,6 +68,7 @@ int print_number(unsigned char *number, int N)
|
||||
return 0;
|
||||
}
|
||||
|
||||
/** Get number of digits in a large number */
|
||||
unsigned int get_digits(unsigned char *number)
|
||||
{
|
||||
unsigned int digits = MAX_DIGITS;
|
||||
@ -70,6 +77,7 @@ unsigned int get_digits(unsigned char *number)
|
||||
return digits;
|
||||
}
|
||||
|
||||
/** Main function */
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
unsigned char
|
||||
|
@ -1,3 +1,7 @@
|
||||
/**
|
||||
* \file
|
||||
* \brief [Problem 26](https://projecteuler.net/problem=26) solution
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
@ -6,14 +10,17 @@
|
||||
#include <omp.h>
|
||||
#endif
|
||||
|
||||
#define MAX_DENO 2000
|
||||
#define MAX_LEN (MAX_DENO + 10)
|
||||
#define MAX_DENO 2000 /**< limit of unit fractions */
|
||||
#define MAX_LEN \
|
||||
(MAX_DENO + 10) /**< length of resulting recurring fraction number */
|
||||
|
||||
/** comparison function for use with internal `qsort` algorithm */
|
||||
int compare(const void *a, const void *b)
|
||||
{
|
||||
return (*(unsigned short *)a - *(unsigned short *)b);
|
||||
}
|
||||
|
||||
/** Main function */
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
unsigned short max_digits = 0, max_idx_number = 0;
|
||||
|
Loading…
Reference in New Issue
Block a user