Add comments

Added comments to the code of problems 36, 37, 38, 39 and 40 in C.
This commit is contained in:
daniele 2019-09-25 21:41:58 +02:00
parent 325dcc3507
commit 80719a58f8
Signed by: fuxino
GPG Key ID: 6FE25B4A3EE16FDA
5 changed files with 78 additions and 3 deletions

View File

@ -1,8 +1,16 @@
/* The decimal number, 585 = 1001001001_2 (binary), is palindromic in both bases.
*
* Find the sum of all numbers, less than one million, which are palindromic in base 10 and base 2.
*
* (Please note that the palindromic number, in either base, may not include leading zeros.)*/
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <time.h> #include <time.h>
#include "projecteuler.h" #include "projecteuler.h"
#define N 1000000
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
int i, sum = 0; int i, sum = 0;
@ -11,7 +19,10 @@ int main(int argc, char **argv)
clock_gettime(CLOCK_MONOTONIC, &start); clock_gettime(CLOCK_MONOTONIC, &start);
for(i = 1; i < 1000000; i += 2) /* Brute force approach. For every number below 1 million,
* check if they're palindrome in base 2 and 10 using the
* function implemented in projecteuler.c.*/
for(i = 1; i < N; i += 2)
{ {
if(is_palindrome(i, 10) && is_palindrome(i, 2)) if(is_palindrome(i, 10) && is_palindrome(i, 2))
{ {

View File

@ -1,3 +1,10 @@
/* The number 3797 has an interesting property. Being prime itself, it is possible to continuously remove digits from left to right,
* and remain prime at each stage: 3797, 797, 97, and 7. Similarly we can work from right to left: 3797, 379, 37, and 3.
*
* Find the sum of the only eleven primes that are both truncatable from left to right and right to left.
*
* NOTE: 2, 3, 5, and 7 are not considered to be truncatable primes.*/
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <math.h> #include <math.h>
@ -14,6 +21,7 @@ int main(int argc, char **argv)
clock_gettime(CLOCK_MONOTONIC, &start); clock_gettime(CLOCK_MONOTONIC, &start);
/* Check every number until 11 truncatable primes are found.*/
while(i < 11) while(i < 11)
{ {
if(is_tr_prime(n)) if(is_tr_prime(n))
@ -40,11 +48,15 @@ int is_tr_prime(int n)
{ {
int i, tmp; int i, tmp;
/* One-digit numbers and non-prime numbers are
* not truncatable primes.*/
if(n < 11 || !is_prime(n)) if(n < 11 || !is_prime(n))
{ {
return 0; return 0;
} }
/* Remove one digit at a time from the right and check
* if the resulting number is prime. Return 0 if it isn't.*/
tmp = n / 10; tmp = n / 10;
while(tmp > 0) while(tmp > 0)
@ -56,6 +68,9 @@ int is_tr_prime(int n)
tmp /= 10; tmp /= 10;
} }
/* Starting from the last digit, check if it's prime, then
* add back one digit at a time on the left and check if it
* is prime. Return 0 when it isn't.*/
i = 10; i = 10;
tmp = n % i; tmp = n % i;
@ -69,5 +84,6 @@ int is_tr_prime(int n)
tmp = n % i; tmp = n % i;
} }
/* If it gets here, the number is truncatable prime.*/
return 1; return 1;
} }

View File

@ -1,3 +1,16 @@
/* Take the number 192 and multiply it by each of 1, 2, and 3:
*
* 192 × 1 = 192
* 192 × 2 = 384
* 192 × 3 = 576
*
* By concatenating each product we get the 1 to 9 pandigital, 192384576. We will call 192384576 the concatenated product of 192 and (1,2,3)
*
* The same can be achieved by starting with 9 and multiplying by 1, 2, 3, 4, and 5, giving the pandigital, 918273645, which is the concatenated
* product of 9 and (1,2,3,4,5).
What is the largest 1 to 9 pandigital 9-digit number that can be formed as the concatenated product of an integer with (1,2, ... , n) where n > 1?*/
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <time.h> #include <time.h>
@ -12,6 +25,12 @@ int main(int argc, char **argv)
clock_gettime(CLOCK_MONOTONIC, &start); clock_gettime(CLOCK_MONOTONIC, &start);
/* A brute force approach is used, starting with 1 and multiplying
* the number by 1, 2 etc., concatenating the results, checking if
* it's 1 to 9 pandigital, and going to the next number when the
* concatenated result is greater than the greatest 9 digit pandigital
* value. The limit is set to 10000, since 1*10000=10000, 2*10000=20000 and
* concatenating this two numbers a 10-digit number is obtained.*/
for(i = 1; i < 10000; i++) for(i = 1; i < 10000; i++)
{ {
n = 0; n = 0;
@ -60,4 +79,3 @@ int main(int argc, char **argv)
return 0; return 0;
} }

View File

@ -1,3 +1,9 @@
/* If p is the perimeter of a right angle triangle with integral length sides, {a,b,c}, there are exactly three solutions for p = 120.
*
* {20,48,52}, {24,45,51}, {30,40,50}
*
* For which value of p 1000, is the number of solutions maximised?*/
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <time.h> #include <time.h>
@ -11,6 +17,7 @@ int main(int argc, char **argv)
clock_gettime(CLOCK_MONOTONIC, &start); clock_gettime(CLOCK_MONOTONIC, &start);
/* Start with p=12 (the smallest pythagorean triplet is (3,4,5) and 3+4+5=12.*/
for(p = 12; p <= 1000; p++) for(p = 12; p <= 1000; p++)
{ {
count = 0; count = 0;
@ -18,6 +25,7 @@ int main(int argc, char **argv)
b = 0; b = 0;
c = 0; c = 0;
/* Generate pythagorean triplets.*/
for(m = 2; m * m < p; m++) for(m = 2; m * m < p; m++)
{ {
for(n = 1; n < m; n++) for(n = 1; n < m; n++)
@ -26,6 +34,9 @@ int main(int argc, char **argv)
b = 2 * m * n; b = 2 * m * n;
c = m * m + n * n; c = m * m + n * n;
/* Increase counter if a+b+c=p and the triplet is new,
* then save the value of c to avoid counting the same
* triplet more than once.*/
if(a + b + c == p && !savedc[c]) if(a + b + c == p && !savedc[c])
{ {
savedc[c] = 1; savedc[c] = 1;
@ -37,12 +48,15 @@ int main(int argc, char **argv)
tmpb = b; tmpb = b;
tmpc = c; tmpc = c;
/* Check all the triplets obtained multiplying a, b and c
* for integer numbers, until the perimeters exceeds p.*/
while(tmpa + tmpb + tmpc < p) while(tmpa + tmpb + tmpc < p)
{ {
tmpa = a * i; tmpa = a * i;
tmpb = b * i; tmpb = b * i;
tmpc = c * i; tmpc = c * i;
/* Increase counter if the new a, b and c give a perimeter=p.*/
if(tmpa + tmpb + tmpc == p && !savedc[tmpc]) if(tmpa + tmpb + tmpc == p && !savedc[tmpc])
{ {
savedc[tmpc] = 1; savedc[tmpc] = 1;
@ -54,6 +68,8 @@ int main(int argc, char **argv)
} }
} }
/* If the current value is greater than the maximum,
* save the new maximum and the value of p.*/
if(count > max) if(count > max)
{ {
max = count; max = count;

View File

@ -1,3 +1,13 @@
/* An irrational decimal fraction is created by concatenating the positive integers:
*
* 0.123456789101112131415161718192021...
*
* It can be seen that the 12th digit of the fractional part is 1.
*
* If d_n represents the nth digit of the fractional part, find the value of the following expression.
*
* d_1 × d_10 × d_100 × d_1000 × d_10000 × d_100000 × d_1000000*/
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <time.h> #include <time.h>
@ -14,6 +24,9 @@ int main(int argc, char **argv)
i = 1; i = 1;
value = 1; value = 1;
/* Loop on all numbers and put the digits in the right place
* in an array. Use modulo and division to get the digits
* for numbers with more than one digit.*/
while(i <= 1000000) while(i <= 1000000)
{ {
if(value < 10) if(value < 10)
@ -64,6 +77,7 @@ int main(int argc, char **argv)
value++; value++;
} }
/* Calculate the product.*/
n = digits[0] * digits[9] * digits[99] * digits[999] * digits[9999] * digits[99999] * digits[999999]; n = digits[0] * digits[9] * digits[99] * digits[999] * digits[9999] * digits[99999] * digits[999999];
clock_gettime(CLOCK_MONOTONIC, &end); clock_gettime(CLOCK_MONOTONIC, &end);