Add more solutions
Added solutions for problems 26, 27, 28, 29 and 30, both in C and python
This commit is contained in:
parent
6b29655330
commit
c3804247b4
66
C/p026.c
Normal file
66
C/p026.c
Normal file
@ -0,0 +1,66 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
#include <time.h>
|
||||
#include <gmp.h>
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int i, j, n, max = 0, max_n = 0;
|
||||
double elapsed;
|
||||
struct timespec start, end;
|
||||
mpz_t k, div;
|
||||
|
||||
clock_gettime(CLOCK_MONOTONIC, &start);
|
||||
|
||||
mpz_init(k);
|
||||
mpz_init(div);
|
||||
|
||||
for(i = 2; i < 1000; i++)
|
||||
{
|
||||
j = i;
|
||||
|
||||
while(j % 2 == 0 && j > 1)
|
||||
j /= 2;
|
||||
|
||||
while(j % 5 == 0 && j > 1)
|
||||
j /= 5;
|
||||
|
||||
mpz_set_ui(k, 9);
|
||||
|
||||
if(j == 1)
|
||||
n = 0;
|
||||
else
|
||||
{
|
||||
n = 1;
|
||||
|
||||
mpz_set_ui(div, j);
|
||||
|
||||
while(!mpz_divisible_p(k, div))
|
||||
{
|
||||
n++;
|
||||
mpz_mul_ui(k, k, 10);
|
||||
mpz_add_ui(k, k, 9);
|
||||
}
|
||||
|
||||
if(n > max)
|
||||
{
|
||||
max = n;
|
||||
max_n = i;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mpz_clears(k, div, 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 26\n");
|
||||
printf("Answer: %d\n", max_n);
|
||||
|
||||
printf("Elapsed time: %.9lf seconds\n", elapsed);
|
||||
|
||||
return 0;
|
||||
}
|
56
C/p027.c
Normal file
56
C/p027.c
Normal file
@ -0,0 +1,56 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
#include <time.h>
|
||||
#include "projecteuler.h"
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int a, b, n, p, count, max = 0, save_a, save_b;
|
||||
double elapsed;
|
||||
struct timespec start, end;
|
||||
|
||||
clock_gettime(CLOCK_MONOTONIC, &start);
|
||||
|
||||
for(a = -999; a < 1000; a++)
|
||||
{
|
||||
for(b = 2; b <= 1000; b++)
|
||||
{
|
||||
n = 0;
|
||||
count = 0;
|
||||
|
||||
while(1)
|
||||
{
|
||||
p = n * n + a * n + b;
|
||||
|
||||
if(p > 1 && is_prime(p))
|
||||
{
|
||||
count++;
|
||||
n++;
|
||||
}
|
||||
else
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(count > max)
|
||||
{
|
||||
max = count;
|
||||
save_a = a;
|
||||
save_b = b;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
clock_gettime(CLOCK_MONOTONIC, &end);
|
||||
|
||||
elapsed = (end.tv_sec - start.tv_sec) + (double)(end.tv_nsec - start.tv_nsec) / 1000000000;
|
||||
|
||||
printf("Project Euler, Problem 27\n");
|
||||
printf("Answer: %d\n", save_a * save_b);
|
||||
|
||||
printf("Elapsed time: %.9lf seconds\n", elapsed);
|
||||
|
||||
return 0;
|
||||
}
|
36
C/p028.c
Normal file
36
C/p028.c
Normal file
@ -0,0 +1,36 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
#define N 1001
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int i, j, step = 0, limit = N * N, sum = 1;
|
||||
double elapsed;
|
||||
struct timespec start, end;
|
||||
|
||||
clock_gettime(CLOCK_MONOTONIC, &start);
|
||||
|
||||
for(i = 0, j = 1; j < limit; i = (i + 1) % 4)
|
||||
{
|
||||
if(i == 0)
|
||||
{
|
||||
step += 2;
|
||||
}
|
||||
|
||||
j += step;
|
||||
sum += j;
|
||||
}
|
||||
|
||||
clock_gettime(CLOCK_MONOTONIC, &end);
|
||||
|
||||
elapsed = (end.tv_sec - start.tv_sec) + (double)(end.tv_nsec - start.tv_nsec) / 1000000000;
|
||||
|
||||
printf("Project Euler, Problem 28\n");
|
||||
printf("Answer: %d\n", sum);
|
||||
|
||||
printf("Elapsed time: %.9lf seconds\n", elapsed);
|
||||
|
||||
return 0;
|
||||
}
|
87
C/p029.c
Normal file
87
C/p029.c
Normal file
@ -0,0 +1,87 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <gmp.h>
|
||||
|
||||
void selection_sort(mpz_t *v, int n);
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
mpz_t a;
|
||||
mpz_t powers[9801];
|
||||
int i, j, count;
|
||||
double elapsed;
|
||||
struct timespec start, end;
|
||||
|
||||
clock_gettime(CLOCK_MONOTONIC, &start);
|
||||
|
||||
mpz_init(a);
|
||||
|
||||
for(i = 0; i < 9801; i++)
|
||||
{
|
||||
mpz_init(powers[i]);
|
||||
}
|
||||
|
||||
for(i = 2; i <= 100; i++)
|
||||
{
|
||||
mpz_set_ui(a, i);
|
||||
for(j = 2; j <= 100; j++)
|
||||
{
|
||||
mpz_pow_ui(powers[(i-2)*99+j-2], a, j);
|
||||
}
|
||||
}
|
||||
|
||||
mpz_clear(a);
|
||||
|
||||
selection_sort(powers, 9801);
|
||||
count = 1;
|
||||
|
||||
for(i = 1; i < 9801; i++)
|
||||
{
|
||||
if(mpz_cmp(powers[i], powers[i-1]))
|
||||
{
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
for(i = 0; i < 9801; i++)
|
||||
{
|
||||
mpz_clear(powers[i]);
|
||||
}
|
||||
|
||||
clock_gettime(CLOCK_MONOTONIC, &end);
|
||||
|
||||
elapsed = (end.tv_sec - start.tv_sec) + (double)(end.tv_nsec - start.tv_nsec) / 1000000000;
|
||||
|
||||
printf("Project Euler, Problem 29\n");
|
||||
printf("Answer: %d\n", count);
|
||||
|
||||
printf("Elapsed time: %.9lf seconds\n", elapsed);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void selection_sort(mpz_t *v, int n)
|
||||
{
|
||||
int i, j, min;
|
||||
mpz_t tmp;
|
||||
|
||||
mpz_init(tmp);
|
||||
|
||||
for(i = 0; i < n - 1; i++)
|
||||
{
|
||||
min = i;
|
||||
|
||||
for(j = i + 1; j < n; j++)
|
||||
{
|
||||
if(mpz_cmp(v[j], v[min])<0)
|
||||
{
|
||||
min = j;
|
||||
}
|
||||
}
|
||||
|
||||
mpz_set(tmp, v[min]);
|
||||
mpz_set(v[min], v[i]);
|
||||
mpz_set(v[i], tmp);
|
||||
}
|
||||
}
|
42
C/p030.c
Normal file
42
C/p030.c
Normal file
@ -0,0 +1,42 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
#include <time.h>
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int i, j, digit, sum, sum_tot = 0;
|
||||
double elapsed;
|
||||
struct timespec start, end;
|
||||
|
||||
clock_gettime(CLOCK_MONOTONIC, &start);
|
||||
|
||||
for(i = 10; i < 354295; i++)
|
||||
{
|
||||
j = i;
|
||||
sum = 0;
|
||||
|
||||
while(j > 0)
|
||||
{
|
||||
digit = j % 10;
|
||||
sum += (pow(digit, 5));
|
||||
j /= 10;
|
||||
}
|
||||
|
||||
if(sum == i)
|
||||
{
|
||||
sum_tot += i;
|
||||
}
|
||||
}
|
||||
|
||||
clock_gettime(CLOCK_MONOTONIC, &end);
|
||||
|
||||
elapsed=(end.tv_sec-start.tv_sec)+(double)(end.tv_nsec-start.tv_nsec)/1000000000;
|
||||
|
||||
printf("Project Euler, problem 30\n");
|
||||
printf("Answer: %d\n", sum_tot);
|
||||
|
||||
printf("Elapsed time: %.9lf seconds\n", elapsed);
|
||||
|
||||
return 0;
|
||||
}
|
43
Python/p026.py
Normal file
43
Python/p026.py
Normal file
@ -0,0 +1,43 @@
|
||||
#!/usr/bin/python3
|
||||
|
||||
from timeit import default_timer
|
||||
|
||||
def main():
|
||||
start = default_timer()
|
||||
|
||||
max_ = 0
|
||||
|
||||
for i in range(2, 1000):
|
||||
j = i
|
||||
|
||||
while j % 2 == 0 and j > 1:
|
||||
j = j // 2
|
||||
|
||||
while j % 5 == 0 and j > 1:
|
||||
j = j // 5
|
||||
|
||||
k = 9
|
||||
|
||||
if j == 1:
|
||||
n = 0
|
||||
else:
|
||||
n = 1
|
||||
div = j
|
||||
while k % div != 0:
|
||||
n = n + 1
|
||||
k = k * 10
|
||||
k = k + 9
|
||||
|
||||
if n > max_:
|
||||
max_ = n
|
||||
max_n = i
|
||||
|
||||
end = default_timer()
|
||||
|
||||
print('Project Euler, Problem 26')
|
||||
print('Answer: {}'.format(max_n))
|
||||
|
||||
print('Elapsed time: {:.9f} seconds'.format(end - start))
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
38
Python/p027.py
Normal file
38
Python/p027.py
Normal file
@ -0,0 +1,38 @@
|
||||
#!/usr/bin/python3
|
||||
|
||||
from timeit import default_timer
|
||||
from projecteuler import is_prime
|
||||
|
||||
def main():
|
||||
start = default_timer()
|
||||
|
||||
max_ = 0
|
||||
|
||||
for a in range(-999, 1000):
|
||||
for b in range(2, 1001):
|
||||
n = 0
|
||||
count = 0
|
||||
|
||||
while True:
|
||||
p = n * n + a * n + b
|
||||
|
||||
if p > 1 and is_prime(p):
|
||||
count = count + 1
|
||||
n = n + 1
|
||||
else:
|
||||
break
|
||||
|
||||
if count > max_:
|
||||
max_ = count
|
||||
save_a = a
|
||||
save_b = b
|
||||
|
||||
end = default_timer()
|
||||
|
||||
print('Project Euler, Problem 27')
|
||||
print('Answer: {}'.format(save_a * save_b))
|
||||
|
||||
print('Elapsed time: {:.9f} seconds'.format(end - start))
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
30
Python/p028.py
Normal file
30
Python/p028.py
Normal file
@ -0,0 +1,30 @@
|
||||
#!/usr/bin/python3
|
||||
|
||||
from timeit import default_timer
|
||||
|
||||
def main():
|
||||
start = default_timer()
|
||||
|
||||
limit = 1001 * 1001
|
||||
|
||||
i = 0
|
||||
j = 1
|
||||
step = 0
|
||||
sum_ = 1
|
||||
|
||||
while j < limit:
|
||||
if i == 0:
|
||||
step = step + 2
|
||||
j = j + step
|
||||
sum_ = sum_ + j
|
||||
i = (i + 1) % 4
|
||||
|
||||
end = default_timer()
|
||||
|
||||
print('Project Euler, Problem 28')
|
||||
print('Answer: {}'.format(sum_))
|
||||
|
||||
print('Elapsed time: {:.9f} seconds'.format(end - start))
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
34
Python/p029.py
Normal file
34
Python/p029.py
Normal file
@ -0,0 +1,34 @@
|
||||
#!/usr/bin/python3
|
||||
|
||||
from numpy import zeros
|
||||
|
||||
from timeit import default_timer
|
||||
|
||||
def main():
|
||||
start = default_timer()
|
||||
|
||||
powers = zeros(9801)
|
||||
|
||||
for i in range(2, 101):
|
||||
a = i
|
||||
for j in range(2, 101):
|
||||
powers[(i-2)*99+j-2] = a ** j
|
||||
|
||||
powers = list(powers)
|
||||
powers.sort()
|
||||
|
||||
count = 1
|
||||
|
||||
for i in range(1, 9801):
|
||||
if powers[i] != powers[i-1]:
|
||||
count = count + 1
|
||||
|
||||
end = default_timer()
|
||||
|
||||
print('Project Euler, Problem 29')
|
||||
print('Answer: {}'.format(count))
|
||||
|
||||
print('Elapsed time: {:.9f} seconds'.format(end - start))
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
32
Python/p030.py
Normal file
32
Python/p030.py
Normal file
@ -0,0 +1,32 @@
|
||||
#!/usr/bin/python3
|
||||
|
||||
from numpy import zeros
|
||||
|
||||
from timeit import default_timer
|
||||
|
||||
def main():
|
||||
start = default_timer()
|
||||
|
||||
tot = 0
|
||||
|
||||
for i in range(10, 354295):
|
||||
j = i
|
||||
sum_ = 0
|
||||
|
||||
while j > 0:
|
||||
digit = j % 10
|
||||
sum_ = sum_ + digit ** 5
|
||||
j = j // 10
|
||||
|
||||
if sum_ == i:
|
||||
tot = tot + i
|
||||
|
||||
end = default_timer()
|
||||
|
||||
print('Project Euler, Problem 30')
|
||||
print('Answer: {}'.format(tot))
|
||||
|
||||
print('Elapsed time: {:.9f} seconds'.format(end - start))
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
Loading…
x
Reference in New Issue
Block a user