Add more solutions
Added solutions for problems 21, 22, 23, 24 and 25 both in C and in python
This commit is contained in:
parent
606cf399b9
commit
6b29655330
1
C/names.txt
Normal file
1
C/names.txt
Normal file
File diff suppressed because one or more lines are too long
58
C/p021.c
Normal file
58
C/p021.c
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <math.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
|
int sum_of_d(int n);
|
||||||
|
|
||||||
|
int main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
int i, n, sum = 0;
|
||||||
|
double elapsed;
|
||||||
|
struct timespec start, end;
|
||||||
|
|
||||||
|
clock_gettime(CLOCK_MONOTONIC, &start);
|
||||||
|
|
||||||
|
for(i = 2; i < 10000; i++)
|
||||||
|
{
|
||||||
|
n = sum_of_d(i);
|
||||||
|
if(i != n && sum_of_d(n) == i)
|
||||||
|
{
|
||||||
|
sum += i + n;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sum /= 2;
|
||||||
|
|
||||||
|
clock_gettime(CLOCK_MONOTONIC, &end);
|
||||||
|
|
||||||
|
elapsed = (end.tv_sec - start.tv_sec) + (double)(end.tv_nsec - start.tv_nsec) / 1000000000;
|
||||||
|
|
||||||
|
printf("Project Euler, Problem 21\n");
|
||||||
|
printf("Answer: %d\n", sum);
|
||||||
|
|
||||||
|
printf("Elapsed time: %.9lf seconds\n", elapsed);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int sum_of_d(int n)
|
||||||
|
{
|
||||||
|
int i, sum = 1, limit;
|
||||||
|
|
||||||
|
limit = floor(sqrt(n));
|
||||||
|
|
||||||
|
for(i = 2; i <= limit; i++)
|
||||||
|
{
|
||||||
|
if(n % i == 0)
|
||||||
|
{
|
||||||
|
sum += i;
|
||||||
|
if(n != i * i)
|
||||||
|
{
|
||||||
|
sum += (n / i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return sum;
|
||||||
|
}
|
88
C/p022.c
Normal file
88
C/p022.c
Normal file
@ -0,0 +1,88 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <time.h>
|
||||||
|
#include "projecteuler.h"
|
||||||
|
|
||||||
|
int compare(void *string1, void *string2);
|
||||||
|
|
||||||
|
int main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
FILE *fp;
|
||||||
|
int i, j, n, len, score, sum = 0;
|
||||||
|
double elapsed;
|
||||||
|
char *line, **names;
|
||||||
|
struct timespec start, end;
|
||||||
|
|
||||||
|
clock_gettime(CLOCK_MONOTONIC, &start);
|
||||||
|
|
||||||
|
if((fp = fopen("names.txt", "r")) == NULL)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Error while opening file %s\n", "names.txt");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
fscanf(fp, "%ms", &line);
|
||||||
|
|
||||||
|
fclose(fp);
|
||||||
|
|
||||||
|
len = strlen(line);
|
||||||
|
n = 1;
|
||||||
|
|
||||||
|
for(i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
if(line[i] == ',')
|
||||||
|
{
|
||||||
|
n++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if((names = (char **)malloc(n*sizeof(char *))) == NULL)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Error while allocating memory\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
names[0] = strtok(line, ",\"");
|
||||||
|
|
||||||
|
for(i = 1; i < n; i++)
|
||||||
|
{
|
||||||
|
names[i] = strtok(NULL, ",\"");
|
||||||
|
}
|
||||||
|
|
||||||
|
quick_sort((void **)names, 0, n-1, compare);
|
||||||
|
|
||||||
|
for(i = 0; i < n; i++)
|
||||||
|
{
|
||||||
|
len = strlen(names[i]);
|
||||||
|
score = 0;
|
||||||
|
for(j = 0; j < len; j++)
|
||||||
|
{
|
||||||
|
score += names[i][j] - 'A' + 1;
|
||||||
|
}
|
||||||
|
score *= (i + 1);
|
||||||
|
sum += score;
|
||||||
|
}
|
||||||
|
|
||||||
|
free(line);
|
||||||
|
|
||||||
|
clock_gettime(CLOCK_MONOTONIC, &end);
|
||||||
|
|
||||||
|
elapsed = (end.tv_sec - start.tv_sec) + (double)(end.tv_nsec - start.tv_nsec) / 1000000000;
|
||||||
|
|
||||||
|
printf("Project Euler, Problem 22\n");
|
||||||
|
printf("Answer: %d\n", sum);
|
||||||
|
printf("Elapsed time: %.9lf seconds\n", elapsed);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int compare(void *string1, void *string2)
|
||||||
|
{
|
||||||
|
char *s1, *s2;
|
||||||
|
|
||||||
|
s1 = (char *)string1;
|
||||||
|
s2 = (char *)string2;
|
||||||
|
|
||||||
|
return strcmp(s1, s2);
|
||||||
|
}
|
94
C/p023.c
Normal file
94
C/p023.c
Normal file
@ -0,0 +1,94 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <math.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
|
int is_abundant(int n);
|
||||||
|
|
||||||
|
int main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
int ab_nums[28123], sums[28123] = {0};
|
||||||
|
int i, j, sum;
|
||||||
|
double elapsed;
|
||||||
|
struct timespec start, end;
|
||||||
|
|
||||||
|
clock_gettime(CLOCK_MONOTONIC, &start);
|
||||||
|
|
||||||
|
for(i = 0; i < 28123; i++)
|
||||||
|
{
|
||||||
|
if(i < 11)
|
||||||
|
{
|
||||||
|
ab_nums[i] = 0;
|
||||||
|
}
|
||||||
|
else if(is_abundant(i+1))
|
||||||
|
{
|
||||||
|
ab_nums[i] = 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ab_nums[i] = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for(i = 0; i < 28123; i++)
|
||||||
|
{
|
||||||
|
if(ab_nums[i])
|
||||||
|
{
|
||||||
|
for(j = i; j < 28123; j++)
|
||||||
|
{
|
||||||
|
if(ab_nums[j])
|
||||||
|
{
|
||||||
|
sum = i + j + 2;
|
||||||
|
|
||||||
|
if(sum <= 28123)
|
||||||
|
{
|
||||||
|
sums[sum-1] = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sum = 0;
|
||||||
|
|
||||||
|
for(i = 0; i < 28123; i++)
|
||||||
|
{
|
||||||
|
if(!sums[i])
|
||||||
|
{
|
||||||
|
sum += i + 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 23\n");
|
||||||
|
printf("Answer: %d\n", sum);
|
||||||
|
|
||||||
|
printf("Elapsed time: %.9lf seconds\n", elapsed);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int is_abundant(int n)
|
||||||
|
{
|
||||||
|
int i, sum = 1, limit;
|
||||||
|
|
||||||
|
limit = floor(sqrt(n));
|
||||||
|
|
||||||
|
for(i = 2; i <= limit; i++)
|
||||||
|
{
|
||||||
|
if(n % i == 0)
|
||||||
|
{
|
||||||
|
sum += i;
|
||||||
|
|
||||||
|
if(n != i*i)
|
||||||
|
{
|
||||||
|
sum += (n / i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return sum > n;
|
||||||
|
}
|
98
C/p024.c
Normal file
98
C/p024.c
Normal file
@ -0,0 +1,98 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
|
void next_perm(int *perm, int n);
|
||||||
|
void swap(int *vet, int i, int j);
|
||||||
|
void sort(int *vet, int i, int n);
|
||||||
|
|
||||||
|
int main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
int i, perm[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
|
||||||
|
double elapsed;
|
||||||
|
struct timespec start, end;
|
||||||
|
|
||||||
|
clock_gettime(CLOCK_MONOTONIC, &start);
|
||||||
|
|
||||||
|
for(i = 0; i < 999999; i++)
|
||||||
|
{
|
||||||
|
next_perm(perm, 10);
|
||||||
|
}
|
||||||
|
|
||||||
|
clock_gettime(CLOCK_MONOTONIC, &end);
|
||||||
|
|
||||||
|
printf("Project Euler, Problem 24\n");
|
||||||
|
printf("Answer: ");
|
||||||
|
|
||||||
|
for(i = 0; i < 10; i++)
|
||||||
|
{
|
||||||
|
printf("%d", perm[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
elapsed = (end.tv_sec - start.tv_sec) + (double)(end.tv_nsec - start.tv_nsec) / 1000000000;
|
||||||
|
|
||||||
|
printf("Elapsed time: %.9lf seconds\n", elapsed);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void next_perm(int *perm, int n)
|
||||||
|
{
|
||||||
|
int i, j, min = n, min_idx, flag = 0;
|
||||||
|
|
||||||
|
for(i = 0; i < n - 1; i++)
|
||||||
|
{
|
||||||
|
if(perm[i] < perm[i+1])
|
||||||
|
{
|
||||||
|
flag=1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!flag)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for(i = n - 2; perm[i] > perm[i+1]; i--);
|
||||||
|
|
||||||
|
for(j = i + 1; j < n; j++)
|
||||||
|
{
|
||||||
|
if(perm[j] > perm[i] && perm[j] < min)
|
||||||
|
{
|
||||||
|
min = perm[j];
|
||||||
|
min_idx = j;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
swap(perm, i, min_idx);
|
||||||
|
sort(perm, i+1, n);
|
||||||
|
}
|
||||||
|
|
||||||
|
void swap(int *vet, int i, int j)
|
||||||
|
{
|
||||||
|
int tmp;
|
||||||
|
|
||||||
|
tmp = vet[i];
|
||||||
|
vet[i] = vet[j];
|
||||||
|
vet[j] = tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
void sort(int *vet, int i, int j)
|
||||||
|
{
|
||||||
|
int a, b, tmp;
|
||||||
|
|
||||||
|
for(a=i+1; a<j; a++)
|
||||||
|
{
|
||||||
|
tmp=vet[a];
|
||||||
|
b=a-1;
|
||||||
|
while(b>=i && vet[b]>tmp)
|
||||||
|
{
|
||||||
|
vet[b+1]=vet[b];
|
||||||
|
b--;
|
||||||
|
}
|
||||||
|
vet[b+1]=tmp;
|
||||||
|
}
|
||||||
|
}
|
76
C/p025.c
Normal file
76
C/p025.c
Normal file
@ -0,0 +1,76 @@
|
|||||||
|
#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 a, b;
|
||||||
|
char *num;
|
||||||
|
size_t size;
|
||||||
|
|
||||||
|
clock_gettime(CLOCK_MONOTONIC, &start);
|
||||||
|
|
||||||
|
mpz_init_set_ui(a, 1);
|
||||||
|
mpz_init_set_ui(b, 1);
|
||||||
|
|
||||||
|
i = 2;
|
||||||
|
|
||||||
|
while(1)
|
||||||
|
{
|
||||||
|
mpz_add(a, a, b);
|
||||||
|
i++;
|
||||||
|
|
||||||
|
if((size = mpz_sizeinbase(a, 10)) == 1000)
|
||||||
|
{
|
||||||
|
if((num = (char *)malloc((2+size)*sizeof(char))) == NULL)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Error while allocating memory\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
gmp_sprintf(num, "%Zd", a);
|
||||||
|
size = strlen(num);
|
||||||
|
free(num);
|
||||||
|
if(size == 1000)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mpz_add(b, a, b);
|
||||||
|
i++;
|
||||||
|
|
||||||
|
if((size = mpz_sizeinbase(b, 10)) == 1000)
|
||||||
|
{
|
||||||
|
if((num = (char *)malloc((2+size)*sizeof(char))) == NULL)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Error while allocating memory\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
gmp_sprintf(num, "%Zd", b);
|
||||||
|
size = strlen(num);
|
||||||
|
free(num);
|
||||||
|
if(size == 1000)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mpz_clears(a, b, 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 25\n");
|
||||||
|
printf("Answer: %d\n", i);
|
||||||
|
|
||||||
|
printf("Elapsed time: %.9lf seconds\n", elapsed);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
@ -128,3 +128,30 @@ int count_divisors(int n)
|
|||||||
|
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void quick_sort(void **array, int l, int r, int (*cmp)(void *lv, void *rv))
|
||||||
|
{
|
||||||
|
int i, j;
|
||||||
|
void *pivot, *tmp;
|
||||||
|
|
||||||
|
if (l >= r) return;
|
||||||
|
|
||||||
|
pivot = array[l];
|
||||||
|
i = l - 1;
|
||||||
|
j = r + 1;
|
||||||
|
|
||||||
|
while (i < j) {
|
||||||
|
|
||||||
|
while (cmp(array[++i], pivot) < 0);
|
||||||
|
while (cmp(array[--j], pivot) > 0);
|
||||||
|
|
||||||
|
if (i < j) {
|
||||||
|
tmp = array[i];
|
||||||
|
array[i] = array[j];
|
||||||
|
array[j] = tmp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
quick_sort(array, l, j, cmp);
|
||||||
|
quick_sort(array, j+1, r, cmp);
|
||||||
|
}
|
||||||
|
@ -7,5 +7,6 @@ long int lcm(long int a, long int b);
|
|||||||
long int gcd(long int a, long int b);
|
long int gcd(long int a, long int b);
|
||||||
int *sieve(int n);
|
int *sieve(int n);
|
||||||
int count_divisors(int n);
|
int count_divisors(int n);
|
||||||
|
void quick_sort(void **array, int l, int r, int (*cmp)(void *lv, void *rv));
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
1
Python/names.txt
Normal file
1
Python/names.txt
Normal file
File diff suppressed because one or more lines are too long
40
Python/p021.py
Normal file
40
Python/p021.py
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
#!/usr/bin/python3
|
||||||
|
|
||||||
|
import math
|
||||||
|
|
||||||
|
from timeit import default_timer
|
||||||
|
|
||||||
|
def sum_of_divisors(n):
|
||||||
|
limit = math.floor(math.sqrt(n)) + 1
|
||||||
|
|
||||||
|
sum_ = 1
|
||||||
|
|
||||||
|
for i in range(2, limit):
|
||||||
|
if n % i == 0:
|
||||||
|
sum_ = sum_ + i
|
||||||
|
if n != i * i:
|
||||||
|
sum_ = sum_ + n // i
|
||||||
|
|
||||||
|
return sum_
|
||||||
|
|
||||||
|
def main():
|
||||||
|
start = default_timer()
|
||||||
|
|
||||||
|
sum_ = 0
|
||||||
|
|
||||||
|
for i in range(2, 10000):
|
||||||
|
n = sum_of_divisors(i)
|
||||||
|
if i != n and sum_of_divisors(n) == i:
|
||||||
|
sum_ = sum_ + i + n
|
||||||
|
|
||||||
|
sum_ = sum_ // 2
|
||||||
|
|
||||||
|
end = default_timer()
|
||||||
|
|
||||||
|
print('Project Euler, Problem 21')
|
||||||
|
print('Answer: {}'.format(sum_))
|
||||||
|
|
||||||
|
print('Elapsed time: {:.9f} seconds'.format(end - start))
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
35
Python/p022.py
Normal file
35
Python/p022.py
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
#!/usr/bin/python3
|
||||||
|
|
||||||
|
from timeit import default_timer
|
||||||
|
|
||||||
|
def main():
|
||||||
|
start = default_timer()
|
||||||
|
|
||||||
|
try:
|
||||||
|
fp = open('names.txt', 'r')
|
||||||
|
except:
|
||||||
|
print('Error while opening file {}'.format('names.txt'))
|
||||||
|
exit(1)
|
||||||
|
|
||||||
|
names = list(fp.readline().replace('"', '').split(','))
|
||||||
|
names.sort()
|
||||||
|
|
||||||
|
sum_ = 0
|
||||||
|
|
||||||
|
for i in range(len(names)):
|
||||||
|
l = len(names[i])
|
||||||
|
score = 0
|
||||||
|
for j in range(l):
|
||||||
|
score = score + ord(names[i][j]) - ord('A') + 1
|
||||||
|
score = score * (i + 1)
|
||||||
|
sum_ = sum_ + score
|
||||||
|
|
||||||
|
end = default_timer()
|
||||||
|
|
||||||
|
print('Project Euler, Problem 22')
|
||||||
|
print('Answer: {}'.format(sum_))
|
||||||
|
|
||||||
|
print('Elapsed time: {:.9f} seconds'.format(end - start))
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
51
Python/p023.py
Normal file
51
Python/p023.py
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
#!/usr/bin/python3
|
||||||
|
|
||||||
|
import math
|
||||||
|
|
||||||
|
from timeit import default_timer
|
||||||
|
|
||||||
|
def is_abundant(n):
|
||||||
|
limit = math.floor(math.sqrt(n)) + 1
|
||||||
|
sum_ = 1
|
||||||
|
|
||||||
|
for i in range(2, limit):
|
||||||
|
if n % i == 0:
|
||||||
|
sum_ = sum_ + i
|
||||||
|
if n != i * i:
|
||||||
|
sum_ = sum_ + n // i
|
||||||
|
|
||||||
|
return sum_ > n
|
||||||
|
|
||||||
|
def main():
|
||||||
|
start = default_timer()
|
||||||
|
|
||||||
|
ab_nums = [False] * 28124
|
||||||
|
|
||||||
|
for i in range(12, 28124):
|
||||||
|
ab_nums[i] = is_abundant(i)
|
||||||
|
|
||||||
|
sums = [False] * 28124
|
||||||
|
|
||||||
|
for i in range(1, 28123):
|
||||||
|
if ab_nums[i]:
|
||||||
|
for j in range(i, 28123):
|
||||||
|
if ab_nums[j]:
|
||||||
|
sum_ = i + j
|
||||||
|
if sum_ <= 28123:
|
||||||
|
sums[sum_] = True
|
||||||
|
|
||||||
|
sum_ = 0
|
||||||
|
|
||||||
|
for i in range(1, 28124):
|
||||||
|
if not sums[i]:
|
||||||
|
sum_ = sum_ + i
|
||||||
|
|
||||||
|
end = default_timer()
|
||||||
|
|
||||||
|
print('Project Euler, Problem 23')
|
||||||
|
print('Answer: {}'.format(sum_))
|
||||||
|
|
||||||
|
print('Elapsed time: {:.9f} seconds'.format(end - start))
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
21
Python/p024.py
Normal file
21
Python/p024.py
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
#!/usr/bin/python3
|
||||||
|
|
||||||
|
from itertools import permutations
|
||||||
|
|
||||||
|
from timeit import default_timer
|
||||||
|
|
||||||
|
def main():
|
||||||
|
start = default_timer()
|
||||||
|
|
||||||
|
perm = list(permutations('0123456789'))
|
||||||
|
res = int(''.join(map(str, perm[999999])))
|
||||||
|
|
||||||
|
end = default_timer()
|
||||||
|
|
||||||
|
print('Project Euler, Problem 24')
|
||||||
|
print('Answer: {}'.format(res))
|
||||||
|
|
||||||
|
print('Elapsed time: {:.9f} seconds'.format(end - start))
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
28
Python/p025.py
Normal file
28
Python/p025.py
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
#!/usr/bin/python3
|
||||||
|
|
||||||
|
from timeit import default_timer
|
||||||
|
|
||||||
|
def main():
|
||||||
|
start = default_timer()
|
||||||
|
|
||||||
|
fib1 = 1
|
||||||
|
fib2 = 1
|
||||||
|
fibn = fib1 + fib2
|
||||||
|
|
||||||
|
i = 3
|
||||||
|
|
||||||
|
while len(str(fibn)) < 1000:
|
||||||
|
fib1 = fib2
|
||||||
|
fib2 = fibn
|
||||||
|
fibn = fib1 + fib2
|
||||||
|
i = i + 1
|
||||||
|
|
||||||
|
end = default_timer()
|
||||||
|
|
||||||
|
print('Project Euler, Problem 25')
|
||||||
|
print('Answer: {}'.format(i))
|
||||||
|
|
||||||
|
print('Elapsed time: {:.9f} seconds'.format(end - start))
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
Loading…
x
Reference in New Issue
Block a user