diff --git a/C/p026.c b/C/p026.c new file mode 100644 index 0000000..731849e --- /dev/null +++ b/C/p026.c @@ -0,0 +1,66 @@ +#include +#include +#include +#include +#include + +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; +} diff --git a/C/p027.c b/C/p027.c new file mode 100644 index 0000000..55bb471 --- /dev/null +++ b/C/p027.c @@ -0,0 +1,56 @@ +#include +#include +#include +#include +#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; +} diff --git a/C/p028.c b/C/p028.c new file mode 100644 index 0000000..b6a61ca --- /dev/null +++ b/C/p028.c @@ -0,0 +1,36 @@ +#include +#include +#include + +#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; +} diff --git a/C/p029.c b/C/p029.c new file mode 100644 index 0000000..068a95b --- /dev/null +++ b/C/p029.c @@ -0,0 +1,87 @@ +#include +#include +#include +#include + +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); + } +} diff --git a/C/p030.c b/C/p030.c new file mode 100644 index 0000000..2839154 --- /dev/null +++ b/C/p030.c @@ -0,0 +1,42 @@ +#include +#include +#include +#include + +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; +} diff --git a/Python/p026.py b/Python/p026.py new file mode 100644 index 0000000..e00f35e --- /dev/null +++ b/Python/p026.py @@ -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() diff --git a/Python/p027.py b/Python/p027.py new file mode 100644 index 0000000..53ea103 --- /dev/null +++ b/Python/p027.py @@ -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() diff --git a/Python/p028.py b/Python/p028.py new file mode 100644 index 0000000..69f8919 --- /dev/null +++ b/Python/p028.py @@ -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() diff --git a/Python/p029.py b/Python/p029.py new file mode 100644 index 0000000..5cfdc59 --- /dev/null +++ b/Python/p029.py @@ -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() diff --git a/Python/p030.py b/Python/p030.py new file mode 100644 index 0000000..c86cba5 --- /dev/null +++ b/Python/p030.py @@ -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()