Add comments
Added comments to the code of problems 36, 37, 38, 39 and 40 in C.
This commit is contained in:
parent
325dcc3507
commit
80719a58f8
13
C/p036.c
13
C/p036.c
@ -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 <stdlib.h>
|
||||
#include <time.h>
|
||||
#include "projecteuler.h"
|
||||
|
||||
#define N 1000000
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int i, sum = 0;
|
||||
@ -11,7 +19,10 @@ int main(int argc, char **argv)
|
||||
|
||||
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))
|
||||
{
|
||||
|
16
C/p037.c
16
C/p037.c
@ -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 <stdlib.h>
|
||||
#include <math.h>
|
||||
@ -14,6 +21,7 @@ int main(int argc, char **argv)
|
||||
|
||||
clock_gettime(CLOCK_MONOTONIC, &start);
|
||||
|
||||
/* Check every number until 11 truncatable primes are found.*/
|
||||
while(i < 11)
|
||||
{
|
||||
if(is_tr_prime(n))
|
||||
@ -40,11 +48,15 @@ int is_tr_prime(int n)
|
||||
{
|
||||
int i, tmp;
|
||||
|
||||
/* One-digit numbers and non-prime numbers are
|
||||
* not truncatable primes.*/
|
||||
if(n < 11 || !is_prime(n))
|
||||
{
|
||||
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;
|
||||
|
||||
while(tmp > 0)
|
||||
@ -56,6 +68,9 @@ int is_tr_prime(int n)
|
||||
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;
|
||||
tmp = n % i;
|
||||
|
||||
@ -69,5 +84,6 @@ int is_tr_prime(int n)
|
||||
tmp = n % i;
|
||||
}
|
||||
|
||||
/* If it gets here, the number is truncatable prime.*/
|
||||
return 1;
|
||||
}
|
||||
|
20
C/p038.c
20
C/p038.c
@ -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 <stdlib.h>
|
||||
#include <time.h>
|
||||
@ -12,6 +25,12 @@ int main(int argc, char **argv)
|
||||
|
||||
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++)
|
||||
{
|
||||
n = 0;
|
||||
@ -60,4 +79,3 @@ int main(int argc, char **argv)
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
16
C/p039.c
16
C/p039.c
@ -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 <stdlib.h>
|
||||
#include <time.h>
|
||||
@ -11,6 +17,7 @@ int main(int argc, char **argv)
|
||||
|
||||
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++)
|
||||
{
|
||||
count = 0;
|
||||
@ -18,6 +25,7 @@ int main(int argc, char **argv)
|
||||
b = 0;
|
||||
c = 0;
|
||||
|
||||
/* Generate pythagorean triplets.*/
|
||||
for(m = 2; m * m < p; m++)
|
||||
{
|
||||
for(n = 1; n < m; n++)
|
||||
@ -26,6 +34,9 @@ int main(int argc, char **argv)
|
||||
b = 2 * m * 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])
|
||||
{
|
||||
savedc[c] = 1;
|
||||
@ -37,12 +48,15 @@ int main(int argc, char **argv)
|
||||
tmpb = b;
|
||||
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)
|
||||
{
|
||||
tmpa = a * i;
|
||||
tmpb = b * i;
|
||||
tmpc = c * i;
|
||||
|
||||
/* Increase counter if the new a, b and c give a perimeter=p.*/
|
||||
if(tmpa + tmpb + tmpc == p && !savedc[tmpc])
|
||||
{
|
||||
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)
|
||||
{
|
||||
max = count;
|
||||
|
16
C/p040.c
16
C/p040.c
@ -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 <stdlib.h>
|
||||
#include <time.h>
|
||||
@ -8,12 +18,15 @@ int main(int argc, char **argv)
|
||||
int digits[1000005];
|
||||
double elapsed;
|
||||
struct timespec start, end;
|
||||
|
||||
|
||||
clock_gettime(CLOCK_MONOTONIC, &start);
|
||||
|
||||
i = 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)
|
||||
{
|
||||
if(value < 10)
|
||||
@ -64,6 +77,7 @@ int main(int argc, char **argv)
|
||||
value++;
|
||||
}
|
||||
|
||||
/* Calculate the product.*/
|
||||
n = digits[0] * digits[9] * digits[99] * digits[999] * digits[9999] * digits[99999] * digits[999999];
|
||||
|
||||
clock_gettime(CLOCK_MONOTONIC, &end);
|
||||
|
Loading…
x
Reference in New Issue
Block a user