From 85c692bd1bc5f20fd7a35d6f7c5241fe31a2c6b7 Mon Sep 17 00:00:00 2001 From: Daniele Fucini Date: Tue, 24 Sep 2019 21:29:00 +0200 Subject: [PATCH] Add comments Added comments for problems from 26 to 30 in C. --- C/p026.c | 30 +++++++++++++++++++++++---- C/p027.c | 63 +++++++++++++++++++++++++++++++++++++++----------------- C/p028.c | 16 ++++++++++++++ C/p029.c | 15 ++++++++++++++ C/p030.c | 15 ++++++++++++++ 5 files changed, 116 insertions(+), 23 deletions(-) diff --git a/C/p026.c b/C/p026.c index 731849e..80519f4 100644 --- a/C/p026.c +++ b/C/p026.c @@ -1,3 +1,19 @@ +/* A unit fraction contains 1 in the numerator. The decimal representation of the unit fractions with denominators 2 to 10 are given: + * + * 1/2 = 0.5 + * 1/3 = 0.(3) + * 1/4 = 0.25 + * 1/5 = 0.2 + * 1/6 = 0.1(6) + * 1/7 = 0.(142857) + * 1/8 = 0.125 + * 1/9 = 0.(1) + * 1/10 = 0.1 + * + * Where 0.1(6) means 0.166666..., and has a 1-digit recurring cycle. It can be seen that 1/7 has a 6-digit recurring cycle. + * + * Find the value of d < 1000 for which 1/d contains the longest recurring cycle in its decimal fraction part.*/ + #include #include #include @@ -20,22 +36,28 @@ int main(int argc, char **argv) { j = i; + /* The repeating cycle of 1/(2^a*5^b*p^c*...) is equal to + * that of 1/p^c*..., so factors 2 and 5 can be eliminated.*/ while(j % 2 == 0 && j > 1) j /= 2; while(j % 5 == 0 && j > 1) j /= 5; - mpz_set_ui(k, 9); - + /* If the denominator had only factors 2 and 5, there is no + * repeating cycle.*/ if(j == 1) n = 0; else { n = 1; - + mpz_set_ui(k, 9); mpz_set_ui(div, j); + /* After eliminating factors 2s and 5s, the length of the repeating cycle + * of 1/d is the smallest n for which k=10^n-1/d is an integer. So we start + * with k=9, then k=99, k=999 and so on until k is divisible by d. + * The number of digits of k (n) is the length of the repeating cycle.*/ while(!mpz_divisible_p(k, div)) { n++; @@ -55,7 +77,7 @@ int main(int argc, char **argv) clock_gettime(CLOCK_MONOTONIC, &end); - elapsed=(end.tv_sec-start.tv_sec)+(double)(end.tv_nsec-start.tv_nsec)/1000000000; + 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); diff --git a/C/p027.c b/C/p027.c index 55bb471..2487739 100644 --- a/C/p027.c +++ b/C/p027.c @@ -1,3 +1,22 @@ +/* Euler discovered the remarkable quadratic formula: + * + * n^2+n+41 + * + * It turns out that the formula will produce 40 primes for the consecutive integer values 0≤n≤39. However, when n=40,402+40+41=40(40+1)+41 is + * divisible by 41, and certainly when n=41,412+41+41 is clearly divisible by 41. + * + * The incredible formula n^2−79n+1601 was discovered, which produces 80 primes for the consecutive values 0≤n≤79. + * The product of the coefficients, −79 and 1601, is −126479. + * + * Considering quadratics of the form: + * + * n^2+an+b, where |a|<1000 and |b|≤1000 + * + * where |n| is the modulus/absolute value of n + * e.g. |11|=11 and |−4|=4 + * + * Find the product of the coefficients, a and b, for the quadratic expression that produces the maximum number of primes for consecutive values of n, starting with n=0.*/ + #include #include #include @@ -7,38 +26,44 @@ int main(int argc, char **argv) { int a, b, n, p, count, max = 0, save_a, save_b; + int *primes; double elapsed; struct timespec start, end; clock_gettime(CLOCK_MONOTONIC, &start); + /* Brute force approach, optimized by checking only values of b where b is prime.*/ for(a = -999; a < 1000; a++) { for(b = 2; b <= 1000; b++) { - n = 0; - count = 0; - - while(1) + /* For n=0, n^2+an+b=b, so b must be prime.*/ + if(is_prime(b)) { - p = n * n + a * n + b; + n = 0; + count = 0; - if(p > 1 && is_prime(p)) + while(1) { - count++; - n++; - } - else - { - break; - } - } + p = n * n + a * n + b; - if(count > max) - { - max = count; - save_a = a; - save_b = b; + if(p > 1 && is_prime(p)) + { + count++; + n++; + } + else + { + break; + } + } + + if(count > max) + { + max = count; + save_a = a; + save_b = b; + } } } } diff --git a/C/p028.c b/C/p028.c index b6a61ca..7592979 100644 --- a/C/p028.c +++ b/C/p028.c @@ -1,3 +1,15 @@ +/* Starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral is formed as follows: + * + * 21 22 23 24 25 + * 20 7 8 9 10 + * 19 6 1 2 11 + * 18 5 4 3 12 + * 17 16 15 14 13 + * + * It can be verified that the sum of the numbers on the diagonals is 101. + * + * What is the sum of the numbers on the diagonals in a 1001 by 1001 spiral formed in the same way?*/ + #include #include #include @@ -12,6 +24,10 @@ int main(int argc, char **argv) clock_gettime(CLOCK_MONOTONIC, &start); + /* Starting with the central 1, it's easy to see that the next four numbers in the diagonal + * are 1+2, 1+2+2, 1+2+2+2 and 1+2+2+2+2, then for the next four number the step is increased + * by two, so from 9 to 9+4, 9+4+4 etc, for the next four number the step is again increased + * by two, and so on. We go on until the value is equal to N*N, with N=1001 for this problem.*/ for(i = 0, j = 1; j < limit; i = (i + 1) % 4) { if(i == 0) diff --git a/C/p029.c b/C/p029.c index 277ffad..68a321e 100644 --- a/C/p029.c +++ b/C/p029.c @@ -1,3 +1,16 @@ +/* Consider all integer combinations of ab for 2 ≤ a ≤ 5 and 2 ≤ b ≤ 5: + * + * 2^2=4, 2^3=8, 2^4=16, 2^5=32 + * 3^2=9, 3^3=27, 3^4=81, 3^5=243 + * 4^2=16, 4^3=64, 4^4=256, 4^5=1024 + * 5^2=25, 5^3=125, 5^4=625, 5^5=3125 + * + * If they are then placed in numerical order, with any repeats removed, we get the following sequence of 15 distinct terms: + * + * 4, 8, 9, 16, 25, 27, 32, 64, 81, 125, 243, 256, 625, 1024, 3125 + * + * How many distinct terms are in the sequence generated by ab for 2 ≤ a ≤ 100 and 2 ≤ b ≤ 100?*/ + #include #include #include @@ -38,6 +51,7 @@ int main(int argc, char **argv) mpz_init(*powers[i]); } + /* Using the GMP Library to calculate all the powers.*/ for(i = 2; i <= 100; i++) { mpz_set_ui(a, i); @@ -49,6 +63,7 @@ int main(int argc, char **argv) mpz_clear(a); + /* Sort the values and count the different values.*/ quick_sort((void **)powers, 0, 9800, compare); count = 1; diff --git a/C/p030.c b/C/p030.c index 2839154..0c6f361 100644 --- a/C/p030.c +++ b/C/p030.c @@ -1,3 +1,15 @@ +/* Surprisingly there are only three numbers that can be written as the sum of fourth powers of their digits: + * + * 1634 = 1^4 + 6^4 + 3^4 + 4^4 + * 8208 = 8^4 + 2^4 + 0^4 + 8^4 + * 9474 = 9^4 + 4^4 + 7^4 + 4^4 + * + * As 1 = 1^4 is not a sum it is not included. + * + * The sum of these numbers is 1634 + 8208 + 9474 = 19316. + * + * Find the sum of all the numbers that can be written as the sum of fifth powers of their digits.*/ + #include #include #include @@ -11,6 +23,9 @@ int main(int argc, char **argv) clock_gettime(CLOCK_MONOTONIC, &start); + /* Straightforward brute force approach. The limit is chosen considering that + * 6*9^5=354294, so no number larger than that can be expressed as sum + * of 5th power of its digits.*/ for(i = 10; i < 354295; i++) { j = i;