diff --git a/project_euler/problem_1/sol1.c b/project_euler/problem_1/sol1.c index 388ea996..c91a824a 100644 --- a/project_euler/problem_1/sol1.c +++ b/project_euler/problem_1/sol1.c @@ -1,14 +1,20 @@ -/*An Efficient code to print all the sum of all numbers that are multiples of 3 - * & 5 below N.*/ +/** + * \file + * \brief [Problem 1](https://projecteuler.net/problem=1) solution + * + * An Efficient code to print all the sum of all numbers that are multiples of 3 + * & 5 below N. + */ #include +/** Main function */ int main() { int t; printf("Enter number of times you want to try"); scanf("%d", &t); - while (t--) + while (t--) // while t > 0, decrement 't' before every iteration { unsigned long long N, p = 0, sum = 0; printf("Enter the value of N "); diff --git a/project_euler/problem_1/sol2.c b/project_euler/problem_1/sol2.c index 78d4585c..d78c22d5 100644 --- a/project_euler/problem_1/sol2.c +++ b/project_euler/problem_1/sol2.c @@ -1,14 +1,17 @@ -/* -If we list all the natural numbers below 10 that are multiples of 3 or 5, -we get 3,5,6 and 9. The sum of these multiples is 23. -Find the sum of all the multiples of 3 or 5 below N. -''' -''' -This solution is based on the pattern that the successive numbers in the series -follow: 0+3,+2,+1,+3,+1,+2,+3. -*/ +/** + * \file + * \brief [Problem 1](https://projecteuler.net/problem=1) solution + * + * If we list all the natural numbers below 10 that are multiples of 3 or 5, + * we get 3,5,6 and 9. The sum of these multiples is 23. + * Find the sum of all the multiples of 3 or 5 below N. + * + * This solution is based on the pattern that the successive numbers in the + * series follow: 0+3,+2,+1,+3,+1,+2,+3. + */ #include +/** Main function */ int main() { int n = 0; diff --git a/project_euler/problem_1/sol3.c b/project_euler/problem_1/sol3.c index 87f51a90..c4c4dc74 100644 --- a/project_euler/problem_1/sol3.c +++ b/project_euler/problem_1/sol3.c @@ -1,14 +1,16 @@ -/* -If we list all the natural numbers below 10 that are multiples of 3 or 5, -we get 3,5,6 and 9. The sum of these multiples is 23. -Find the sum of all the multiples of 3 or 5 below N. -''' -''' -This solution is based on the pattern that the successive numbers in the series -follow: 0+3,+2,+1,+3,+1,+2,+3. -*/ +/** + * \file + * \brief [Problem 1](https://projecteuler.net/problem=1) solution. + * This solution is based on the pattern that the successive numbers in the + * series follow: 0+3,+2,+1,+3,+1,+2,+3. + * + * If we list all the natural numbers below 10 that are multiples of 3 or 5, + * we get 3,5,6 and 9. The sum of these multiples is 23. + * Find the sum of all the multiples of 3 or 5 below N. + */ #include +/** Main function */ int main() { int n = 0; @@ -49,4 +51,5 @@ int main() } printf("%d\n", sum); + return 0; } \ No newline at end of file diff --git a/project_euler/problem_1/sol4.c b/project_euler/problem_1/sol4.c index 7f1fd6bb..3477412c 100644 --- a/project_euler/problem_1/sol4.c +++ b/project_euler/problem_1/sol4.c @@ -1,8 +1,14 @@ -/*An Efficient code to print all the sum of all numbers that are multiples of 3 - * & 5 below N.*/ +/** + * \file + * \brief [Problem 1](https://projecteuler.net/problem=1) solution + * + * An Efficient code to print all the sum of all numbers that are multiples of 3 + * & 5 below N. + */ #include +/** Main function */ int main() { int t; diff --git a/project_euler/problem_10/sol1.c b/project_euler/problem_10/sol1.c index 08351892..8e6976a2 100644 --- a/project_euler/problem_10/sol1.c +++ b/project_euler/problem_10/sol1.c @@ -1,19 +1,25 @@ +/** + * \file + * \brief [Problem 10](https://projecteuler.net/problem=10) solution + */ #include #include #include -char is_prime(long n) +/** Function to check if a number is prime */ +char is_prime(unsigned long n) { - for (long i = 2; i < sqrtl(n) + 1; i++) + for (unsigned long i = 2; i < sqrtl(n) + 1; i++) if (n % i == 0) return 0; return 1; } -long long sum_of_primes(long N) +/** Computes sum of prime numbers less than N */ +unsigned long long sum_of_primes(unsigned long N) { - long long sum = 2; + unsigned long long sum = 2; for (long i = 3; i < N; i += 2) /* skip even numbers */ if (is_prime(i)) @@ -22,14 +28,15 @@ long long sum_of_primes(long N) return sum; } +/** Main function */ int main(int argc, char *argv[]) { - long n = 100; + unsigned long n = 100; if (argc == 2) /* if command line argument is provided */ n = atol(argv[1]); /* use that as the upper limit */ - printf("%ld: %lld\n", n, sum_of_primes(n)); + printf("%ld: %llu\n", n, sum_of_primes(n)); return 0; } \ No newline at end of file diff --git a/project_euler/problem_10/sol2.c b/project_euler/problem_10/sol2.c index 6a484bdc..6926ae06 100644 --- a/project_euler/problem_10/sol2.c +++ b/project_euler/problem_10/sol2.c @@ -1,7 +1,12 @@ +/** + * \file + * \brief [Problem 10](https://projecteuler.net/problem=10) solution + */ #include #include #include +/** Main function */ int main(int argc, char *argv[]) { long n = 100; diff --git a/project_euler/problem_12/sol1.c b/project_euler/problem_12/sol1.c index f0492780..a1e1fb2c 100644 --- a/project_euler/problem_12/sol1.c +++ b/project_euler/problem_12/sol1.c @@ -1,15 +1,21 @@ +/** + * \file + * \brief [Problem 11](https://projecteuler.net/problem=11) solution + */ #include #include #include +/** + * Get number of divisors of a given number + * + * If \f$x = a \times b\f$, then both \f$a\f$ and \f$b\f$ are divisors of + * \f$x\f$. Since multiplication is commutative, we only need to search till a + * maximum of \f$a=b = a^2\f$ i.e., till \f$\sqrt{x}\f$. At every integer till + * then, there are eaxctly 2 divisors and at \f$a=b\f$, there is only one + * divisor. + */ long count_divisors(long long n) -/* - If x = a * b, then both a and b are divisors of x. - Since multiplication is commutative, we only need to search - till a maximum of a=b = a^2 i.e., till sqrt(x). - At every integer till then, there are eaxctly 2 divisors - and at a=b, there is only one divisor. -*/ { long num_divisors = 0; @@ -22,6 +28,7 @@ long count_divisors(long long n) return num_divisors; } +/** Main function */ int main(int argc, char **argv) { int MAX_DIVISORS = 500; @@ -44,4 +51,4 @@ int main(int argc, char **argv) MAX_DIVISORS, triangle_number); return 0; -} \ No newline at end of file +} diff --git a/project_euler/problem_2/so1.c b/project_euler/problem_2/so1.c index 33745fa6..2b73325a 100644 --- a/project_euler/problem_2/so1.c +++ b/project_euler/problem_2/so1.c @@ -1,13 +1,19 @@ -/* -Problem: -Each new term in the Fibonacci sequence is generated by adding the previous two -terms. By starting with 1 and 2, the first 10 terms will be: -1,2,3,5,8,13,21,34,55,89,.. -By considering the terms in the Fibonacci sequence whose values do not exceed n, -find the sum of the even-valued terms. e.g. for n=10, we have {2,8}, sum is 10. -*/ +/** + * \file + * \brief [Problem 2](https://projecteuler.net/problem=2) solution + * + * Problem: + * + * Each new term in the Fibonacci sequence is generated by adding the previous + * two terms. By starting with 1 and 2, the first 10 terms will be: + * `1,2,3,5,8,13,21,34,55,89,..` + * By considering the terms in the Fibonacci sequence whose values do not exceed + * n, find the sum of the even-valued terms. e.g. for n=10, we have {2,8}, sum + * is 10. + */ #include +/** Main function */ int main() { int n = 0; @@ -27,4 +33,5 @@ int main() } printf("%d\n", sum); + return 0; } \ No newline at end of file diff --git a/project_euler/problem_3/sol1.c b/project_euler/problem_3/sol1.c index 8fd37cba..24872476 100644 --- a/project_euler/problem_3/sol1.c +++ b/project_euler/problem_3/sol1.c @@ -1,13 +1,18 @@ -/* -Problem: -The prime factors of 13195 are 5,7,13 and 29. What is the largest prime factor -of a given number N? e.g. for 10, largest prime factor = 5. For 17, largest -prime factor = 17. -*/ +/** + * \file + * \brief [Problem 3](https://projecteuler.net/problem=3) solution + * + * Problem: + * + * The prime factors of 13195 are 5,7,13 and 29. What is the largest prime + * factor of a given number N? e.g. for 10, largest prime factor = 5. For 17, + * largest prime factor = 17. + */ #include #include -int isprime(int no) +/** Check if the given number is prime */ +char isprime(int no) { int sq; @@ -30,6 +35,7 @@ int isprime(int no) return 1; } +/** Main function */ int main() { int maxNumber = 0; @@ -69,4 +75,5 @@ int main() printf("%d\n", maxNumber); } } + return 0; } \ No newline at end of file diff --git a/project_euler/problem_3/sol2.c b/project_euler/problem_3/sol2.c index f9cce337..39eefb8c 100644 --- a/project_euler/problem_3/sol2.c +++ b/project_euler/problem_3/sol2.c @@ -1,11 +1,16 @@ -/* -Problem: -The prime factors of 13195 are 5,7,13 and 29. What is the largest prime factor -of a given number N? e.g. for 10, largest prime factor = 5. For 17, largest -prime factor = 17. -*/ +/** + * \file + * \brief [Problem 3](https://projecteuler.net/problem=3) solution + * + * Problem: + * + * The prime factors of 13195 are 5,7,13 and 29. What is the largest prime + * factor of a given number N? e.g. for 10, largest prime factor = 5. For 17, + * largest prime factor = 17. + */ #include +/** Main function */ int main() { int n = 0; @@ -24,4 +29,5 @@ int main() if (n > 1) prime = n; printf("%d\n", prime); + return 0; } \ No newline at end of file diff --git a/project_euler/problem_4/sol.c b/project_euler/problem_4/sol.c index ba5677a8..a445d936 100644 --- a/project_euler/problem_4/sol.c +++ b/project_euler/problem_4/sol.c @@ -1,5 +1,14 @@ +/** + * \file + * \brief [Problem 4](https://projecteuler.net/problem=4) solution + */ #include +/** Check if number is palindromic + * \param[in] n number to check + * \returns 1 if palindromic + * \returns 0 if not palindromic + */ int is_palindromic(unsigned int n) { unsigned int reversed = 0, t = n; @@ -12,6 +21,7 @@ int is_palindromic(unsigned int n) return reversed == n; } +/** Main function */ int main(void) { unsigned int i, j, max = 0; diff --git a/project_euler/problem_401/sol1.c b/project_euler/problem_401/sol1.c index 3e090a9b..43eee4f0 100644 --- a/project_euler/problem_401/sol1.c +++ b/project_euler/problem_401/sol1.c @@ -1,3 +1,9 @@ +/** + * \file + * \brief [Problem 401](https://projecteuler.net/problem=401) solution + * + * Sum of squares of divisors + */ #include #include #include @@ -8,9 +14,17 @@ #include #endif -#define MOD (uint64_t)1e9 -#define MAX_L 5000 +#define MOD (uint64_t)1e9 /**< modulo limit */ +#define MAX_L 5000 /**< chunk size of array allocation */ +/** + * Check if a number is present in given array + * \param[in] N number to check + * \param[in] D array to check + * \param[in] L length of array + * \returns 1 if present + * \returns 0 if absent + */ char is_in(uint64_t N, uint64_t *D, uint64_t L) { uint64_t i; @@ -20,6 +34,12 @@ char is_in(uint64_t N, uint64_t *D, uint64_t L) return 0; } +/** + * Get all integer divisors of a number + * \param[in] N number to find divisors for + * \param[out] D array to store divisors in + * \returns number of divisors found + */ uint64_t get_divisors(uint64_t N, uint64_t *D) { uint64_t q, r; @@ -31,43 +51,49 @@ uint64_t get_divisors(uint64_t N, uint64_t *D) return 1; } + // search till sqrt(N) + // because after this, the pair of divisors will repeat themselves for (i = 1; i * i <= N + 1; i++) { - r = N % i; + r = N % i; // get reminder + // reminder = 0 if 'i' is a divisor of 'N' if (r == 0) { q = N / i; - if (!is_in(i, D, num)) + if (!is_in(i, D, num)) // if divisor was already stored { D[num] = i; num++; } - if (!is_in(q, D, num)) + if (!is_in(q, D, num)) // if divisor was already stored { D[num] = q; num++; } } - if (num == MAX_L) + + if (num == MAX_L) // limit of array reached, allocate more space D = (uint64_t *)realloc(D, MAX_L * sizeof(uint64_t) << 1); } return num; } /** - * sum of squares of all integer factors - **/ + * compute sum of squares of all integer factors of a number + * \param[in] N + * \returns sum of squares + */ uint64_t sigma2(uint64_t N) { - uint64_t sum = 0, DD, L; + uint64_t sum = 0, L; int64_t i; uint64_t *D = (uint64_t *)malloc(MAX_L * sizeof(uint64_t)); L = get_divisors(N, D); for (i = 1; i < L; i++) { - DD = (D[i] * D[i]) % MOD; + uint64_t DD = (D[i] * D[i]) % MOD; sum += DD; } @@ -78,13 +104,14 @@ uint64_t sigma2(uint64_t N) /** * sum of squares of factors of numbers * from 1 thru N - **/ + */ uint64_t sigma(uint64_t N) { uint64_t s, sum = 0; int64_t i; #ifdef _OPENMP +// parallelize on threads #pragma omp parallel for reduction(+ : sum) #endif for (i = 0; i <= N; i++) @@ -95,15 +122,17 @@ uint64_t sigma(uint64_t N) return sum % MOD; } +/** Main function */ int main(int argc, char **argv) { uint64_t N = 1000; if (argc == 2) N = strtoll(argv[1], NULL, 10); - else if (argc > 2) + else if (N > 2) { fprintf(stderr, "Wrong number of input arguments!\n"); + printf("Usage:\t ./sol1.c [N=1000]"); return -1; } diff --git a/project_euler/problem_5/sol.c b/project_euler/problem_5/sol.c index b3190d2e..c36acbc4 100644 --- a/project_euler/problem_5/sol.c +++ b/project_euler/problem_5/sol.c @@ -1,5 +1,13 @@ +/** + * \file + * \brief [Problem 5](https://projecteuler.net/problem=5) solution + */ #include +/** Compute [Greatest Common Divisor + * (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of two numbers + * using Euclids algorithm + */ unsigned long gcd(unsigned long a, unsigned long b) { unsigned long r; @@ -17,12 +25,16 @@ unsigned long gcd(unsigned long a, unsigned long b) return b; } +/** Compute [Least Common Multiple + * (LCM)](https://en.wikipedia.org/wiki/Least_common_multiple) of two numbers + */ unsigned long lcm(unsigned long a, unsigned long b) { unsigned long long p = (unsigned long long)a * b; return p / gcd(a, b); } +/** Main function */ int main(void) { unsigned long ans = 1; diff --git a/project_euler/problem_6/sol.c b/project_euler/problem_6/sol.c index 3dd68bd9..5530880a 100644 --- a/project_euler/problem_6/sol.c +++ b/project_euler/problem_6/sol.c @@ -1,5 +1,10 @@ +/** + * \file + * \brief [Problem 6](https://projecteuler.net/problem=6) solution + */ #include +/** Main function */ int main(void) { unsigned s1 = 0, s2 = 0, i; diff --git a/project_euler/problem_7/sol.c b/project_euler/problem_7/sol.c index 72cc6622..917aac10 100644 --- a/project_euler/problem_7/sol.c +++ b/project_euler/problem_7/sol.c @@ -1,6 +1,11 @@ +/** + * \file + * \brief [Problem 7](https://projecteuler.net/problem=7) solution + */ #include #include +/** Main function */ int main(void) { char *sieve; @@ -9,7 +14,7 @@ int main(void) size_t n = 1000000; const unsigned target = 10001; - sieve = calloc(n, sizeof *sieve); + sieve = (char *)calloc(n, sizeof(char)); for (i = 2; i < n; i++) { if (!sieve[i]) diff --git a/project_euler/problem_8/sol1.c b/project_euler/problem_8/sol1.c index c5ce9911..4b18a60a 100644 --- a/project_euler/problem_8/sol1.c +++ b/project_euler/problem_8/sol1.c @@ -1,6 +1,17 @@ +/** + * \file + * \brief [Problem 8](https://projecteuler.net/problem=8) solution + */ #include #include +/** Compute the product of two numbers in a file + * + * \param[in] fp pointer to file that is already open + * \param[in] start_pos line number of the first numer + * \param[in] num_digits number of digits on the line to multiply + * \returns expected product + */ long long int get_product(FILE *fp, long start_pos, int num_digits) { char ch = ' '; /* temporary variable to store character read from file */ @@ -46,6 +57,7 @@ long long int get_product(FILE *fp, long start_pos, int num_digits) return prod; } +/** Main function */ int main(int argc, char *argv[]) { int position = 0; diff --git a/project_euler/problem_8/sol2.c b/project_euler/problem_8/sol2.c index 6e768fc9..fda6ab0c 100644 --- a/project_euler/problem_8/sol2.c +++ b/project_euler/problem_8/sol2.c @@ -1,7 +1,12 @@ +/** + * \file + * \brief [Problem 8](https://projecteuler.net/problem=8) solution + */ #include #include #include /* for memmove */ +/** Main function */ int main(int argc, char *argv[]) { int position = 0, num_bad_chars = 0; diff --git a/project_euler/problem_9/sol1.c b/project_euler/problem_9/sol1.c index c3b3d421..6a0a64b8 100644 --- a/project_euler/problem_9/sol1.c +++ b/project_euler/problem_9/sol1.c @@ -1,5 +1,11 @@ +/** + * \file + * \brief [Problem 9](https://projecteuler.net/problem=9) solution - A naive + * implementation + */ #include +/** Main function */ int main(void) { for (int a = 1; a < 300; a++) diff --git a/project_euler/problem_9/sol2.c b/project_euler/problem_9/sol2.c index 976655de..69153b3e 100644 --- a/project_euler/problem_9/sol2.c +++ b/project_euler/problem_9/sol2.c @@ -1,19 +1,24 @@ +/** + * \file + * \brief [Problem 9](https://projecteuler.net/problem=9) solution + * + Problem Statement: + A Pythagorean triplet is a set of three natural numbers, \f$a < b < c\f$, + for which, \f$a^2 + b^2 = c^2\f$. For example, \f$3^2 + 4^2 = 9 + 16 = 25 = + 5^2\f$. There exists exactly one Pythagorean triplet for which \f$a + b + c = + 1000\f$. Find the product abc. + + + Given \f$a^2 + b^2 = c^2\f$ and \f$a+b+c = n\f$, we can write: + \f{eqnarray*}{ + b &=& \frac{n^2 - 2an}{2n - 2a}\\ + c &=& n - a - b + \f} + */ #include #include -/** - Problem Statement: - A Pythagorean triplet is a set of three natural numbers, a < b < c, for - which, a^2 + b^2 = c^2 For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2. There exists - exactly one Pythagorean triplet for which a + b + c = 1000. Find the product - abc. - - - Given a^2 + b^2 = c^2 and a+b+c = n, we can write: - b = (n^2 - 2*a*n) / (2*n - 2*a) - c = n - a - b - **/ - +/** Main function */ int main(void) { int N = 1000;