diff --git a/C/p206.c b/C/p206.c new file mode 100644 index 0000000..6cccf8c --- /dev/null +++ b/C/p206.c @@ -0,0 +1,60 @@ +/* Find the unique positive integer whose square has the form 1_2_3_4_5_6_7_8_9_0, + * where each “_” is a single digit.*/ + +#include +#include +#include + +int main(int argc, char **argv) +{ + int i, found = 0; + /* Since the square of n has 19 digits, n must be at least 10^9.*/ + long int n = 1e9, p; + double elapsed; + struct timespec start, end; + + clock_gettime(CLOCK_MONOTONIC, &start); + + while(!found) + { + /* If the square on n ends with 10, n must be divisible by 10.*/ + n += 10; + p = n * n; + + /* A square divisible by 10 is also divisible by 100.*/ + if(p % 100 != 0) + { + continue; + } + + /* Check if the digits of the square correspond to the given pattern.*/ + i = 9; + p /= 100; + + while(p > 0) + { + if(p % 10 != i) + { + break; + } + p /= 100; + i--; + } + + if(p == 0) + { + 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 206\n"); + printf("Answer: %d\n", n); + + printf("Elapsed time: %.9lf seconds\n", elapsed); + + return 0; +} diff --git a/C/p357.c b/C/p357.c new file mode 100644 index 0000000..33c8096 --- /dev/null +++ b/C/p357.c @@ -0,0 +1,78 @@ +/* Consider the divisors of 30: 1,2,3,5,6,10,15,30. + * It can be seen that for every divisor d of 30, d+30/d is prime. + * + * Find the sum of all positive integers n not exceeding 100 000 000 + * such that for every divisor d of n, d+n/d is prime.*/ + +#include +#include +#include +#include +#include "projecteuler.h" + +#define N 100000000 + +int check_d_nd_prime(int n); + +int *primes; + +int main(int argc, char **argv) +{ + int i; + long int sum = 0; + double elapsed; + struct timespec start, end; + + clock_gettime(CLOCK_MONOTONIC, &start); + + if((primes = sieve(N+2)) == NULL) + { + fprintf(stderr, "Error! Sieve function returned NULL\n"); + return 1; + } + + for(i = 2; i <= N; i += 2) + { + /* Every number is divisible by 1, so 1+n/1=n+1 must be prime.*/ + if(primes[i+1] && check_d_nd_prime(i)) + { + sum += 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 357\n"); + printf("Answer: %ld\n", sum); + + printf("Elapsed time: %.9lf seconds\n", elapsed); + + return 0; +} + +int check_d_nd_prime(int n) +{ + int i, limit; + + /* To get all divisors, it's sufficient to loop up to the square root + * of the number, because for every divisor d smaller than sqrt(n) n/d + * is also a divisor larger than sqrt(n).*/ + limit = floor(sqrt(n)); + + for(i = 2; i <= limit; i++) + { + if(n % i == 0) + { + /* We only need to check the property for i and not n/i. + * If d=n/i, we would have to check if n/i+n/(n/i)=n/i+i is prime.*/ + if(!primes[i+n/i]) + { + return 0; + } + } + } + + return 1; +} diff --git a/Python/p206.py b/Python/p206.py new file mode 100644 index 0000000..e65da35 --- /dev/null +++ b/Python/p206.py @@ -0,0 +1,46 @@ +#!/usr/bin/python3 + +# Find the unique positive integer whose square has the form 1_2_3_4_5_6_7_8_9_0, +# where each “_” is a single digit. + +from timeit import default_timer + +def main(): + start = default_timer() + +# Since the square of n has 19 digits, n must be at least 10^9. + n = 1000000000 + found = 0 + + while not found: +# If the square on n ends with 10, n must be divisible by 10. + n = n + 10 + p = n * n + +# A square divisible by 10 is also divisible by 100. + if p % 100 != 0: + continue + +# Check if the digits of the square correspond to the given pattern. + i = 9 + p = p // 100 + + while p > 0: + if p % 10 != i: + break + p = p // 100 + i = i - 1 + + if p == 0: + found = 1 + + end = default_timer() + + print('Project Euler, Problem 206') + print('Answer: {}'.format(n)) + + print('Elapsed time: {:.9f} seconds'.format(end - start)) + +if __name__ == '__main__': + main() +