Add more solutions
Added solutions for problems from 41 to 5, both in C and python
This commit is contained in:
parent
7bab173c05
commit
f8cce530b5
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
29
Python/p041.py
Normal file
29
Python/p041.py
Normal file
@ -0,0 +1,29 @@
|
||||
#!/usr/bin/python3
|
||||
|
||||
from timeit import default_timer
|
||||
from projecteuler import is_pandigital, is_prime
|
||||
|
||||
def count_digits(n):
|
||||
i = 0
|
||||
|
||||
while n > 0:
|
||||
i = i + 1
|
||||
n = n // 10
|
||||
|
||||
return i
|
||||
|
||||
def main():
|
||||
start = default_timer()
|
||||
|
||||
for i in range(7654321, 0, -2):
|
||||
if is_pandigital(i, count_digits(i)) and is_prime(i):
|
||||
print('Project Euler, Problem 41')
|
||||
print('Answer: {}'.format(i))
|
||||
break
|
||||
|
||||
end = default_timer()
|
||||
|
||||
print('Elapsed time: {:.9f} seconds'.format(end - start))
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
50
Python/p042.py
Normal file
50
Python/p042.py
Normal file
@ -0,0 +1,50 @@
|
||||
#!/usr/bin/python3
|
||||
|
||||
from timeit import default_timer
|
||||
|
||||
def is_triang(n):
|
||||
i = 1
|
||||
j = 1
|
||||
|
||||
while j <= n:
|
||||
if n == j:
|
||||
return True
|
||||
i = i + 1
|
||||
j = j + i
|
||||
|
||||
return False
|
||||
|
||||
def main():
|
||||
start = default_timer()
|
||||
|
||||
try:
|
||||
fp = open('words.txt', 'r')
|
||||
except:
|
||||
print('Error while opening file {}'.format('words.txt'))
|
||||
exit(1)
|
||||
|
||||
words = list(fp.readline().replace('"', '').split(','))
|
||||
|
||||
fp.close()
|
||||
|
||||
count = 0
|
||||
|
||||
for word in words:
|
||||
value = 0
|
||||
l = len(word)
|
||||
|
||||
for j in range(l):
|
||||
value = value + ord(word[j]) - ord('A') + 1
|
||||
|
||||
if is_triang(value):
|
||||
count = count + 1
|
||||
|
||||
end = default_timer()
|
||||
|
||||
print('Project Euler, Problem 42')
|
||||
print('Answer: {}'.format(count))
|
||||
|
||||
print('Elapsed time: {:.9f} seconds'.format(end - start))
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
64
Python/p043.py
Normal file
64
Python/p043.py
Normal file
@ -0,0 +1,64 @@
|
||||
#!/usr/bin/python3
|
||||
|
||||
from itertools import permutations
|
||||
|
||||
from timeit import default_timer
|
||||
|
||||
def has_property(n):
|
||||
value = int(n[1]) * 100 + int(n[2]) * 10 + int(n[3])
|
||||
|
||||
if value % 2 != 0:
|
||||
return False
|
||||
|
||||
value = int(n[2]) * 100 + int(n[3]) * 10 + int(n[4])
|
||||
|
||||
if value % 3 != 0:
|
||||
return False
|
||||
|
||||
value = int(n[3]) * 100 + int(n[4]) * 10 + int(n[5])
|
||||
|
||||
if value % 5 != 0:
|
||||
return False
|
||||
|
||||
value = int(n[4]) * 100 + int(n[5]) * 10 + int(n[6])
|
||||
|
||||
if value % 7 != 0:
|
||||
return False
|
||||
|
||||
value = int(n[5]) * 100 + int(n[6]) * 10 + int(n[7])
|
||||
|
||||
if value % 11 != 0:
|
||||
return False
|
||||
|
||||
value = int(n[6]) * 100 + int(n[7]) * 10 + int(n[8])
|
||||
|
||||
if value % 13 != 0:
|
||||
return False
|
||||
|
||||
value = int(n[7]) * 100 + int(n[8]) * 10 + int(n[9])
|
||||
|
||||
if value % 17 != 0:
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
def main():
|
||||
start = default_timer()
|
||||
|
||||
perm = list(permutations('0123456789'))
|
||||
|
||||
sum_ = 0
|
||||
|
||||
for i in perm:
|
||||
if has_property(i):
|
||||
sum_ = sum_ + int(''.join(map(str, i)))
|
||||
|
||||
end = default_timer()
|
||||
|
||||
print('Project Euler, Problem 43')
|
||||
print('Answer: {}'.format(sum_))
|
||||
|
||||
print('Elapsed time: {:.9f} seconds'.format(end - start))
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
34
Python/p044.py
Normal file
34
Python/p044.py
Normal file
@ -0,0 +1,34 @@
|
||||
#!/usr/bin/python3
|
||||
|
||||
from math import sqrt
|
||||
|
||||
from timeit import default_timer
|
||||
from projecteuler import is_pentagonal
|
||||
|
||||
def main():
|
||||
start = default_timer()
|
||||
|
||||
found = 0
|
||||
n = 2
|
||||
|
||||
while not found:
|
||||
pn = n * (3 * n - 1) // 2
|
||||
|
||||
for m in range(1, n):
|
||||
pm = m * (3 * m - 1) // 2
|
||||
|
||||
if is_pentagonal(pn+pm) and is_pentagonal(pn-pm):
|
||||
found = 1
|
||||
break
|
||||
|
||||
n = n + 1
|
||||
|
||||
end = default_timer()
|
||||
|
||||
print('Project Euler, Problem 44')
|
||||
print('Answer: {}'.format(pn-pm))
|
||||
|
||||
print('Elapsed time: {:.9f} seconds'.format(end - start))
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
29
Python/p045.py
Normal file
29
Python/p045.py
Normal file
@ -0,0 +1,29 @@
|
||||
#!/usr/bin/python3
|
||||
|
||||
from math import sqrt
|
||||
|
||||
from timeit import default_timer
|
||||
from projecteuler import is_pentagonal
|
||||
|
||||
def main():
|
||||
start = default_timer()
|
||||
|
||||
found = 0
|
||||
i = 143
|
||||
|
||||
while not found:
|
||||
i = i + 1
|
||||
n = i * (2 * i - 1)
|
||||
|
||||
if is_pentagonal(n):
|
||||
found = 1
|
||||
|
||||
end = default_timer()
|
||||
|
||||
print('Project Euler, Problem 45')
|
||||
print('Answer: {}'.format(n))
|
||||
|
||||
print('Elapsed time: {:.9f} seconds'.format(end - start))
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
52
Python/p046.py
Normal file
52
Python/p046.py
Normal file
@ -0,0 +1,52 @@
|
||||
#!/usr/bin/python3
|
||||
|
||||
from timeit import default_timer
|
||||
from projecteuler import sieve
|
||||
|
||||
def goldbach(n):
|
||||
global primes
|
||||
|
||||
for i in range(2, n):
|
||||
if primes[i] == 1:
|
||||
j = 1
|
||||
|
||||
while True:
|
||||
tmp = i + 2 * j * j
|
||||
|
||||
if tmp == n:
|
||||
return True
|
||||
|
||||
j = j + 1
|
||||
|
||||
if tmp >= n:
|
||||
break
|
||||
|
||||
return False
|
||||
|
||||
def main():
|
||||
start = default_timer()
|
||||
|
||||
global primes
|
||||
|
||||
N = 10000
|
||||
|
||||
primes = sieve(N)
|
||||
|
||||
found = 0
|
||||
i = 3
|
||||
|
||||
while not found and i < N:
|
||||
if primes[i] == 0:
|
||||
if not goldbach(i):
|
||||
found = 1
|
||||
i = i + 2
|
||||
|
||||
end = default_timer()
|
||||
|
||||
print('Project Euler, Problem 46')
|
||||
print('Answer: {}'.format(i-2))
|
||||
|
||||
print('Elapsed time: {:.9f} seconds'.format(end - start))
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
59
Python/p047.py
Normal file
59
Python/p047.py
Normal file
@ -0,0 +1,59 @@
|
||||
#!/usr/bin/python3
|
||||
|
||||
from timeit import default_timer
|
||||
from projecteuler import sieve
|
||||
|
||||
def count_distinct_factors(n):
|
||||
global primes
|
||||
count = 0
|
||||
|
||||
if n % 2 == 0:
|
||||
count = count + 1
|
||||
|
||||
while True:
|
||||
n = n // 2
|
||||
|
||||
if n % 2 != 0:
|
||||
break
|
||||
|
||||
i = 3
|
||||
|
||||
while n > 1:
|
||||
if primes[i] == 1 and n % i == 0:
|
||||
count = count + 1
|
||||
|
||||
while True:
|
||||
n = n // i
|
||||
|
||||
if n % i != 0:
|
||||
break
|
||||
i = i + 2
|
||||
|
||||
return count
|
||||
|
||||
def main():
|
||||
start = default_timer()
|
||||
|
||||
global primes
|
||||
|
||||
N = 150000
|
||||
|
||||
primes = sieve(N)
|
||||
found = 0
|
||||
i = 645
|
||||
|
||||
while not found and i < N - 3:
|
||||
if primes[i] == 0 and primes[i+1] == 0 and primes[i+2] == 0 and primes[i+3] == 0:
|
||||
if count_distinct_factors(i) == 4 and count_distinct_factors(i+1) == 4 and count_distinct_factors(i+2) == 4 and count_distinct_factors(i+3) == 4:
|
||||
found = 1
|
||||
i = i + 1
|
||||
|
||||
end = default_timer()
|
||||
|
||||
print('Project Euler, Problem 47')
|
||||
print('Answer: {}'.format(i-1))
|
||||
|
||||
print('Elapsed time: {:.9f} seconds'.format(end - start))
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
22
Python/p048.py
Normal file
22
Python/p048.py
Normal file
@ -0,0 +1,22 @@
|
||||
#!/usr/bin/python3
|
||||
|
||||
from timeit import default_timer
|
||||
|
||||
def main():
|
||||
start = default_timer()
|
||||
|
||||
sum_ = 0
|
||||
|
||||
for i in range(1, 1001):
|
||||
power = i ** i
|
||||
sum_ = sum_ + power
|
||||
|
||||
end = default_timer()
|
||||
|
||||
print('Project Euler, Problem 48')
|
||||
print('Answer: {}'.format(str(sum_)[-10:]))
|
||||
|
||||
print('Elapsed time: {:.9f} seconds'.format(end - start))
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
53
Python/p049.py
Normal file
53
Python/p049.py
Normal file
@ -0,0 +1,53 @@
|
||||
#!/usr/bin/python3
|
||||
|
||||
from numpy import zeros
|
||||
|
||||
from timeit import default_timer
|
||||
from projecteuler import sieve
|
||||
|
||||
def check_digits(a, b):
|
||||
digits1 = zeros(10, int)
|
||||
digits2 = zeros(10, int)
|
||||
|
||||
while a > 0:
|
||||
digits1[a%10] = digits1[a%10] + 1
|
||||
a = a // 10
|
||||
|
||||
while b > 0:
|
||||
digits2[b%10] = digits2[b%10] + 1
|
||||
b = b // 10
|
||||
|
||||
for i in range(10):
|
||||
if digits1[i] != digits2[i]:
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
def main():
|
||||
start = default_timer()
|
||||
|
||||
N = 10000
|
||||
|
||||
primes = sieve(N)
|
||||
|
||||
found = 0
|
||||
i = 1489
|
||||
|
||||
while i < N and found == 0:
|
||||
if primes[i] == 1:
|
||||
for j in range(1, 4255):
|
||||
if i + 2 * j < N and primes[i+j] == 1 and primes[i+2*j] == 1 and\
|
||||
check_digits(i, i+j) and check_digits(i, i+2*j):
|
||||
found = 1
|
||||
break
|
||||
i = i + 1
|
||||
|
||||
end = default_timer()
|
||||
|
||||
print('Project Euler, Problem 49')
|
||||
print('Answer: {}'.format(str(i-1)+str(i-1+j)+str(i-1+2*j)))
|
||||
|
||||
print('Elapsed time: {:.9f} seconds'.format(end - start))
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
41
Python/p050.py
Normal file
41
Python/p050.py
Normal file
@ -0,0 +1,41 @@
|
||||
#!/usr/bin/python3
|
||||
|
||||
from timeit import default_timer
|
||||
from projecteuler import sieve
|
||||
|
||||
def main():
|
||||
start = default_timer()
|
||||
|
||||
N = 1000000
|
||||
|
||||
primes = sieve(N)
|
||||
|
||||
max_ = 0
|
||||
max_p = 0
|
||||
|
||||
for i in range(2, N):
|
||||
if primes[i] == 1:
|
||||
count = 1
|
||||
sum_ = i
|
||||
j = i + 1
|
||||
|
||||
while j < N and sum_ < N:
|
||||
if primes[j] == 1:
|
||||
sum_ = sum_ + j
|
||||
count = count + 1
|
||||
|
||||
if sum_ < N and primes[sum_] == 1 and count > max_:
|
||||
max_ = count
|
||||
max_p = sum_
|
||||
|
||||
j = j + 1
|
||||
|
||||
end = default_timer()
|
||||
|
||||
print('Project Euler, Problem 50')
|
||||
print('Answer: {}'.format(max_p))
|
||||
|
||||
print('Elapsed time: {:.9f} seconds'.format(end - start))
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
1
Python/words.txt
Normal file
1
Python/words.txt
Normal file
File diff suppressed because one or more lines are too long
Loading…
x
Reference in New Issue
Block a user