mirror of
https://github.com/TheAlgorithms/C
synced 2024-11-22 05:21:49 +03:00
document project euler till prob 12
This commit is contained in:
parent
8242411530
commit
dd40af2736
@ -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 <stdio.h>
|
||||
|
||||
/** 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 ");
|
||||
|
@ -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 <stdio.h>
|
||||
|
||||
/** Main function */
|
||||
int main()
|
||||
{
|
||||
int n = 0;
|
||||
|
@ -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 <stdio.h>
|
||||
|
||||
/** Main function */
|
||||
int main()
|
||||
{
|
||||
int n = 0;
|
||||
@ -49,4 +51,5 @@ int main()
|
||||
}
|
||||
|
||||
printf("%d\n", sum);
|
||||
return 0;
|
||||
}
|
@ -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 <stdio.h>
|
||||
|
||||
/** Main function */
|
||||
int main()
|
||||
{
|
||||
int t;
|
||||
|
@ -1,19 +1,25 @@
|
||||
/**
|
||||
* \file
|
||||
* \brief [Problem 10](https://projecteuler.net/problem=10) solution
|
||||
*/
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
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;
|
||||
}
|
@ -1,7 +1,12 @@
|
||||
/**
|
||||
* \file
|
||||
* \brief [Problem 10](https://projecteuler.net/problem=10) solution
|
||||
*/
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
/** Main function */
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
long n = 100;
|
||||
|
@ -1,15 +1,21 @@
|
||||
/**
|
||||
* \file
|
||||
* \brief [Problem 11](https://projecteuler.net/problem=11) solution
|
||||
*/
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
/**
|
||||
* 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;
|
||||
|
@ -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 <stdio.h>
|
||||
|
||||
/** Main function */
|
||||
int main()
|
||||
{
|
||||
int n = 0;
|
||||
@ -27,4 +33,5 @@ int main()
|
||||
}
|
||||
|
||||
printf("%d\n", sum);
|
||||
return 0;
|
||||
}
|
@ -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 <math.h>
|
||||
#include <stdio.h>
|
||||
|
||||
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;
|
||||
}
|
@ -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 <stdio.h>
|
||||
|
||||
/** Main function */
|
||||
int main()
|
||||
{
|
||||
int n = 0;
|
||||
@ -24,4 +29,5 @@ int main()
|
||||
if (n > 1)
|
||||
prime = n;
|
||||
printf("%d\n", prime);
|
||||
return 0;
|
||||
}
|
@ -1,5 +1,14 @@
|
||||
/**
|
||||
* \file
|
||||
* \brief [Problem 4](https://projecteuler.net/problem=4) solution
|
||||
*/
|
||||
#include <stdio.h>
|
||||
|
||||
/** 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;
|
||||
|
@ -1,3 +1,9 @@
|
||||
/**
|
||||
* \file
|
||||
* \brief [Problem 401](https://projecteuler.net/problem=401) solution
|
||||
*
|
||||
* Sum of squares of divisors
|
||||
*/
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@ -8,9 +14,17 @@
|
||||
#include <omp.h>
|
||||
#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;
|
||||
}
|
||||
|
||||
|
@ -1,5 +1,13 @@
|
||||
/**
|
||||
* \file
|
||||
* \brief [Problem 5](https://projecteuler.net/problem=5) solution
|
||||
*/
|
||||
#include <stdio.h>
|
||||
|
||||
/** 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;
|
||||
|
@ -1,5 +1,10 @@
|
||||
/**
|
||||
* \file
|
||||
* \brief [Problem 6](https://projecteuler.net/problem=6) solution
|
||||
*/
|
||||
#include <stdio.h>
|
||||
|
||||
/** Main function */
|
||||
int main(void)
|
||||
{
|
||||
unsigned s1 = 0, s2 = 0, i;
|
||||
|
@ -1,6 +1,11 @@
|
||||
/**
|
||||
* \file
|
||||
* \brief [Problem 7](https://projecteuler.net/problem=7) solution
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
/** 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])
|
||||
|
@ -1,6 +1,17 @@
|
||||
/**
|
||||
* \file
|
||||
* \brief [Problem 8](https://projecteuler.net/problem=8) solution
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
/** 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;
|
||||
|
@ -1,7 +1,12 @@
|
||||
/**
|
||||
* \file
|
||||
* \brief [Problem 8](https://projecteuler.net/problem=8) solution
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h> /* for memmove */
|
||||
|
||||
/** Main function */
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int position = 0, num_bad_chars = 0;
|
||||
|
@ -1,5 +1,11 @@
|
||||
/**
|
||||
* \file
|
||||
* \brief [Problem 9](https://projecteuler.net/problem=9) solution - A naive
|
||||
* implementation
|
||||
*/
|
||||
#include <stdio.h>
|
||||
|
||||
/** Main function */
|
||||
int main(void)
|
||||
{
|
||||
for (int a = 1; a < 300; a++)
|
||||
|
@ -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 <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
/**
|
||||
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;
|
||||
|
Loading…
Reference in New Issue
Block a user