Add more solutions
Added solutions for problems from 41 to 5, both in C and python
This commit is contained in:
48
C/p041.c
Normal file
48
C/p041.c
Normal file
@ -0,0 +1,48 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
#include <time.h>
|
||||
#include "projecteuler.h"
|
||||
|
||||
int count_digits(int n);
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int i, found = 0;
|
||||
double elapsed;
|
||||
struct timespec start, end;
|
||||
|
||||
clock_gettime(CLOCK_MONOTONIC, &start);
|
||||
|
||||
for(i = 7654321; !found && i > 0; i -= 2)
|
||||
{
|
||||
|
||||
if(is_pandigital(i, count_digits(i)) && is_prime(i))
|
||||
{
|
||||
printf("Project Euler, Problem 41\n");
|
||||
printf("Answer: %d\n", i);
|
||||
found = 1;
|
||||
}
|
||||
}
|
||||
|
||||
clock_gettime(CLOCK_MONOTONIC, &end);
|
||||
|
||||
elapsed = (end.tv_sec - start.tv_sec) + (double)(end.tv_nsec - start.tv_nsec) / 1000000000;
|
||||
|
||||
printf("Elapsed time: %.9lf seconds\n", elapsed);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int count_digits(int n)
|
||||
{
|
||||
int i = 0;
|
||||
|
||||
while(n > 0)
|
||||
{
|
||||
i++;
|
||||
n /= 10;
|
||||
}
|
||||
|
||||
return i;
|
||||
}
|
96
C/p042.c
Normal file
96
C/p042.c
Normal file
@ -0,0 +1,96 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
int is_triang(int n);
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int i, j, n, len, value, count = 0;
|
||||
char *line, **words;
|
||||
double elapsed;
|
||||
FILE *fp;
|
||||
struct timespec start, end;
|
||||
|
||||
clock_gettime(CLOCK_MONOTONIC, &start);
|
||||
|
||||
if((fp = fopen("words.txt", "r")) == NULL)
|
||||
{
|
||||
fprintf(stderr, "Error while opening file %s\n", "words.txt");
|
||||
return 1;
|
||||
}
|
||||
|
||||
fscanf(fp, "%ms", &line);
|
||||
|
||||
fclose(fp);
|
||||
|
||||
n = 1;
|
||||
len = strlen(line);
|
||||
|
||||
for(i = 0; i < len; i++)
|
||||
{
|
||||
if(line[i] == ',')
|
||||
{
|
||||
n++;
|
||||
}
|
||||
}
|
||||
|
||||
if((words = (char **)malloc(n*sizeof(char *))) == NULL)
|
||||
{
|
||||
fprintf(stderr, "Error while allocating memory\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
words[0] = strtok(line, ",\"");
|
||||
|
||||
for(i = 1; i < n; i++)
|
||||
{
|
||||
words[i] = strtok(NULL, ",\"");
|
||||
}
|
||||
|
||||
for(i = 0; i < n; i++)
|
||||
{
|
||||
value = 0;
|
||||
len = strlen(words[i]);
|
||||
|
||||
for(j = 0; j < len; j++)
|
||||
{
|
||||
value += (words[i][j] - 'A' + 1);
|
||||
}
|
||||
|
||||
if(is_triang(value))
|
||||
{
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
free(line);
|
||||
free(words);
|
||||
|
||||
clock_gettime(CLOCK_MONOTONIC, &end);
|
||||
|
||||
elapsed = (end.tv_sec-start.tv_sec) + (double)(end.tv_nsec - start.tv_nsec) / 1000000000;
|
||||
|
||||
printf("Project Euler, Problem 42\n");
|
||||
printf("Answer: %d\n", count);
|
||||
|
||||
printf("Elapsed time: %.9lf seconds\n", elapsed);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int is_triang(int n)
|
||||
{
|
||||
int i, j;
|
||||
|
||||
for(i = 1, j = 1; j <= n; i++, j += i)
|
||||
{
|
||||
if(n == j)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
118
C/p043.c
Normal file
118
C/p043.c
Normal file
@ -0,0 +1,118 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
void permute(int *v, int i, int n);
|
||||
void swap(int *a, int *b);
|
||||
int has_property(int *v);
|
||||
|
||||
long int sum = 0;
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int n[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
|
||||
double elapsed;
|
||||
struct timespec start, end;
|
||||
|
||||
clock_gettime(CLOCK_MONOTONIC, &start);
|
||||
|
||||
permute(n, 0, 9);
|
||||
|
||||
clock_gettime(CLOCK_MONOTONIC, &end);
|
||||
|
||||
elapsed = (end.tv_sec - start.tv_sec) + (double)(end.tv_nsec - start.tv_nsec) / 1000000000;
|
||||
|
||||
printf("Project Euler, Problem 43\n");
|
||||
printf("Answer: %ld\n", sum);
|
||||
|
||||
printf("Elapsed time: %.9lf seconds\n", elapsed);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void permute(int *v, int i, int n)
|
||||
{
|
||||
int j;
|
||||
|
||||
if(i == n)
|
||||
{
|
||||
if(has_property(v))
|
||||
{
|
||||
sum += v[0] * 1e9 + v[1] * 1e8 + v[2] * 1e7 + v[3] * 1e6 + v[4] * 1e5 + v[5] * 1e4 + v[6] * 1e3 + v[7] * 1e2 + v[8] * 1e1 + v[9];
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for(j = i; j <= n; j++)
|
||||
{
|
||||
swap((v+i), (v+j));
|
||||
permute(v, i+1, n);
|
||||
swap((v+i), (v+j));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void swap(int *a, int *b)
|
||||
{
|
||||
int tmp;
|
||||
|
||||
tmp = *a;
|
||||
*a = *b;
|
||||
*b = tmp;
|
||||
}
|
||||
|
||||
int has_property(int *v)
|
||||
{
|
||||
long int value;
|
||||
|
||||
value = v[1] * 100 + v[2] * 10 + v[3];
|
||||
|
||||
if(value % 2 != 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
value = v[2] * 100 + v[3] * 10 + v[4];
|
||||
|
||||
if(value % 3 != 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
value = v[3] * 100 + v[4] * 10 + v[5];
|
||||
|
||||
if(value % 5 != 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
value = v[4] * 100 + v[5] * 10 + v[6];
|
||||
|
||||
if(value % 7 != 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
value = v[5] * 100 + v[6] * 10 + v[7];
|
||||
|
||||
if(value % 11 != 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
value = v[6] * 100 + v[7] * 10 + v[8];
|
||||
|
||||
if(value %13 != 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
value = v[7] * 100 + v[8] * 10 + v[9];
|
||||
|
||||
if(value % 17 != 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
44
C/p044.c
Normal file
44
C/p044.c
Normal file
@ -0,0 +1,44 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
#include <time.h>
|
||||
#include "projecteuler.h"
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int n, m, pn, pm, found = 0;
|
||||
double elapsed;
|
||||
struct timespec start, end;
|
||||
|
||||
clock_gettime(CLOCK_MONOTONIC, &start);
|
||||
|
||||
n = 2;
|
||||
|
||||
while(!found)
|
||||
{
|
||||
pn = n * (3 * n - 1) / 2;
|
||||
|
||||
for(m = 1; m < n; m++)
|
||||
{
|
||||
pm = m * (3 * m - 1) / 2;
|
||||
|
||||
if(is_pentagonal(pn+pm) && is_pentagonal(pn-pm))
|
||||
{
|
||||
found = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
n++;
|
||||
}
|
||||
|
||||
clock_gettime(CLOCK_MONOTONIC, &end);
|
||||
|
||||
elapsed = (end.tv_sec - start.tv_sec) + (double)(end.tv_nsec - start.tv_nsec) / 1000000000;
|
||||
|
||||
printf("Project Euler, Problem 44\n");
|
||||
printf("Answer: %d\n", pn-pm);
|
||||
|
||||
printf("Elapsed time: %.9lf seconds\n", elapsed);
|
||||
|
||||
return 0;
|
||||
}
|
39
C/p045.c
Normal file
39
C/p045.c
Normal file
@ -0,0 +1,39 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
#include <time.h>
|
||||
#include "projecteuler.h"
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int found = 0;
|
||||
long int i, n;
|
||||
double elapsed;
|
||||
struct timespec start, end;
|
||||
|
||||
clock_gettime(CLOCK_MONOTONIC, &start);
|
||||
|
||||
i = 143;
|
||||
|
||||
while(!found)
|
||||
{
|
||||
i++;
|
||||
n = i * (2 * i - 1);
|
||||
|
||||
if(is_pentagonal(n))
|
||||
{
|
||||
found = 1;
|
||||
}
|
||||
}
|
||||
|
||||
clock_gettime(CLOCK_MONOTONIC, &end);
|
||||
|
||||
elapsed = (end.tv_sec - start.tv_sec) + (double)(end.tv_nsec - start.tv_nsec) / 1000000000;
|
||||
|
||||
printf("Project Euler, Problem 45\n");
|
||||
printf("Answer: %ld\n", n);
|
||||
|
||||
printf("Elapsed time: %.9lf seconds\n", elapsed);
|
||||
|
||||
return 0;
|
||||
}
|
73
C/p046.c
Normal file
73
C/p046.c
Normal file
@ -0,0 +1,73 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
#include <time.h>
|
||||
#include "projecteuler.h"
|
||||
|
||||
#define N 10000
|
||||
|
||||
int goldbach(int n);
|
||||
|
||||
int *primes;
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int i, found = 0;
|
||||
double elapsed;
|
||||
struct timespec start, end;
|
||||
|
||||
clock_gettime(CLOCK_MONOTONIC, &start);
|
||||
|
||||
primes = sieve(N);
|
||||
|
||||
for(i = 3; !found && i < N; i += 2)
|
||||
{
|
||||
if(!primes[i])
|
||||
{
|
||||
if(!goldbach(i))
|
||||
{
|
||||
found = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
free(primes);
|
||||
|
||||
clock_gettime(CLOCK_MONOTONIC, &end);
|
||||
|
||||
elapsed = (end.tv_sec - start.tv_sec) + (double)(end.tv_nsec - start.tv_nsec) / 1000000000;
|
||||
|
||||
printf("Project Euler, Problem 46\n");
|
||||
printf("Answer: %d\n", i-2);
|
||||
|
||||
printf("Elapsed time: %.9lf seconds\n", elapsed);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int goldbach(int n)
|
||||
{
|
||||
int i, j, tmp;
|
||||
|
||||
for(i = 2; i < n; i++)
|
||||
{
|
||||
if(primes[i])
|
||||
{
|
||||
j = 1;
|
||||
|
||||
do
|
||||
{
|
||||
tmp = i + 2 * j * j;
|
||||
|
||||
if(tmp == n)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
j++;
|
||||
}while(tmp < n);
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
77
C/p047.c
Normal file
77
C/p047.c
Normal file
@ -0,0 +1,77 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
#include <time.h>
|
||||
#include "projecteuler.h"
|
||||
|
||||
#define N 150000
|
||||
|
||||
int count_distinct_factors(int n);
|
||||
|
||||
int *primes;
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int i, found = 0;
|
||||
double elapsed;
|
||||
struct timespec start, end;
|
||||
|
||||
clock_gettime(CLOCK_MONOTONIC, &start);
|
||||
|
||||
primes = sieve(N);
|
||||
|
||||
for(i = 645; !found && i < N - 3; i++)
|
||||
{
|
||||
if(!primes[i] && !primes[i+1] && !primes[i+2] && !primes[i+3])
|
||||
{
|
||||
if(count_distinct_factors(i) == 4 && count_distinct_factors(i+1) == 4 &&
|
||||
count_distinct_factors(i+2) == 4 && count_distinct_factors(i+3) == 4)
|
||||
{
|
||||
found = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
free(primes);
|
||||
|
||||
clock_gettime(CLOCK_MONOTONIC, &end);
|
||||
|
||||
elapsed = (end.tv_sec - start.tv_sec) + (double)(end.tv_nsec - start.tv_nsec) / 1000000000;
|
||||
|
||||
printf("Project Euler, Problem 47\n");
|
||||
printf("Answer: %d\n", i-1);
|
||||
|
||||
printf("Elapsed time: %.9lf seconds\n", elapsed);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int count_distinct_factors(int n)
|
||||
{
|
||||
int i, count=0;
|
||||
|
||||
if(n % 2 == 0)
|
||||
{
|
||||
count++;
|
||||
|
||||
do
|
||||
{
|
||||
n /= 2;
|
||||
}while(n % 2 == 0);
|
||||
}
|
||||
|
||||
for(i = 3; n > 1; i += 2)
|
||||
{
|
||||
if(primes[i] && n % i == 0)
|
||||
{
|
||||
count++;
|
||||
|
||||
do
|
||||
{
|
||||
n /= i;
|
||||
}while(n % i == 0);
|
||||
}
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
40
C/p048.c
Normal file
40
C/p048.c
Normal file
@ -0,0 +1,40 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <gmp.h>
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int i;
|
||||
double elapsed;
|
||||
struct timespec start, end;
|
||||
mpz_t power, sum;
|
||||
char *res;
|
||||
|
||||
clock_gettime(CLOCK_MONOTONIC, &start);
|
||||
|
||||
mpz_init_set_ui(sum, 0);
|
||||
mpz_init(power);
|
||||
|
||||
for(i = 1; i <= 1000; i++)
|
||||
{
|
||||
mpz_ui_pow_ui(power, i, i);
|
||||
mpz_add(sum, sum, power);
|
||||
}
|
||||
|
||||
res = mpz_get_str(NULL, 10, sum);
|
||||
|
||||
mpz_clears(power, sum, NULL);
|
||||
|
||||
clock_gettime(CLOCK_MONOTONIC, &end);
|
||||
|
||||
elapsed = (end.tv_sec - start.tv_sec) + (double)(end.tv_nsec - start.tv_nsec) / 1000000000;
|
||||
|
||||
printf("Project Euler, Problem 48\n");
|
||||
printf("Answer: %s\n", res+strlen(res)-10);
|
||||
|
||||
printf("Elapsed time: %.9lf seconds\n", elapsed);
|
||||
|
||||
return 0;
|
||||
}
|
79
C/p049.c
Normal file
79
C/p049.c
Normal file
@ -0,0 +1,79 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
#include <time.h>
|
||||
#include "projecteuler.h"
|
||||
|
||||
#define N 10000
|
||||
|
||||
int check_digits(int a, int b);
|
||||
|
||||
int *primes;
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int i, j, found = 0;
|
||||
double elapsed;
|
||||
struct timespec start, end;
|
||||
|
||||
clock_gettime(CLOCK_MONOTONIC, &start);
|
||||
|
||||
primes = sieve(N);
|
||||
|
||||
for(i = 1489; i < N && !found; i++)
|
||||
{
|
||||
if(primes[i])
|
||||
{
|
||||
for(j = 1; j < 4255; j++)
|
||||
{
|
||||
if(i + 2 * j < N && primes[i+j] && primes[i+2*j] &&
|
||||
check_digits(i, i+j) && check_digits(i, i+2*j))
|
||||
{
|
||||
found = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
free(primes);
|
||||
|
||||
clock_gettime(CLOCK_MONOTONIC, &end);
|
||||
|
||||
elapsed = (end.tv_sec - start.tv_sec) + (double)(end.tv_nsec - start.tv_nsec) / 1000000000;
|
||||
|
||||
printf("Project Euler, Problem 49\n");
|
||||
printf("Answer: %d%d%d\n", i-1, i-1+j, i-1+2*j);
|
||||
|
||||
printf("Elapsed time: %.9lf seconds\n", elapsed);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int check_digits(int a, int b)
|
||||
{
|
||||
int i;
|
||||
int digits1[10] = {0}, digits2[10] = {0};
|
||||
|
||||
while(a > 0)
|
||||
{
|
||||
digits1[a%10]++;
|
||||
a /= 10;
|
||||
}
|
||||
|
||||
while(b > 0)
|
||||
{
|
||||
digits2[b%10]++;
|
||||
b /= 10;
|
||||
}
|
||||
|
||||
for(i = 0; i < 10; i++)
|
||||
{
|
||||
if(digits1[i] != digits2[i])
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
56
C/p050.c
Normal file
56
C/p050.c
Normal file
@ -0,0 +1,56 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
#include <time.h>
|
||||
#include "projecteuler.h"
|
||||
|
||||
#define N 1000000
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int i, j, max = 0, max_p = 0, sum, count;
|
||||
int *primes;
|
||||
double elapsed;
|
||||
struct timespec start, end;
|
||||
|
||||
clock_gettime(CLOCK_MONOTONIC, &start);
|
||||
|
||||
primes = sieve(N);
|
||||
|
||||
for(i = 2; i < N; i++)
|
||||
{
|
||||
if(primes[i])
|
||||
{
|
||||
count = 1;
|
||||
sum = i;
|
||||
|
||||
for(j = i + 1; j < N && sum < N; j++)
|
||||
{
|
||||
if(primes[j])
|
||||
{
|
||||
sum += j;
|
||||
count++;
|
||||
|
||||
if(sum < N && primes[sum] && count > max)
|
||||
{
|
||||
max = count;
|
||||
max_p = sum;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
free(primes);
|
||||
|
||||
clock_gettime(CLOCK_MONOTONIC, &end);
|
||||
|
||||
elapsed = (end.tv_sec - start.tv_sec) + (double)(end.tv_nsec - start.tv_nsec) / 1000000000;
|
||||
|
||||
printf("Project Euler, Problem 50\n");
|
||||
printf("Answer: %d\n", max_p);
|
||||
|
||||
printf("Elapsed time: %.9lf seconds\n", elapsed);
|
||||
|
||||
return 0;
|
||||
}
|
1
C/words.txt
Normal file
1
C/words.txt
Normal file
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user