From 42511baeee5299ff6fdc47684e7de0d4be6dcb11 Mon Sep 17 00:00:00 2001 From: Daniele Fucini Date: Wed, 25 Sep 2019 19:23:40 +0200 Subject: [PATCH] Add more solutions in C Added solutions for problems 56, 57, 58, 59 and 60 in C. --- C/cipher.txt | 1 + C/p056.c | 50 ++++++++++++++++++ C/p057.c | 81 +++++++++++++++++++++++++++++ C/p058.c | 78 ++++++++++++++++++++++++++++ C/p059.c | 143 +++++++++++++++++++++++++++++++++++++++++++++++++++ C/p060 | Bin 0 -> 22272 bytes C/p060.c | 110 +++++++++++++++++++++++++++++++++++++++ 7 files changed, 463 insertions(+) create mode 100644 C/cipher.txt create mode 100644 C/p056.c create mode 100644 C/p057.c create mode 100644 C/p058.c create mode 100644 C/p059.c create mode 100755 C/p060 create mode 100644 C/p060.c diff --git a/C/cipher.txt b/C/cipher.txt new file mode 100644 index 0000000..b3b3247 --- /dev/null +++ b/C/cipher.txt @@ -0,0 +1 @@ +36,22,80,0,0,4,23,25,19,17,88,4,4,19,21,11,88,22,23,23,29,69,12,24,0,88,25,11,12,2,10,28,5,6,12,25,10,22,80,10,30,80,10,22,21,69,23,22,69,61,5,9,29,2,66,11,80,8,23,3,17,88,19,0,20,21,7,10,17,17,29,20,69,8,17,21,29,2,22,84,80,71,60,21,69,11,5,8,21,25,22,88,3,0,10,25,0,10,5,8,88,2,0,27,25,21,10,31,6,25,2,16,21,82,69,35,63,11,88,4,13,29,80,22,13,29,22,88,31,3,88,3,0,10,25,0,11,80,10,30,80,23,29,19,12,8,2,10,27,17,9,11,45,95,88,57,69,16,17,19,29,80,23,29,19,0,22,4,9,1,80,3,23,5,11,28,92,69,9,5,12,12,21,69,13,30,0,0,0,0,27,4,0,28,28,28,84,80,4,22,80,0,20,21,2,25,30,17,88,21,29,8,2,0,11,3,12,23,30,69,30,31,23,88,4,13,29,80,0,22,4,12,10,21,69,11,5,8,88,31,3,88,4,13,17,3,69,11,21,23,17,21,22,88,65,69,83,80,84,87,68,69,83,80,84,87,73,69,83,80,84,87,65,83,88,91,69,29,4,6,86,92,69,15,24,12,27,24,69,28,21,21,29,30,1,11,80,10,22,80,17,16,21,69,9,5,4,28,2,4,12,5,23,29,80,10,30,80,17,16,21,69,27,25,23,27,28,0,84,80,22,23,80,17,16,17,17,88,25,3,88,4,13,29,80,17,10,5,0,88,3,16,21,80,10,30,80,17,16,25,22,88,3,0,10,25,0,11,80,12,11,80,10,26,4,4,17,30,0,28,92,69,30,2,10,21,80,12,12,80,4,12,80,10,22,19,0,88,4,13,29,80,20,13,17,1,10,17,17,13,2,0,88,31,3,88,4,13,29,80,6,17,2,6,20,21,69,30,31,9,20,31,18,11,94,69,54,17,8,29,28,28,84,80,44,88,24,4,14,21,69,30,31,16,22,20,69,12,24,4,12,80,17,16,21,69,11,5,8,88,31,3,88,4,13,17,3,69,11,21,23,17,21,22,88,25,22,88,17,69,11,25,29,12,24,69,8,17,23,12,80,10,30,80,17,16,21,69,11,1,16,25,2,0,88,31,3,88,4,13,29,80,21,29,2,12,21,21,17,29,2,69,23,22,69,12,24,0,88,19,12,10,19,9,29,80,18,16,31,22,29,80,1,17,17,8,29,4,0,10,80,12,11,80,84,67,80,10,10,80,7,1,80,21,13,4,17,17,30,2,88,4,13,29,80,22,13,29,69,23,22,69,12,24,12,11,80,22,29,2,12,29,3,69,29,1,16,25,28,69,12,31,69,11,92,69,17,4,69,16,17,22,88,4,13,29,80,23,25,4,12,23,80,22,9,2,17,80,70,76,88,29,16,20,4,12,8,28,12,29,20,69,26,9,69,11,80,17,23,80,84,88,31,3,88,4,13,29,80,21,29,2,12,21,21,17,29,2,69,12,31,69,12,24,0,88,20,12,25,29,0,12,21,23,86,80,44,88,7,12,20,28,69,11,31,10,22,80,22,16,31,18,88,4,13,25,4,69,12,24,0,88,3,16,21,80,10,30,80,17,16,25,22,88,3,0,10,25,0,11,80,17,23,80,7,29,80,4,8,0,23,23,8,12,21,17,17,29,28,28,88,65,75,78,68,81,65,67,81,72,70,83,64,68,87,74,70,81,75,70,81,67,80,4,22,20,69,30,2,10,21,80,8,13,28,17,17,0,9,1,25,11,31,80,17,16,25,22,88,30,16,21,18,0,10,80,7,1,80,22,17,8,73,88,17,11,28,80,17,16,21,11,88,4,4,19,25,11,31,80,17,16,21,69,11,1,16,25,2,0,88,2,10,23,4,73,88,4,13,29,80,11,13,29,7,29,2,69,75,94,84,76,65,80,65,66,83,77,67,80,64,73,82,65,67,87,75,72,69,17,3,69,17,30,1,29,21,1,88,0,23,23,20,16,27,21,1,84,80,18,16,25,6,16,80,0,0,0,23,29,3,22,29,3,69,12,24,0,88,0,0,10,25,8,29,4,0,10,80,10,30,80,4,88,19,12,10,19,9,29,80,18,16,31,22,29,80,1,17,17,8,29,4,0,10,80,12,11,80,84,86,80,35,23,28,9,23,7,12,22,23,69,25,23,4,17,30,69,12,24,0,88,3,4,21,21,69,11,4,0,8,3,69,26,9,69,15,24,12,27,24,69,49,80,13,25,20,69,25,2,23,17,6,0,28,80,4,12,80,17,16,25,22,88,3,16,21,92,69,49,80,13,25,6,0,88,20,12,11,19,10,14,21,23,29,20,69,12,24,4,12,80,17,16,21,69,11,5,8,88,31,3,88,4,13,29,80,22,29,2,12,29,3,69,73,80,78,88,65,74,73,70,69,83,80,84,87,72,84,88,91,69,73,95,87,77,70,69,83,80,84,87,70,87,77,80,78,88,21,17,27,94,69,25,28,22,23,80,1,29,0,0,22,20,22,88,31,11,88,4,13,29,80,20,13,17,1,10,17,17,13,2,0,88,31,3,88,4,13,29,80,6,17,2,6,20,21,75,88,62,4,21,21,9,1,92,69,12,24,0,88,3,16,21,80,10,30,80,17,16,25,22,88,29,16,20,4,12,8,28,12,29,20,69,26,9,69,65,64,69,31,25,19,29,3,69,12,24,0,88,18,12,9,5,4,28,2,4,12,21,69,80,22,10,13,2,17,16,80,21,23,7,0,10,89,69,23,22,69,12,24,0,88,19,12,10,19,16,21,22,0,10,21,11,27,21,69,23,22,69,12,24,0,88,0,0,10,25,8,29,4,0,10,80,10,30,80,4,88,19,12,10,19,9,29,80,18,16,31,22,29,80,1,17,17,8,29,4,0,10,80,12,11,80,84,86,80,36,22,20,69,26,9,69,11,25,8,17,28,4,10,80,23,29,17,22,23,30,12,22,23,69,49,80,13,25,6,0,88,28,12,19,21,18,17,3,0,88,18,0,29,30,69,25,18,9,29,80,17,23,80,1,29,4,0,10,29,12,22,21,69,12,24,0,88,3,16,21,3,69,23,22,69,12,24,0,88,3,16,26,3,0,9,5,0,22,4,69,11,21,23,17,21,22,88,25,11,88,7,13,17,19,13,88,4,13,29,80,0,0,0,10,22,21,11,12,3,69,25,2,0,88,21,19,29,30,69,22,5,8,26,21,23,11,94 \ No newline at end of file diff --git a/C/p056.c b/C/p056.c new file mode 100644 index 0000000..c40ca4f --- /dev/null +++ b/C/p056.c @@ -0,0 +1,50 @@ +/* A googol (10^100) is a massive number: one followed by one-hundred zeros; 100^100 is almost unimaginably large: one followed by two-hundred zeros. + * Despite their size, the sum of the digits in each number is only 1. + * + * Considering natural numbers of the form, a^b, where a, b < 100, what is the maximum digital sum?*/ + +#include +#include +#include +#include + +int main(int argc, char **argv) +{ + int a, b, sum, max = 0; + double elapsed; + struct timespec start, end; + mpz_t pow; + + clock_gettime(CLOCK_MONOTONIC, &start); + + mpz_init(pow); + + /* Straightforward brute force approach using the GMP Library.*/ + for(a = 1; a < 100; a++) + { + for(b = 1; b < 100; b++) + { + mpz_ui_pow_ui(pow, a, b); + sum = 0; + + while(mpz_cmp_ui(pow, 0)) + sum += mpz_tdiv_q_ui(pow, pow, 10); + + if(sum > max) + max = sum; + } + } + + mpz_clear(pow); + + clock_gettime(CLOCK_MONOTONIC, &end); + + elapsed = (end.tv_sec - start.tv_sec) + (double)(end.tv_nsec - start.tv_nsec) / 1000000000; + + printf("Project Euler, Problem 56\n"); + printf("Answer: %d\n", max); + + printf("Elapsed time: %.9lf seconds\n", elapsed); + + return 0; +} diff --git a/C/p057.c b/C/p057.c new file mode 100644 index 0000000..bfe11b3 --- /dev/null +++ b/C/p057.c @@ -0,0 +1,81 @@ +/* It is possible to show that the square root of two can be expressed as an infinite continued fraction. + * + * √2=1+1/(2+1/(2+1/(2+… + * + * By expanding this for the first four iterations, we get: + * + * 1+1/2=3/2=1.5 + * 1+1/(2+1/2)=7/5=1.4 + * 1+1/(2+1/(2+1/2))=17/12=1.41666… + * 1+1/(2+1/(2+1/(2+1/2)))=41/29=1.41379… + * + * The next three expansions are 99/70, 239/169, and 577/408, but the eighth expansion, 1393/985, is the first example where the number of digits + * in the numerator exceeds the number of digits in the denominator. + * + * In the first one-thousand expansions, how many fractions contain a numerator with more digits than the denominator?*/ + +#include +#include +#include +#include + +int count_digits(mpz_t n); + +int main(int argc, char **argv) +{ + int i, count = 0; + mpz_t n, d, d2; + double elapsed; + struct timespec start, end; + + clock_gettime(CLOCK_MONOTONIC, &start); + + mpz_init_set_ui(n, 1); + mpz_init_set_ui(d, 1); + mpz_init(d2); + + /* If n/d is the current term of the expansion, the next term can be calculated as + * (n+2d)/(n+d). Using the GMP Library the problem becomes trivial.*/ + for(i = 1; i < 1000; i++) + { + mpz_mul_ui(d2, d, 2); + mpz_add(d, n, d); + mpz_add(n, n, d2); + + if(count_digits(n) > count_digits(d)) + { + count++; + } + } + + mpz_clears(n, d, d2, 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 57\n"); + printf("Answer: %d\n", count); + + printf("Elapsed time: %.9lf seconds\n", elapsed); + + return 0; +} + +int count_digits(mpz_t n) +{ + int count = 0; + mpz_t value; + + mpz_init_set(value, n); + + while(mpz_cmp_ui(value, 0)) + { + mpz_tdiv_q_ui(value, value, 10); + count++; + } + + mpz_clear(value); + + return count; +} diff --git a/C/p058.c b/C/p058.c new file mode 100644 index 0000000..9eeeb15 --- /dev/null +++ b/C/p058.c @@ -0,0 +1,78 @@ +/* Starting with 1 and spiralling anticlockwise in the following way, a square spiral with side length 7 is formed. + * + * 37 36 35 34 33 32 31 + * 38 17 16 15 14 13 30 + * 39 18 5 4 3 12 29 + * 40 19 6 1 2 11 28 + * 41 20 7 8 9 10 27 + * 42 21 22 23 24 25 26 + * 43 44 45 46 47 48 49 + * + * It is interesting to note that the odd squares lie along the bottom right diagonal, but what is more interesting is that 8 out of the 13 numbers + * lying along both diagonals are prime; that is, a ratio of 8/13 ≈ 62%. + * + * If one complete new layer is wrapped around the spiral above, a square spiral with side length 9 will be formed. If this process is continued, + * what is the side length of the square spiral for which the ratio of primes along both diagonals first falls below 10%?*/ + +#include +#include +#include +#include +#include "projecteuler.h" + +int main(int argc, char **argv) +{ + int i = 1, l = 1, step = 2, count = 0, diag = 5; + double ratio, elapsed; + struct timespec start, end; + + clock_gettime(CLOCK_MONOTONIC, &start); + + /* Starting with 1, the next four numbers in the diagonal are 3 (1+2), 5 (1+2+2), 7 (1+2+2+2) + * and 9 (1+2+2+2+2). Check which are prime, increment the counter every time a new prime is + * found, and divide by the number of elements of the diagonal, which are increase by 4 at + * every cycle. The next four number added to the diagonal are 13 (9+4), 17 (9+4+4), 21 and 25. + * Then 25+6 etc., at every cycle the step is increased by 2. Continue until the ratio goes below 0.1.*/ + do + { + i += step; + + if(is_prime(i)) + { + count++; + } + + i += step; + + if(is_prime(i)) + { + count++; + } + + i += step; + + if(is_prime(i)) + { + count++; + } + + i += step; + ratio = (double)count / diag; + + step += 2; + diag += 4; + l += 2; + + }while(ratio >= 0.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 58\n"); + printf("Answer: %d\n", l); + + printf("Elapsed time: %.9lf seconds\n", elapsed); + + return 0; +} diff --git a/C/p059.c b/C/p059.c new file mode 100644 index 0000000..52dca01 --- /dev/null +++ b/C/p059.c @@ -0,0 +1,143 @@ +/* Each character on a computer is assigned a unique code and the preferred standard is ASCII (American Standard Code for Information Interchange). + * For example, uppercase A = 65, asterisk (*) = 42, and lowercase k = 107. + * + * A modern encryption method is to take a text file, convert the bytes to ASCII, then XOR each byte with a given value, taken from a secret key. + * The advantage with the XOR function is that using the same encryption key on the cipher text, restores the plain text; for example, 65 XOR 42 = 107, + * then 107 XOR 42 = 65. + * + * For unbreakable encryption, the key is the same length as the plain text message, and the key is made up of random bytes. The user would keep + * the encrypted message and the encryption key in different locations, and without both "halves", it is impossible to decrypt the message. + * + * Unfortunately, this method is impractical for most users, so the modified method is to use a password as a key. If the password is shorter than + * the message, which is likely, the key is repeated cyclically throughout the message. The balance for this method is using a sufficiently long + * password key for security, but short enough to be memorable. + * + * Your task has been made easy, as the encryption key consists of three lower case characters. Using cipher.txt, a file containing the + * encrypted ASCII codes, and the knowledge that the plain text must contain common English words, decrypt the message and find the sum of the + * ASCII values in the original text.*/ + +#include +#include +#include +#include + +int main(int argc, char **argv) +{ + int i, j, n, sum, found = 0; + double elapsed; + char c1, c2, c3, dummy; + char *enc_text, *plain_text; + FILE *fp; + struct timespec start, end; + + clock_gettime(CLOCK_MONOTONIC, &start); + + if((fp = fopen("cipher.txt", "r")) == NULL) + { + fprintf(stderr, "Error while opening file %s\n", "cipher.txt"); + return 1; + } + + n = 0; + + /* Count the number of characters (i.e. ASCII values) in the file.*/ + while(fscanf(fp, "%d%c", &i, &dummy) != EOF) + { + n++; + } + + fclose(fp); + + if((enc_text = (char *)malloc(n*sizeof(char))) == NULL) + { + fprintf(stderr, "Error while allocating memory\n"); + return 1; + } + + if((plain_text = (char *)malloc((n+1)*sizeof(char))) == NULL) + { + fprintf(stderr, "Error while allocating memory\n"); + return 1; + } + + if((fp = fopen("cipher.txt", "r")) == NULL) + { + fprintf(stderr, "Error while opening file %s\n", "cipher.txt"); + return 1; + } + + /* Save the encrypted text.*/ + for(i = 0; i < n; i++) + { + fscanf(fp, "%d%c", &j, &dummy); + enc_text[i] = (char)j; + } + + fclose(fp); + + /* Try every combination of three lowercase letters until the key is found.*/ + for(c1 = 'a'; c1 <= 'z' && !found; c1++) + { + for(c2 = 'a'; c2 <= 'z' && !found; c2++) + { + for(c3 = 'a'; c3 <= 'z' && !found; c3++) + { + /* Try the current key.*/ + for(i = 0; i < n - 2; i += 3) + { + plain_text[i] = enc_text[i] ^ c1; + plain_text[i+1] = enc_text[i+1] ^ c2; + plain_text[i+2] = enc_text[i+2] ^ c3; + } + + /* Since a step of 3 was used, the last two characters or + * just the last one might not have been decrypted yet.*/ + if(i == n - 2) + { + plain_text[i] = enc_text[i] ^ c1; + plain_text[i+1] = enc_text[i+1] ^ c2; + } + + if(i == n - 1) + { + plain_text[i] = enc_text[i] ^ c1; + } + + /* Set string terminator.*/ + plain_text[n] = 0; + + /* Check if the decrypted text contains the 8 most common English words. If it does, + * the right key was likely found.*/ + if(strstr(plain_text, "the") != NULL && strstr(plain_text, "be") != NULL && + strstr(plain_text, "to") != NULL && strstr(plain_text, "of") != NULL && + strstr(plain_text, "and") != NULL & strstr(plain_text, "in") != NULL && + strstr(plain_text, "that") != NULL && strstr(plain_text, "have") != NULL) + { + sum = 0; + + /* Sum the ASCII values.*/ + for(i = 0; i < n; i++) + { + sum += (int)plain_text[i]; + } + + found = 1; + } + } + } + } + + free(enc_text); + free(plain_text); + + clock_gettime(CLOCK_MONOTONIC, &end); + + elapsed=(end.tv_sec-start.tv_sec)+(double)(end.tv_nsec-start.tv_nsec)/1000000000; + + printf("Project Euler, Problem 59\n"); + printf("Answer: %d\n", sum); + + printf("Elapsed time: %.9lf seconds\n", elapsed); + + return 0; +} diff --git a/C/p060 b/C/p060 new file mode 100755 index 0000000000000000000000000000000000000000..07f349a863db2c27549f4b36c355b8ca477727e0 GIT binary patch literal 22272 zcmeHP4Rl;poxhXxBbFwUmK1}49ShYIX-Me@Ev1l@zVga8G(v-@w9`pu(hOuWVLl-3 zY1%GBS>KL<>beRD+SQ}m^{7P5x&bRpQnn4O?heRuf>cehzzhj>s6mDrnEn0l$IE+@ zjO*Fs*>gN6Hz)JI_y2Y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Bm#Q8Yi5yn?TTVwE?~ zlWK)XLICqq$tn0dP^|Lm`7u!|M3kLMfs#|@caR~RvS019leL0FjWbbV%%w6Co&}`( zWw@DCC7*Zf#CeH{WGE5L3lWY_)pEcN%Y$qU8zX71{p)r9}1l literal 0 HcmV?d00001 diff --git a/C/p060.c b/C/p060.c new file mode 100644 index 0000000..4687f17 --- /dev/null +++ b/C/p060.c @@ -0,0 +1,110 @@ +/* The primes 3, 7, 109, and 673, are quite remarkable. By taking any two primes and concatenating them in any order the result will always be prime. + * For example, taking 7 and 109, both 7109 and 1097 are prime. The sum of these four primes, 792, represents the lowest sum for a set of four primes + * with this property. + * + * Find the lowest sum for a set of five primes for which any two primes concatenate to produce another prime.*/ + +#include +#include +#include +#include +#include "projecteuler.h" + +#define N 10000 + +int cat(int i, int j); + +int *primes; + +int main(int argc, char **argv) +{ + int found = 0, p1, p2, p3, p4, p5, n; + double elapsed; + struct timespec start, end; + + clock_gettime(CLOCK_MONOTONIC, &start); + + primes = sieve(N); + + /* Straightforward brute force approach.*/ + for(p1 = 3; p1 < N && !found; p1 += 2) + { + /* If p1 is not prime, go to next number.*/ + if(!primes[p1]) + { + continue; + } + + for(p2 = p1 + 2; p2 < N && !found; p2 += 2) + { + /* If p2 is not prime, or at least one of the possible concatenations of + * p1 and p2 is not prime, go to the next number.*/ + if(!primes[p2] || !is_prime(cat(p1, p2)) || !is_prime(cat(p2, p1))) + { + continue; + } + + for(p3 = p2 + 2; p3 < N && !found; p3 += 2) + { + /* If p3 is not prime, or at least one of the possible concatenations of + * p1, p2 and p3 is not prime, go to the next number.*/ + if(!primes[p3] || !is_prime(cat(p1, p3)) || !is_prime(cat(p3, p1)) || + !is_prime(cat(p2, p3)) || !is_prime(cat(p3, p2))) + { + continue; + } + + for(p4 = p3 + 2; p4 < N && !found; p4 += 2) + { + /* If p4 is not prime, or at least one of the possible concatenations of + * p1, p2, p3 and p4 is not prime, go to the next number.*/ + if(!primes[p4] || !is_prime(cat(p1, p4)) || !is_prime(cat(p4, p1)) || + !is_prime(cat(p2, p4)) || !is_prime(cat(p4, p2)) || + !is_prime(cat(p3, p4)) || !is_prime(cat(p4, p3))) + { + continue; + } + + for(p5 = p4 + 2; p5 < N && !found; p5 += 2) + { + /* If p5 is not prime, or at least one of the possible concatenations of + * p1, p2, p3, p4 and p5 is not prime, go to the next number.*/ + if(!primes[p5] || !is_prime(cat(p1, p5)) || !is_prime(cat(p5, p1)) || + !is_prime(cat(p2, p5)) || !is_prime(cat(p5, p2)) || + !is_prime(cat(p3, p5)) || !is_prime(cat(p5, p3)) || + !is_prime(cat(p4, p5)) || !is_prime(cat(p5, p4))) + { + continue; + } + + /* If it gets here, the five values have been found.*/ + n = p1 + p2 + p3 + p4 + p5; + found = 1; + } + } + } + } + } + + 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 60\n"); + printf("Answer: %d\n", n); + + printf("Elapsed time: %.9lf seconds\n", elapsed); + + return 0; +} + +int cat(int i, int j) +{ + char n[10]; + + sprintf(n, "%d%d", i, j); + + return atoi(n); +}