diff --git a/C/p033.c b/C/p033.c new file mode 100644 index 0000000..ce8509f --- /dev/null +++ b/C/p033.c @@ -0,0 +1,48 @@ +#include +#include +#include +#include "projecteuler.h" + +int main(int argc, char **argv) +{ + int i, j, n, d, prod_n=1, prod_d=1, div; + float f1, f2; + double elapsed; + struct timespec start, end; + + clock_gettime(CLOCK_MONOTONIC, &start); + + for(i = 11; i < 100; i++) + { + for(j = 11; j < 100; j++) + { + if(i % 10 && j % 10 && i != j && i % 10 == j / 10) + { + n = i / 10; + d = j % 10; + + f1 = (float)i / j; + f2 = (float)n / d; + + if(f1 == f2) + { + prod_n *= i; + prod_d *= j; + } + } + } + } + + div = gcd(prod_n, prod_d); + + clock_gettime(CLOCK_MONOTONIC, &end); + + elapsed = (end.tv_sec - start.tv_sec) + (double)(end.tv_nsec - start.tv_nsec) / 1000000000; + + printf("Project Euler, Problem 33\n"); + printf("Answer: %d\n", prod_d/div); + + printf("Elapsed time: %.9lf seconds\n", elapsed); + + return 0; +} diff --git a/C/p034.c b/C/p034.c new file mode 100644 index 0000000..6bae6bd --- /dev/null +++ b/C/p034.c @@ -0,0 +1,68 @@ +#include +#include +#include +#include + +int main(int argc, char **argv) +{ + int i; + unsigned long int digit; + double elapsed; + struct timespec start, end; + mpz_t a, b, q, sum_f, sum, factorials[10]; + + clock_gettime(CLOCK_MONOTONIC, &start); + + mpz_init_set_ui(a, 10); + mpz_init_set_ui(sum, 0); + mpz_inits(b, q, sum_f, sum, NULL); + + for(i = 0; i < 10; i++) + { + mpz_init_set_ui(factorials[i], 1); + } + + for(i = 2; i < 10; i++) + { + mpz_fac_ui(factorials[i], i); + } + + while(mpz_cmp_ui(a, 50000) < 0) + { + mpz_set(b, a); + mpz_set_ui(sum_f, 0); + + while(mpz_cmp_ui(b, 0)) + { + digit = mpz_fdiv_qr_ui(b, q, b, 10); + mpz_add(sum_f, sum_f, factorials[digit]); + } + + if(!mpz_cmp(a, sum_f)) + { + mpz_add(sum, sum, a); + } + + mpz_add_ui(a, a, 1); + } + + mpz_clears(a, b, q, sum_f, NULL); + + for(i = 0; i < 10; i++) + { + mpz_clear(factorials[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 34\n"); + gmp_printf("Answer: %Zd\n", sum); + + printf("Elapsed time: %.9lf seconds\n", elapsed); + + mpz_clear(sum); + + return 0; +} diff --git a/C/p035.c b/C/p035.c new file mode 100644 index 0000000..0399886 --- /dev/null +++ b/C/p035.c @@ -0,0 +1,77 @@ +#include +#include +#include +#include +#include "projecteuler.h" + +#define N 1000000 + +int is_circular_prime(int n); + +int *primes; + +int main(int argc, char **argv) +{ + int i, count = 13; + double elapsed; + struct timespec start, end; + + clock_gettime(CLOCK_MONOTONIC, &start); + + primes = sieve(N); + + for(i = 101; i < 1000000; i += 2) + { + if(is_circular_prime(i)) + { + count++; + } + } + + 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 35\n"); + printf("Answer: %d\n", count); + + printf("Elapsed time: %.9lf seconds\n", elapsed); + + return 0; +} + +int is_circular_prime(int n) +{ + int i, tmp, count; + + if(primes[n] == 0) + { + return 0; + } + + tmp = n; + count = 0; + + while(tmp > 0) + { + if(tmp % 2 == 0) + { + return 0; + } + count++; + tmp /= 10; + } + + for(i = 1; i < count; i++) + { + n = n % (int)pow(10, count-1) * 10 + n / (int)pow(10, count-1); + if(primes[n] == 0) + { + return 0; + } + } + + return 1; +} diff --git a/C/projecteuler.c b/C/projecteuler.c index 277eead..2dac986 100644 --- a/C/projecteuler.c +++ b/C/projecteuler.c @@ -34,9 +34,11 @@ int is_prime(long int num) long int gcd(long int a, long int b) { if(b == 0) + { return a; - else - return gcd(b, a%b); + } + + return gcd(b, a%b); } long int lcm(long int a, long int b) diff --git a/Python/p033.py b/Python/p033.py new file mode 100644 index 0000000..722cab1 --- /dev/null +++ b/Python/p033.py @@ -0,0 +1,36 @@ +#!/usr/bin/python3 + +from timeit import default_timer +from projecteuler import gcd + +def main(): + start = default_timer() + + prod_n = 1 + prod_d = 1 + + for i in range(11, 100): + for j in range(11, 100): + if i % 10 != 0 and j % 10 != 0 and\ + i != j and i % 10 == j // 10: + n = i // 10 + d = j % 10 + + f1 = i / j + f2 = n / d + + if f1 == f2: + prod_n = prod_n * i + prod_d = prod_d * j + + div = gcd(prod_n, prod_d) + + end = default_timer() + + print('Project Euler, Problem 33') + print('Answer: {}'.format(prod_d // div)) + + print('Elapsed time: {:.9f} seconds'.format(end - start)) + +if __name__ == '__main__': + main() diff --git a/Python/p034.py b/Python/p034.py new file mode 100644 index 0000000..3ffb5a7 --- /dev/null +++ b/Python/p034.py @@ -0,0 +1,41 @@ +#!/usr/bin/python3 + +from math import factorial + +from numpy import ones + +from timeit import default_timer + +def main(): + start = default_timer() + + a = 10 + sum_ = 0 + factorials = ones(10, int) + + for i in range(2, 10): + factorials[i] = factorial(i) + + while a < 50000: + b = a + sum_f = 0 + + while b != 0: + digit = b % 10 + b = b // 10 + sum_f = sum_f + factorials[digit] + + if a == sum_f: + sum_ = sum_ + a + + a = a + 1 + + end = default_timer() + + print('Project Euler, Problem 34') + print('Answer: {}'.format(sum_)) + + print('Elapsed time: {:.9f} seconds'.format(end - start)) + +if __name__ == '__main__': + main() diff --git a/Python/p035.py b/Python/p035.py new file mode 100644 index 0000000..83ad763 --- /dev/null +++ b/Python/p035.py @@ -0,0 +1,49 @@ +#!/usr/bin/python3 + +from timeit import default_timer +from projecteuler import sieve + +def is_circular_prime(n): + global primes + + if primes[n] == 0: + return 0 + + tmp = n + count = 0 + + while tmp > 0: + if tmp % 2 == 0: + return 0 + count = count + 1 + tmp = tmp // 10 + + for i in range(1, count): + n = n % (10 ** (count - 1)) * 10 + n // (10 ** (count - 1)) + + if primes[n] == 0: + return 0 + + return 1 + +def main(): + start = default_timer() + + global primes + + primes = sieve(1000000) + count = 13 + + for i in range(101, 1000000, 2): + if is_circular_prime(i): + count = count + 1 + + end = default_timer() + + print('Project Euler, Problem 35') + print('Answer: {}'.format(count)) + + print('Elapsed time: {:.9f} seconds'.format(end - start)) + +if __name__ == '__main__': + main()