Add more solutions in C

Added solutions for problems 56, 57, 58, 59 and 60 in C.
This commit is contained in:
daniele 2019-09-25 19:23:40 +02:00
parent f017e59241
commit 42511baeee
Signed by: fuxino
GPG Key ID: 6FE25B4A3EE16FDA
7 changed files with 463 additions and 0 deletions

1
C/cipher.txt Normal file
View File

@ -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

50
C/p056.c Normal file
View File

@ -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 <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <gmp.h>
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;
}

81
C/p057.c Normal file
View File

@ -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 <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <gmp.h>
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;
}

78
C/p058.c Normal file
View File

@ -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 <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#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;
}

143
C/p059.c Normal file
View File

@ -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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
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;
}

BIN
C/p060 Executable file

Binary file not shown.

110
C/p060.c Normal file
View File

@ -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 <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#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);
}