Fix C code with astyle

This commit is contained in:
daniele 2024-12-15 11:17:25 +01:00
parent 81ee2c7f5a
commit 7c72d0d5f1
Signed by: fuxino
GPG Key ID: 981A2B2A3BBF5514
101 changed files with 6456 additions and 6254 deletions

View File

@ -2,36 +2,38 @@
*
* Find the sum of all the multiples of 3 or 5 below 1000.*/
#define _POSIX_C_SOURCE 199309L
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, char **argv)
{
int i, sum = 0;
double elapsed;
struct timespec start, end;
int i, sum = 0;
double elapsed;
struct timespec start, end;
clock_gettime(CLOCK_MONOTONIC, &start);
clock_gettime(CLOCK_MONOTONIC, &start);
/* Simple brute-force approach: try every number between 3 and 999,
* check if it's a multiple of 3 or 5, if yes add it to the total.*/
for(i = 3; i < 1000; i++)
{
if (i % 3 == 0 || i % 5 == 0)
{
sum += i;
}
}
/* Simple brute-force approach: try every number between 3 and 999,
* check if it's a multiple of 3 or 5, if yes add it to the total.*/
for(i = 3; i < 1000; i++)
{
if (i % 3 == 0 || i % 5 == 0)
{
sum += i;
}
}
clock_gettime(CLOCK_MONOTONIC, &end);
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 1\n");
printf("Answer: %d\n", sum);
printf("Project Euler, Problem 1\n");
printf("Answer: %d\n", sum);
printf("Elapsed time: %.9lf seconds\n", elapsed);
printf("Elapsed time: %.9lf seconds\n", elapsed);
return 0;
return 0;
}

View File

@ -4,6 +4,8 @@
*
* By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.*/
#define _POSIX_C_SOURCE 199309L
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
@ -12,36 +14,36 @@
int main(int argc, char **argv)
{
int fib0 = 1, fib1 = 2, fib2, sum = 2;
double elapsed;
struct timespec start, end;
int fib0 = 1, fib1 = 2, fib2, sum = 2;
double elapsed;
struct timespec start, end;
clock_gettime(CLOCK_MONOTONIC, &start);
clock_gettime(CLOCK_MONOTONIC, &start);
fib2 = fib0 + fib1;
fib2 = fib0 + fib1;
/* Simple brute-force approach: generate every value in the Fibonacci
* sequence smaller than 4 million and if it's even add it to the total.*/
while(fib2 <= N)
{
if(fib2 % 2 == 0)
{
sum += fib2;
}
/* Simple brute-force approach: generate every value in the Fibonacci
* sequence smaller than 4 million and if it's even add it to the total.*/
while(fib2 <= N)
{
if(fib2 % 2 == 0)
{
sum += fib2;
}
fib0 = fib1;
fib1 = fib2;
fib2 = fib0 + fib1;
}
fib0 = fib1;
fib1 = fib2;
fib2 = fib0 + fib1;
}
clock_gettime(CLOCK_MONOTONIC, &end);
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 2\n");
printf("Answer: %d\n", sum);
printf("Project Euler, Problem 2\n");
printf("Answer: %d\n", sum);
printf("Elapsed time: %.9lf seconds\n", elapsed);
printf("Elapsed time: %.9lf seconds\n", elapsed);
return 0;
return 0;
}

View File

@ -2,6 +2,8 @@
*
* What is the largest prime factor of the number 600851475143?*/
#define _POSIX_C_SOURCE 199309L
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
@ -11,26 +13,26 @@ long int max_prime_factor(long int num);
int main(int argc, char **argv)
{
long int res;
long int num = 600851475143;
double elapsed;
struct timespec start, end;
long int res;
long int num = 600851475143;
double elapsed;
struct timespec start, end;
clock_gettime(CLOCK_MONOTONIC, &start);
clock_gettime(CLOCK_MONOTONIC, &start);
/* Use a function to calculate the largest prime factor.*/
res = max_prime_factor(num);
/* Use a function to calculate the largest prime factor.*/
res = max_prime_factor(num);
clock_gettime(CLOCK_MONOTONIC, &end);
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 3\n");
printf("Answer: %ld\n", res);
printf("Project Euler, Problem 3\n");
printf("Answer: %ld\n", res);
printf("Elapsed time: %.9lf seconds\n", elapsed);
printf("Elapsed time: %.9lf seconds\n", elapsed);
return 0;
return 0;
}
/* Recursive approach: if num is prime, return num, otherwise
@ -38,39 +40,39 @@ int main(int argc, char **argv)
* by its prime factors until only the largest remains.*/
long int max_prime_factor(long int num)
{
long int i;
long int i;
/* Use function defined in projecteuler.c to check if a number is prime.*/
if(is_prime(num))
{
return num;
}
/* Use function defined in projecteuler.c to check if a number is prime.*/
if(is_prime(num))
{
return num;
}
/* If num is even, find the largest prime factor of num/2.*/
if(num % 2 == 0)
{
return max_prime_factor(num/2);
}
else
{
i = 3;
while(1)
{
/* If num is divisible by i and i is prime, find largest prime
* factor of num/i.*/
if(num % i == 0)
{
if(is_prime(i))
/* If num is even, find the largest prime factor of num/2.*/
if(num % 2 == 0)
{
return max_prime_factor(num/2);
}
else
{
i = 3;
while(1)
{
/* If num is divisible by i and i is prime, find largest prime
* factor of num/i.*/
if(num % i == 0)
{
return max_prime_factor(num/i);
if(is_prime(i))
{
return max_prime_factor(num/i);
}
}
}
i += 2;
}
}
i += 2;
}
}
/* Should never get here.*/
return -1;
/* Should never get here.*/
return -1;
}

View File

@ -2,6 +2,8 @@
*
* Find the largest palindrome made from the product of two 3-digit numbers.*/
#define _POSIX_C_SOURCE 199309L
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
@ -9,36 +11,36 @@
int main(int argc, char **argv)
{
int i, j, max = 0, num;
double elapsed;
struct timespec start, end;
int i, j, max = 0, num;
double elapsed;
struct timespec start, end;
clock_gettime(CLOCK_MONOTONIC, &start);
clock_gettime(CLOCK_MONOTONIC, &start);
/* Using a brute-force approach: generate every product of 3-digit numbers
* and check if it's palindrome. If the product found is greater than the
* current maximum, save the current product.*/
for(i = 999; i >= 100; i--)
{
for(j = i; j >= 100; j--)
{
num = i * j;
/* Use the function defined in projecteuler.c to check if a number is palindrome.*/
if(num > max && is_palindrome(num, 10))
{
max = num;
}
}
}
/* Using a brute-force approach: generate every product of 3-digit numbers
* and check if it's palindrome. If the product found is greater than the
* current maximum, save the current product.*/
for(i = 999; i >= 100; i--)
{
for(j = i; j >= 100; j--)
{
num = i * j;
/* Use the function defined in projecteuler.c to check if a number is palindrome.*/
if(num > max && is_palindrome(num, 10))
{
max = num;
}
}
}
clock_gettime(CLOCK_MONOTONIC, &end);
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 4\n");
printf("Answer: %d\n", max);
printf("Project Euler, Problem 4\n");
printf("Answer: %d\n", max);
printf("Elapsed time: %.9lf seconds\n", elapsed);
printf("Elapsed time: %.9lf seconds\n", elapsed);
return 0;
return 0;
}

View File

@ -2,6 +2,8 @@
*
* What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?*/
#define _POSIX_C_SOURCE 199309L
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
@ -9,23 +11,23 @@
int main(int argc, char **argv)
{
long int res, n[20] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20};
double elapsed;
struct timespec start, end;
long int res, n[20] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20};
double elapsed;
struct timespec start, end;
clock_gettime(CLOCK_MONOTONIC, &start);
clock_gettime(CLOCK_MONOTONIC, &start);
/* Function define in projecteuler.c to find the least common multiple of multiple numbers.*/
res = lcmm(n, 20);
/* Function define in projecteuler.c to find the least common multiple of multiple numbers.*/
res = lcmm(n, 20);
clock_gettime(CLOCK_MONOTONIC, &end);
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 5\n");
printf("Answer: %ld\n", res);
printf("Project Euler, Problem 5\n");
printf("Answer: %ld\n", res);
printf("Elapsed time: %.9lf seconds\n", elapsed);
printf("Elapsed time: %.9lf seconds\n", elapsed);
return 0;
return 0;
}

View File

@ -1,44 +1,46 @@
/* The sum of the squares of the first ten natural numbers is,
*
* 1^2 + 2^2 + ... + 10^2 = 385
*
*
* The square of the sum of the first ten natural numbers is,
*
* (1 + 2 + ... + 10)^2 = 55^2 = 3025
*
*
* Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 385 = 2640.
*
* Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.*/
#define _POSIX_C_SOURCE 199309L
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, char **argv)
{
int i, sum_squares = 0, square_sum = 0;
double elapsed;
struct timespec start, end;
int i, sum_squares = 0, square_sum = 0;
double elapsed;
struct timespec start, end;
clock_gettime(CLOCK_MONOTONIC, &start);
clock_gettime(CLOCK_MONOTONIC, &start);
/* Straightforward brute-force approach.*/
for(i = 1; i <= 100; i++)
{
sum_squares += i*i;
square_sum += i;
}
/* Straightforward brute-force approach.*/
for(i = 1; i <= 100; i++)
{
sum_squares += i*i;
square_sum += i;
}
square_sum *= square_sum;
square_sum *= square_sum;
clock_gettime(CLOCK_MONOTONIC, &end);
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 6\n");
printf("Answer: %d\n", square_sum-sum_squares);
printf("Project Euler, Problem 6\n");
printf("Answer: %d\n", square_sum-sum_squares);
printf("Elapsed time: %.9lf seconds\n", elapsed);
printf("Elapsed time: %.9lf seconds\n", elapsed);
return 0;
return 0;
}

View File

@ -2,6 +2,8 @@
*
* What is the 10 001st prime number?*/
#define _POSIX_C_SOURCE 199309L
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
@ -9,33 +11,33 @@
int main(int argc, char **argv)
{
int count = 1, n = 1, target = 10001;
double elapsed;
struct timespec start, end;
int count = 1, n = 1, target = 10001;
double elapsed;
struct timespec start, end;
clock_gettime(CLOCK_MONOTONIC, &start);
clock_gettime(CLOCK_MONOTONIC, &start);
/* Brute force approach: start with count=1 and check every odd number
* (2 is the only even prime), if it's prime increment count, until the
* target prime is reached.*/
while(count != target)
{
n += 2;
/* Use the function in projecteuler.c to check if a number is prime.*/
if(is_prime(n))
{
count++;
}
}
/* Brute force approach: start with count=1 and check every odd number
* (2 is the only even prime), if it's prime increment count, until the
* target prime is reached.*/
while(count != target)
{
n += 2;
/* Use the function in projecteuler.c to check if a number is prime.*/
if(is_prime(n))
{
count++;
}
}
clock_gettime(CLOCK_MONOTONIC, &end);
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 7\n");
printf("Answer: %d\n", n);
printf("Project Euler, Problem 7\n");
printf("Answer: %d\n", n);
printf("Elapsed time: %.9lf seconds\n", elapsed);
printf("Elapsed time: %.9lf seconds\n", elapsed);
return 0;
return 0;
}

144
C/p008.c
View File

@ -23,90 +23,92 @@
*
* Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?*/
#define _POSIX_C_SOURCE 199309L
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, char **argv)
{
char string[] = "73167176531330624919225119674426574742355349"
"19493496983520312774506326239578318016984801"
"86947885184385861560789112949495459501737958"
"33195285320880551112540698747158523863050715"
"69329096329522744304355766896648950445244523"
"16173185640309871112172238311362229893423380"
"30813533627661428280644448664523874930358907"
"29629049156044077239071381051585930796086670"
"17242712188399879790879227492190169972088809"
"37766572733300105336788122023542180975125454"
"05947522435258490771167055601360483958644670"
"63244157221553975369781797784617406495514929"
"08625693219784686224828397224137565705605749"
"02614079729686524145351004748216637048440319"
"98900088952434506585412275886668811642717147"
"99244429282308634656748139191231628245861786"
"64583591245665294765456828489128831426076900"
"42242190226710556263211111093705442175069416"
"58960408071984038509624554443629812309878799"
"27244284909188845801561660979191338754992005"
"24063689912560717606058861164671094050775410"
"02256983155200055935729725716362695618826704"
"28252483600823257530420752963450";
char cur, out;
long int max = 0, tmp = 1;
int i, j;
double elapsed;
struct timespec start, end;
char string[] = "73167176531330624919225119674426574742355349"
"19493496983520312774506326239578318016984801"
"86947885184385861560789112949495459501737958"
"33195285320880551112540698747158523863050715"
"69329096329522744304355766896648950445244523"
"16173185640309871112172238311362229893423380"
"30813533627661428280644448664523874930358907"
"29629049156044077239071381051585930796086670"
"17242712188399879790879227492190169972088809"
"37766572733300105336788122023542180975125454"
"05947522435258490771167055601360483958644670"
"63244157221553975369781797784617406495514929"
"08625693219784686224828397224137565705605749"
"02614079729686524145351004748216637048440319"
"98900088952434506585412275886668811642717147"
"99244429282308634656748139191231628245861786"
"64583591245665294765456828489128831426076900"
"42242190226710556263211111093705442175069416"
"58960408071984038509624554443629812309878799"
"27244284909188845801561660979191338754992005"
"24063689912560717606058861164671094050775410"
"02256983155200055935729725716362695618826704"
"28252483600823257530420752963450";
char cur, out;
long int max = 0, tmp = 1;
int i, j;
double elapsed;
struct timespec start, end;
clock_gettime(CLOCK_MONOTONIC, &start);
clock_gettime(CLOCK_MONOTONIC, &start);
for(i = 0; i < 1000; i++)
{
/* For the first 13 digits, just multiply them.*/
if(i < 13)
{
cur = string[i] - '0';
tmp *= (long int)cur;
}
else
{
/* If the current product is greater than the maximum, save the current as maximum.*/
if(tmp > max)
{
max = tmp;
}
/* Check the value of the first digit of the previous sequence, which will not be part
* of the next sequence.*/
out = string[i-13] - '0';
/* If the digit is zero, multiply all the 13 digits of the new sequence.*/
if(out == 0)
{
tmp = 1;
for(j = i - 12; j <= i; j++)
{
cur = string[j] - '0';
tmp *= (long int)cur;
}
}
/* If the digit not zero, instead of multiplying all the 13 digits of the new sequence,
* divide the current product by the remove digit and multiply it by the new digit.*/
else
{
for(i = 0; i < 1000; i++)
{
/* For the first 13 digits, just multiply them.*/
if(i < 13)
{
cur = string[i] - '0';
tmp /= (long int)out;
tmp *= (long int)cur;
}
}
}
}
else
{
/* If the current product is greater than the maximum, save the current as maximum.*/
if(tmp > max)
{
max = tmp;
}
/* Check the value of the first digit of the previous sequence, which will not be part
* of the next sequence.*/
out = string[i-13] - '0';
/* If the digit is zero, multiply all the 13 digits of the new sequence.*/
if(out == 0)
{
tmp = 1;
for(j = i - 12; j <= i; j++)
{
cur = string[j] - '0';
tmp *= (long int)cur;
}
}
/* If the digit not zero, instead of multiplying all the 13 digits of the new sequence,
* divide the current product by the remove digit and multiply it by the new digit.*/
else
{
cur = string[i] - '0';
tmp /= (long int)out;
tmp *= (long int)cur;
}
}
}
clock_gettime(CLOCK_MONOTONIC, &end);
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 8\n");
printf("Answer: %ld\n", max);
printf("Project Euler, Problem 8\n");
printf("Answer: %ld\n", max);
printf("Elapsed time: %.9lf seconds\n", elapsed);
printf("Elapsed time: %.9lf seconds\n", elapsed);
return 0;
return 0;
}

View File

@ -8,6 +8,8 @@
*
* Find the product abc.*/
#define _POSIX_C_SOURCE 199309L
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
@ -15,61 +17,61 @@
int main(int argc, char **argv)
{
int a, b, c, tmpa, tmpb, tmpc, i, m, n, found = 0;
double elapsed;
struct timespec start, end;
int a, b, c, tmpa, tmpb, tmpc, i, m, n, found = 0;
double elapsed;
struct timespec start, end;
clock_gettime(CLOCK_MONOTONIC, &start);
clock_gettime(CLOCK_MONOTONIC, &start);
/* Brute force approach: generate all the Pythagorean triplets using
* Euclid's formula, until the one where a+b+c=1000 is found.*/
for(m = 2; found == 0; m++)
{
for(n = 1; n < m && !found; n++)
{
if(gcd(m, n) == 1 && ((m % 2 == 0 && n % 2 != 0) || (m % 2 != 0 && n % 2 == 0)))
{
a = m * m - n * n;
b = 2 * m * n;
c = m * m + n * n;
if(a + b + c == 1000)
/* Brute force approach: generate all the Pythagorean triplets using
* Euclid's formula, until the one where a+b+c=1000 is found.*/
for(m = 2; found == 0; m++)
{
for(n = 1; n < m && !found; n++)
{
if(gcd(m, n) == 1 && ((m % 2 == 0 && n % 2 != 0) || (m % 2 != 0 && n % 2 == 0)))
{
found = 1;
break;
a = m * m - n * n;
b = 2 * m * n;
c = m * m + n * n;
if(a + b + c == 1000)
{
found = 1;
break;
}
i = 2;
do
{
tmpa = a * i;
tmpb = b * i;
tmpc = c * i;
if(tmpa + tmpb + tmpc == 1000)
{
a = tmpa;
b = tmpb;
c = tmpc;
found = 1;
break;
}
i++;
} while(tmpa + tmpb + tmpc < 1000);
}
}
}
i = 2;
clock_gettime(CLOCK_MONOTONIC, &end);
do
{
tmpa = a * i;
tmpb = b * i;
tmpc = c * i;
elapsed = (end.tv_sec - start.tv_sec) + (double)(end.tv_nsec - start.tv_nsec) / 1000000000;
if(tmpa + tmpb + tmpc == 1000)
{
a = tmpa;
b = tmpb;
c = tmpc;
found = 1;
break;
}
i++;
}while(tmpa + tmpb + tmpc < 1000);
}
}
}
printf("Project Euler, Problem 9\n");
printf("Answer: %d\n", a*b*c);
clock_gettime(CLOCK_MONOTONIC, &end);
printf("Elapsed time: %.9lf seconds\n", elapsed);
elapsed = (end.tv_sec - start.tv_sec) + (double)(end.tv_nsec - start.tv_nsec) / 1000000000;
printf("Project Euler, Problem 9\n");
printf("Answer: %d\n", a*b*c);
printf("Elapsed time: %.9lf seconds\n", elapsed);
return 0;
return 0;
}

View File

@ -2,6 +2,8 @@
*
* Find the sum of all the primes below two million.*/
#define _POSIX_C_SOURCE 199309L
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
@ -11,41 +13,41 @@
int main(int argc, char **argv)
{
int i;
int *primes;
long int sum = 0;
double elapsed;
struct timespec start, end;
int i;
int *primes;
long int sum = 0;
double elapsed;
struct timespec start, end;
clock_gettime(CLOCK_MONOTONIC, &start);
clock_gettime(CLOCK_MONOTONIC, &start);
/* Use the function in projecteuler.c implementing the
* Sieve of Eratosthenes algorithm to generate primes.*/
if((primes = sieve(N)) == NULL)
{
fprintf(stderr, "Error! Sieve function returned NULL\n");
return 1;
}
/* Use the function in projecteuler.c implementing the
* Sieve of Eratosthenes algorithm to generate primes.*/
if((primes = sieve(N)) == NULL)
{
fprintf(stderr, "Error! Sieve function returned NULL\n");
return 1;
}
/* Sum all the primes.*/
for(i = 0; i < N; i++)
{
if(primes[i])
{
sum += i;
}
}
/* Sum all the primes.*/
for(i = 0; i < N; i++)
{
if(primes[i])
{
sum += i;
}
}
free(primes);
free(primes);
clock_gettime(CLOCK_MONOTONIC, &end);
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 10\n");
printf("Answer: %ld\n", sum);
printf("Project Euler, Problem 10\n");
printf("Answer: %ld\n", sum);
printf("Elapsed time: %.9lf seconds\n", elapsed);
printf("Elapsed time: %.9lf seconds\n", elapsed);
return 0;
return 0;
}

179
C/p011.c
View File

@ -25,110 +25,113 @@
*
* What is the greatest product of four adjacent numbers in the same direction (up, down, left, right, or diagonally) in the 20×20 grid?*/
#define _POSIX_C_SOURCE 199309L
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, char **argv)
{
int grid[][20] = {{8, 2, 22, 97, 38, 15, 0, 40, 0, 75, 4, 5, 7, 78, 52, 12, 50, 77, 91, 8},
{49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 4, 56, 62, 0},
{81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30, 3, 49, 13, 36, 65},
{52, 70, 95, 23, 4, 60, 11, 42, 69, 24, 68, 56, 1, 32, 56, 71, 37, 2, 36, 91},
{22, 31, 16, 71, 51, 67, 63, 89, 41, 92, 36, 54, 22, 40, 40, 28, 66, 33, 13, 80},
{24, 47, 32, 60, 99, 3, 45, 2, 44, 75, 33, 53, 78, 36, 84, 20, 35, 17, 12, 50},
{32, 98, 81, 28, 64, 23, 67, 10, 26, 38, 40, 67, 59, 54, 70, 66, 18, 38, 64, 70},
{67, 26, 20, 68, 2, 62, 12, 20, 95, 63, 94, 39, 63, 8, 40, 91, 66, 49, 94, 21},
{24, 55, 58, 5, 66, 73, 99, 26, 97, 17, 78, 78, 96, 83, 14, 88, 34, 89, 63, 72},
{21, 36, 23, 9, 75, 0, 76, 44, 20, 45, 35, 14, 0, 61, 33, 97, 34, 31, 33, 95},
{78, 17, 53, 28, 22, 75, 31, 67, 15, 94, 3, 80, 4, 62, 16, 14, 9, 53, 56, 92},
{16, 39, 5, 42, 96, 35, 31, 47, 55, 58, 88, 24, 0, 17, 54, 24, 36, 29, 85, 57},
{86, 56, 0, 48, 35, 71, 89, 7, 5, 44, 44, 37, 44, 60, 21, 58, 51, 54, 17, 58},
{19, 80, 81, 68, 5, 94, 47, 69, 28, 73, 92, 13, 86, 52, 17, 77, 4, 89, 55, 40},
{4, 52, 8, 83, 97, 35, 99, 16, 7, 97, 57, 32, 16, 26, 26, 79, 33, 27, 98, 66},
{88, 36, 68, 87, 57, 62, 20, 72, 3, 46, 33, 67, 46, 55, 12, 32, 63, 93, 53, 69},
{4, 42, 16, 73, 38, 25, 39, 11, 24, 94, 72, 18, 8, 46, 29, 32, 40, 62, 76, 36},
{20, 69, 36, 41, 72, 30, 23, 88, 34, 62, 99, 69, 82, 67, 59, 85, 74, 4, 36, 16},
{20, 73, 35, 29, 78, 31, 90, 1, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 5, 54},
{1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48}};
int i, j, k, w, max = 0, prod;
double elapsed;
struct timespec start, end;
int grid[][20] = {{8, 2, 22, 97, 38, 15, 0, 40, 0, 75, 4, 5, 7, 78, 52, 12, 50, 77, 91, 8},
{49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 4, 56, 62, 0},
{81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30, 3, 49, 13, 36, 65},
{52, 70, 95, 23, 4, 60, 11, 42, 69, 24, 68, 56, 1, 32, 56, 71, 37, 2, 36, 91},
{22, 31, 16, 71, 51, 67, 63, 89, 41, 92, 36, 54, 22, 40, 40, 28, 66, 33, 13, 80},
{24, 47, 32, 60, 99, 3, 45, 2, 44, 75, 33, 53, 78, 36, 84, 20, 35, 17, 12, 50},
{32, 98, 81, 28, 64, 23, 67, 10, 26, 38, 40, 67, 59, 54, 70, 66, 18, 38, 64, 70},
{67, 26, 20, 68, 2, 62, 12, 20, 95, 63, 94, 39, 63, 8, 40, 91, 66, 49, 94, 21},
{24, 55, 58, 5, 66, 73, 99, 26, 97, 17, 78, 78, 96, 83, 14, 88, 34, 89, 63, 72},
{21, 36, 23, 9, 75, 0, 76, 44, 20, 45, 35, 14, 0, 61, 33, 97, 34, 31, 33, 95},
{78, 17, 53, 28, 22, 75, 31, 67, 15, 94, 3, 80, 4, 62, 16, 14, 9, 53, 56, 92},
{16, 39, 5, 42, 96, 35, 31, 47, 55, 58, 88, 24, 0, 17, 54, 24, 36, 29, 85, 57},
{86, 56, 0, 48, 35, 71, 89, 7, 5, 44, 44, 37, 44, 60, 21, 58, 51, 54, 17, 58},
{19, 80, 81, 68, 5, 94, 47, 69, 28, 73, 92, 13, 86, 52, 17, 77, 4, 89, 55, 40},
{4, 52, 8, 83, 97, 35, 99, 16, 7, 97, 57, 32, 16, 26, 26, 79, 33, 27, 98, 66},
{88, 36, 68, 87, 57, 62, 20, 72, 3, 46, 33, 67, 46, 55, 12, 32, 63, 93, 53, 69},
{4, 42, 16, 73, 38, 25, 39, 11, 24, 94, 72, 18, 8, 46, 29, 32, 40, 62, 76, 36},
{20, 69, 36, 41, 72, 30, 23, 88, 34, 62, 99, 69, 82, 67, 59, 85, 74, 4, 36, 16},
{20, 73, 35, 29, 78, 31, 90, 1, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 5, 54},
{1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48}
};
int i, j, k, w, max = 0, prod;
double elapsed;
struct timespec start, end;
clock_gettime(CLOCK_MONOTONIC, &start);
clock_gettime(CLOCK_MONOTONIC, &start);
/* Brute-force approach: for each number in the grid, try products with its three
* adjacent numbers in every direction (horizontal, vertical and the two diagonals).
* If the product is larger than the current maximum, save it.*/
for(i = 0; i < 17; i++)
{
for(j = 0; j < 17; j++)
{
prod = 1;
/* Horizontal direction.*/
for(k = j; k < j + 4; k++)
{
prod *= grid[i][k];
}
/* Brute-force approach: for each number in the grid, try products with its three
* adjacent numbers in every direction (horizontal, vertical and the two diagonals).
* If the product is larger than the current maximum, save it.*/
for(i = 0; i < 17; i++)
{
for(j = 0; j < 17; j++)
{
prod = 1;
/* Horizontal direction.*/
for(k = j; k < j + 4; k++)
{
prod *= grid[i][k];
}
if(prod > max)
{
max = prod;
}
prod = 1;
/* Vertical direction.*/
for(k = i; k < i + 4; k++)
{
prod *= grid[k][j];
}
if(prod > max)
{
max = prod;
}
prod = 1;
/* Diagonal direction, from top left to bottom right.*/
for(k = i, w = j; k < i + 4 && w < j + 4; k++, w++)
{
prod *= grid[k][w];
}
if(k == i + 4 && w == j + 4)
{
if(prod > max)
{
max = prod;
max = prod;
}
}
}
}
/* The last diagonal is handled separately.*/
for(i = 0; i < 17; i++)
{
for(j = 3; j < 20; j++)
{
prod = 1;
/* Diagonal direction, from top right to bottom left.*/
for(k = i, w = j; k < i + 4 && w > j - 4; k++, w--)
{
prod *= grid[k][w];
}
if(prod > max)
{
max = prod;
}
}
}
prod = 1;
/* Vertical direction.*/
for(k = i; k < i + 4; k++)
{
prod *= grid[k][j];
}
if(prod > max)
{
max = prod;
}
clock_gettime(CLOCK_MONOTONIC, &end);
prod = 1;
/* Diagonal direction, from top left to bottom right.*/
for(k = i, w = j; k < i + 4 && w < j + 4; k++, w++)
{
prod *= grid[k][w];
}
if(k == i + 4 && w == j + 4)
{
if(prod > max)
{
max = prod;
}
}
}
}
elapsed = (end.tv_sec - start.tv_sec) + (double)(end.tv_nsec - start.tv_nsec) / 1000000000;
/* The last diagonal is handled separately.*/
for(i = 0; i < 17; i++)
{
for(j = 3; j < 20; j++)
{
prod = 1;
/* Diagonal direction, from top right to bottom left.*/
for(k = i, w = j; k < i + 4 && w > j - 4; k++, w--)
{
prod *= grid[k][w];
}
if(prod > max)
{
max = prod;
}
}
}
printf("Project Euler, Problem 11\n");
printf("Answer: %d\n", max);
clock_gettime(CLOCK_MONOTONIC, &end);
printf("Elapsed time: %.9lf seconds\n", elapsed);
elapsed = (end.tv_sec - start.tv_sec) + (double)(end.tv_nsec - start.tv_nsec) / 1000000000;
return 0;
printf("Project Euler, Problem 11\n");
printf("Answer: %d\n", max);
printf("Elapsed time: %.9lf seconds\n", elapsed);
return 0;
}

View File

@ -17,6 +17,8 @@
*
* What is the value of the first triangle number to have over five hundred divisors?*/
#define _POSIX_C_SOURCE 199309L
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
@ -24,34 +26,34 @@
int main(int argc, char **argv)
{
int i = 0, finished = 0, count, triang = 0;
double elapsed;
struct timespec start, end;
int i = 0, finished = 0, count, triang = 0;
double elapsed;
struct timespec start, end;
clock_gettime(CLOCK_MONOTONIC, &start);
clock_gettime(CLOCK_MONOTONIC, &start);
/* Generate all triangle numbers until the first one with more than 500 divisors is found.*/
while(!finished)
{
i++;
triang += i;
/* Use the function implemented in projecteuler.c to count divisors of a number.*/
count = count_divisors(triang);
if(count > 500)
{
finished = 1;
}
}
/* Generate all triangle numbers until the first one with more than 500 divisors is found.*/
while(!finished)
{
i++;
triang += i;
/* Use the function implemented in projecteuler.c to count divisors of a number.*/
count = count_divisors(triang);
clock_gettime(CLOCK_MONOTONIC, &end);
if(count > 500)
{
finished = 1;
}
}
elapsed = (end.tv_sec - start.tv_sec) + (double)(end.tv_nsec - start.tv_nsec) / 1000000000;
clock_gettime(CLOCK_MONOTONIC, &end);
printf("Project Euler, Problem 12\n");
printf("Answer: %d\n", triang);
elapsed = (end.tv_sec - start.tv_sec) + (double)(end.tv_nsec - start.tv_nsec) / 1000000000;
printf("Elapsed time: %.9lf seconds\n", elapsed);
printf("Project Euler, Problem 12\n");
printf("Answer: %d\n", triang);
return 0;
printf("Elapsed time: %.9lf seconds\n", elapsed);
return 0;
}

159
C/p013.c
View File

@ -101,6 +101,8 @@
* 20849603980134001723930671666823555245252804609722
* 53503534226472524250874054075591789781264330331690*/
#define _POSIX_C_SOURCE 199309L
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
@ -108,94 +110,95 @@
int main(int argc, char **argv)
{
char n[100][51] = {"37107287533902102798797998220837590246510135740250", "46376937677490009712648124896970078050417018260538",
"74324986199524741059474233309513058123726617309629", "91942213363574161572522430563301811072406154908250",
"23067588207539346171171980310421047513778063246676", "89261670696623633820136378418383684178734361726757",
"28112879812849979408065481931592621691275889832738", "44274228917432520321923589422876796487670272189318",
"47451445736001306439091167216856844588711603153276", "70386486105843025439939619828917593665686757934951",
"62176457141856560629502157223196586755079324193331", "64906352462741904929101432445813822663347944758178",
"92575867718337217661963751590579239728245598838407", "58203565325359399008402633568948830189458628227828",
"80181199384826282014278194139940567587151170094390", "35398664372827112653829987240784473053190104293586",
"86515506006295864861532075273371959191420517255829", "71693888707715466499115593487603532921714970056938",
"54370070576826684624621495650076471787294438377604", "53282654108756828443191190634694037855217779295145",
"36123272525000296071075082563815656710885258350721", "45876576172410976447339110607218265236877223636045",
"17423706905851860660448207621209813287860733969412", "81142660418086830619328460811191061556940512689692",
"51934325451728388641918047049293215058642563049483", "62467221648435076201727918039944693004732956340691",
"15732444386908125794514089057706229429197107928209", "55037687525678773091862540744969844508330393682126",
"18336384825330154686196124348767681297534375946515", "80386287592878490201521685554828717201219257766954",
"78182833757993103614740356856449095527097864797581", "16726320100436897842553539920931837441497806860984",
"48403098129077791799088218795327364475675590848030", "87086987551392711854517078544161852424320693150332",
"59959406895756536782107074926966537676326235447210", "69793950679652694742597709739166693763042633987085",
"41052684708299085211399427365734116182760315001271", "65378607361501080857009149939512557028198746004375",
"35829035317434717326932123578154982629742552737307", "94953759765105305946966067683156574377167401875275",
"88902802571733229619176668713819931811048770190271", "25267680276078003013678680992525463401061632866526",
"36270218540497705585629946580636237993140746255962", "24074486908231174977792365466257246923322810917141",
"91430288197103288597806669760892938638285025333403", "34413065578016127815921815005561868836468420090470",
"23053081172816430487623791969842487255036638784583", "11487696932154902810424020138335124462181441773470",
"63783299490636259666498587618221225225512486764533", "67720186971698544312419572409913959008952310058822",
"95548255300263520781532296796249481641953868218774", "76085327132285723110424803456124867697064507995236",
"37774242535411291684276865538926205024910326572967", "23701913275725675285653248258265463092207058596522",
"29798860272258331913126375147341994889534765745501", "18495701454879288984856827726077713721403798879715",
"38298203783031473527721580348144513491373226651381", "34829543829199918180278916522431027392251122869539",
"40957953066405232632538044100059654939159879593635", "29746152185502371307642255121183693803580388584903",
"41698116222072977186158236678424689157993532961922", "62467957194401269043877107275048102390895523597457",
"23189706772547915061505504953922979530901129967519", "86188088225875314529584099251203829009407770775672",
"11306739708304724483816533873502340845647058077308", "82959174767140363198008187129011875491310547126581",
"97623331044818386269515456334926366572897563400500", "42846280183517070527831839425882145521227251250327",
"55121603546981200581762165212827652751691296897789", "32238195734329339946437501907836945765883352399886",
"75506164965184775180738168837861091527357929701337", "62177842752192623401942399639168044983993173312731",
"32924185707147349566916674687634660915035914677504", "99518671430235219628894890102423325116913619626622",
"73267460800591547471830798392868535206946944540724", "76841822524674417161514036427982273348055556214818",
"97142617910342598647204516893989422179826088076852", "87783646182799346313767754307809363333018982642090",
"10848802521674670883215120185883543223812876952786", "71329612474782464538636993009049310363619763878039",
"62184073572399794223406235393808339651327408011116", "66627891981488087797941876876144230030984490851411",
"60661826293682836764744779239180335110989069790714", "85786944089552990653640447425576083659976645795096",
"66024396409905389607120198219976047599490197230297", "64913982680032973156037120041377903785566085089252",
"16730939319872750275468906903707539413042652315011", "94809377245048795150954100921645863754710598436791",
"78639167021187492431995700641917969777599028300699", "15368713711936614952811305876380278410754449733078",
"40789923115535562561142322423255033685442488917353", "44889911501440648020369068063960672322193204149535",
"41503128880339536053299340368006977710650566631954", "81234880673210146739058568557934581403627822703280",
"82616570773948327592232845941706525094512325230608", "22918802058777319719839450180888072429661980811197",
"77158542502016545090413245809786882778948721859617", "72107838435069186155435662884062257473692284509516",
"20849603980134001723930671666823555245252804609722", "53503534226472524250874054075591789781264330331690"};
char result[100];
int i;
double elapsed;
struct timespec start, end;
mpz_t a, b;
char n[100][51] = {"37107287533902102798797998220837590246510135740250", "46376937677490009712648124896970078050417018260538",
"74324986199524741059474233309513058123726617309629", "91942213363574161572522430563301811072406154908250",
"23067588207539346171171980310421047513778063246676", "89261670696623633820136378418383684178734361726757",
"28112879812849979408065481931592621691275889832738", "44274228917432520321923589422876796487670272189318",
"47451445736001306439091167216856844588711603153276", "70386486105843025439939619828917593665686757934951",
"62176457141856560629502157223196586755079324193331", "64906352462741904929101432445813822663347944758178",
"92575867718337217661963751590579239728245598838407", "58203565325359399008402633568948830189458628227828",
"80181199384826282014278194139940567587151170094390", "35398664372827112653829987240784473053190104293586",
"86515506006295864861532075273371959191420517255829", "71693888707715466499115593487603532921714970056938",
"54370070576826684624621495650076471787294438377604", "53282654108756828443191190634694037855217779295145",
"36123272525000296071075082563815656710885258350721", "45876576172410976447339110607218265236877223636045",
"17423706905851860660448207621209813287860733969412", "81142660418086830619328460811191061556940512689692",
"51934325451728388641918047049293215058642563049483", "62467221648435076201727918039944693004732956340691",
"15732444386908125794514089057706229429197107928209", "55037687525678773091862540744969844508330393682126",
"18336384825330154686196124348767681297534375946515", "80386287592878490201521685554828717201219257766954",
"78182833757993103614740356856449095527097864797581", "16726320100436897842553539920931837441497806860984",
"48403098129077791799088218795327364475675590848030", "87086987551392711854517078544161852424320693150332",
"59959406895756536782107074926966537676326235447210", "69793950679652694742597709739166693763042633987085",
"41052684708299085211399427365734116182760315001271", "65378607361501080857009149939512557028198746004375",
"35829035317434717326932123578154982629742552737307", "94953759765105305946966067683156574377167401875275",
"88902802571733229619176668713819931811048770190271", "25267680276078003013678680992525463401061632866526",
"36270218540497705585629946580636237993140746255962", "24074486908231174977792365466257246923322810917141",
"91430288197103288597806669760892938638285025333403", "34413065578016127815921815005561868836468420090470",
"23053081172816430487623791969842487255036638784583", "11487696932154902810424020138335124462181441773470",
"63783299490636259666498587618221225225512486764533", "67720186971698544312419572409913959008952310058822",
"95548255300263520781532296796249481641953868218774", "76085327132285723110424803456124867697064507995236",
"37774242535411291684276865538926205024910326572967", "23701913275725675285653248258265463092207058596522",
"29798860272258331913126375147341994889534765745501", "18495701454879288984856827726077713721403798879715",
"38298203783031473527721580348144513491373226651381", "34829543829199918180278916522431027392251122869539",
"40957953066405232632538044100059654939159879593635", "29746152185502371307642255121183693803580388584903",
"41698116222072977186158236678424689157993532961922", "62467957194401269043877107275048102390895523597457",
"23189706772547915061505504953922979530901129967519", "86188088225875314529584099251203829009407770775672",
"11306739708304724483816533873502340845647058077308", "82959174767140363198008187129011875491310547126581",
"97623331044818386269515456334926366572897563400500", "42846280183517070527831839425882145521227251250327",
"55121603546981200581762165212827652751691296897789", "32238195734329339946437501907836945765883352399886",
"75506164965184775180738168837861091527357929701337", "62177842752192623401942399639168044983993173312731",
"32924185707147349566916674687634660915035914677504", "99518671430235219628894890102423325116913619626622",
"73267460800591547471830798392868535206946944540724", "76841822524674417161514036427982273348055556214818",
"97142617910342598647204516893989422179826088076852", "87783646182799346313767754307809363333018982642090",
"10848802521674670883215120185883543223812876952786", "71329612474782464538636993009049310363619763878039",
"62184073572399794223406235393808339651327408011116", "66627891981488087797941876876144230030984490851411",
"60661826293682836764744779239180335110989069790714", "85786944089552990653640447425576083659976645795096",
"66024396409905389607120198219976047599490197230297", "64913982680032973156037120041377903785566085089252",
"16730939319872750275468906903707539413042652315011", "94809377245048795150954100921645863754710598436791",
"78639167021187492431995700641917969777599028300699", "15368713711936614952811305876380278410754449733078",
"40789923115535562561142322423255033685442488917353", "44889911501440648020369068063960672322193204149535",
"41503128880339536053299340368006977710650566631954", "81234880673210146739058568557934581403627822703280",
"82616570773948327592232845941706525094512325230608", "22918802058777319719839450180888072429661980811197",
"77158542502016545090413245809786882778948721859617", "72107838435069186155435662884062257473692284509516",
"20849603980134001723930671666823555245252804609722", "53503534226472524250874054075591789781264330331690"
};
char result[100];
int i;
double elapsed;
struct timespec start, end;
mpz_t a, b;
clock_gettime(CLOCK_MONOTONIC, &start);
clock_gettime(CLOCK_MONOTONIC, &start);
/* Using the GNU Multiple Precision Arithmetic Library (GMP)
* to sum the numbers and get the first 10 digits of the sum.*/
mpz_inits(a, b, NULL);
mpz_set_str(a, n[0], 10);
/* Using the GNU Multiple Precision Arithmetic Library (GMP)
* to sum the numbers and get the first 10 digits of the sum.*/
mpz_inits(a, b, NULL);
mpz_set_str(a, n[0], 10);
for(i = 1; i < 100; i++)
{
mpz_set_str(b, n[i], 10);
mpz_add(a, a, b);
}
for(i = 1; i < 100; i++)
{
mpz_set_str(b, n[i], 10);
mpz_add(a, a, b);
}
gmp_sprintf(result, "%Zd\n", a);
gmp_sprintf(result, "%Zd\n", a);
mpz_clears(a, b, NULL);
mpz_clears(a, b, NULL);
clock_gettime(CLOCK_MONOTONIC, &end);
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 13\n");
printf("Answer: ");
printf("Project Euler, Problem 13\n");
printf("Answer: ");
for(i = 0; i < 10; i++)
{
printf("%c", result[i]);
}
for(i = 0; i < 10; i++)
{
printf("%c", result[i]);
}
printf("\n");
printf("\n");
printf("Elapsed time: %.9lf seconds\n", elapsed);
printf("Elapsed time: %.9lf seconds\n", elapsed);
return 0;
return 0;
}

View File

@ -14,6 +14,8 @@
*
* NOTE: Once the chain starts the terms are allowed to go above one million.*/
#define _POSIX_C_SOURCE 199309L
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
@ -26,36 +28,36 @@ int collatz_found[N] = {0};
int main(int argc, char **argv)
{
int i, count, max = 0, max_l = 0;
double elapsed;
struct timespec start, end;
int i, count, max = 0, max_l = 0;
double elapsed;
struct timespec start, end;
clock_gettime(CLOCK_MONOTONIC, &start);
clock_gettime(CLOCK_MONOTONIC, &start);
for(i = 1; i < N; i++)
{
/* For each number from 1 to 1000000, find the length of the sequence
* and save its value, so that it can be used for the next numbers.*/
count = collatz_length(i);
collatz_found[i] = count;
if(count > max_l)
{
max_l = count;
max = i;
}
}
for(i = 1; i < N; i++)
{
/* For each number from 1 to 1000000, find the length of the sequence
* and save its value, so that it can be used for the next numbers.*/
count = collatz_length(i);
collatz_found[i] = count;
clock_gettime(CLOCK_MONOTONIC, &end);
if(count > max_l)
{
max_l = count;
max = i;
}
}
elapsed = (end.tv_sec - start.tv_sec) + (double)(end.tv_nsec - start.tv_nsec) / 1000000000;
clock_gettime(CLOCK_MONOTONIC, &end);
printf("Project Euler, Problem 14\n");
printf("Answer: %d\n", max);
elapsed = (end.tv_sec - start.tv_sec) + (double)(end.tv_nsec - start.tv_nsec) / 1000000000;
printf("Elapsed time: %.9lf seconds\n", elapsed);
printf("Project Euler, Problem 14\n");
printf("Answer: %d\n", max);
return 0;
printf("Elapsed time: %.9lf seconds\n", elapsed);
return 0;
}
/* Recursive function to calculate the Collatz sequence for n.
@ -63,16 +65,16 @@ int main(int argc, char **argv)
* Collatz(n)=1+Collatz(3*n+1).*/
int collatz_length(long int n)
{
if(n == 1)
return 1;
if(n == 1)
return 1;
/* If Collatz(n) has been previously calculated,
* just return the value.*/
if(n < N && collatz_found[n])
return collatz_found[n];
/* If Collatz(n) has been previously calculated,
* just return the value.*/
if(n < N && collatz_found[n])
return collatz_found[n];
if(n % 2 == 0)
return 1 + collatz_length(n/2);
else
return 1 + collatz_length(3*n+1);
if(n % 2 == 0)
return 1 + collatz_length(n/2);
else
return 1 + collatz_length(3*n+1);
}

View File

@ -1,6 +1,8 @@
/* Starting in the top left corner of a 2×2 grid, and only being able to move to the right and down, there are exactly 6 routes to the bottom right corner
* How many such routes are there through a 20×20 grid?*/
#define _POSIX_C_SOURCE 199309L
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
@ -8,33 +10,33 @@
int main(int argc, char **argv)
{
double elapsed;
struct timespec start, end;
mpz_t count, tmp;
double elapsed;
struct timespec start, end;
mpz_t count, tmp;
clock_gettime(CLOCK_MONOTONIC, &start);
clock_gettime(CLOCK_MONOTONIC, &start);
/* Using a combinatorial solution: in a 20x20 grid there will always be
* 20 movements to the right and 20 movements down, that can be represented
* as a string of Rs and Ds. The number of routes is the number of combinations.
* This is obtained calculating n!/(k!*(n-k)!), where n=40 and k=20. The GMP
* Library is used to calculate the factorials.*/
mpz_inits(count, tmp, NULL);
mpz_fac_ui(count, 40);
mpz_fac_ui(tmp, 20);
mpz_mul(tmp, tmp, tmp);
mpz_tdiv_q(count, count, tmp);
/* Using a combinatorial solution: in a 20x20 grid there will always be
* 20 movements to the right and 20 movements down, that can be represented
* as a string of Rs and Ds. The number of routes is the number of combinations.
* This is obtained calculating n!/(k!*(n-k)!), where n=40 and k=20. The GMP
* Library is used to calculate the factorials.*/
mpz_inits(count, tmp, NULL);
mpz_fac_ui(count, 40);
mpz_fac_ui(tmp, 20);
mpz_mul(tmp, tmp, tmp);
mpz_tdiv_q(count, count, tmp);
clock_gettime(CLOCK_MONOTONIC, &end);
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 15\n");
gmp_printf("Answer: %Zd\n", count);
printf("Project Euler, Problem 15\n");
gmp_printf("Answer: %Zd\n", count);
printf("Elapsed time: %.9lf seconds\n", elapsed);
printf("Elapsed time: %.9lf seconds\n", elapsed);
mpz_clears(count, tmp, NULL);
mpz_clears(count, tmp, NULL);
return 0;
return 0;
}

View File

@ -2,6 +2,8 @@
*
* What is the sum of the digits of the number 2^1000?*/
#define _POSIX_C_SOURCE 199309L
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
@ -9,37 +11,37 @@
int main(int argc, char **argv)
{
double elapsed;
struct timespec start, end;
mpz_t p, sum, r;
double elapsed;
struct timespec start, end;
mpz_t p, sum, r;
clock_gettime(CLOCK_MONOTONIC, &start);
clock_gettime(CLOCK_MONOTONIC, &start);
/* Simply calculate 2^1000 with the GMP Library
* and sum all the digits.*/
mpz_init_set_ui(p, 2);
mpz_init_set_ui(sum, 0);
mpz_init(r);
/* Simply calculate 2^1000 with the GMP Library
* and sum all the digits.*/
mpz_init_set_ui(p, 2);
mpz_init_set_ui(sum, 0);
mpz_init(r);
mpz_pow_ui(p, p, 1000);
mpz_pow_ui(p, p, 1000);
while(mpz_cmp_ui(p, 0))
{
/* To get each digit, simply get the reminder of the division by 10.*/
mpz_tdiv_qr_ui(p, r, p, 10);
mpz_add(sum, sum, r);
}
while(mpz_cmp_ui(p, 0))
{
/* To get each digit, simply get the reminder of the division by 10.*/
mpz_tdiv_qr_ui(p, r, p, 10);
mpz_add(sum, sum, r);
}
clock_gettime(CLOCK_MONOTONIC, &end);
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 16\n");
gmp_printf("Answer: %Zd\n", sum);
printf("Project Euler, Problem 16\n");
gmp_printf("Answer: %Zd\n", sum);
printf("Elapsed time: %.9lf seconds\n", elapsed);
printf("Elapsed time: %.9lf seconds\n", elapsed);
mpz_clears(p, sum, r, NULL);
mpz_clears(p, sum, r, NULL);
return 0;
return 0;
}

118
C/p017.c
View File

@ -5,78 +5,80 @@
* NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and forty-two) contains 23 letters and 115 (one hundred and fifteen)
* contains 20 letters. The use of "and" when writing out numbers is in compliance with British usage.*/
#define _POSIX_C_SOURCE 199309L
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, char **argv)
{
/* Number of letters for numbers from 1 to 19.*/
int n1_19[19] = {3, 3, 5, 4, 4, 3, 5, 5, 4, 3, 6, 6, 8, 8, 7, 7, 9, 8, 8};
/* Number of letters for "twenty", "thirty", ..., "ninety".*/
int n20_90[8] = {6, 6, 5, 5, 5, 7, 6, 6};
/* Number of letters for "one hundred and", "two hundred and", ...,
* "nine hundred and".*/
int n100_900[9] = {13, 13, 15, 14, 14, 13, 15, 15, 14};
/* Number of letters for 1000.*/
int n1000 = 11;
int sum = 0, i, j;
double elapsed;
struct timespec start, end;
/* Number of letters for numbers from 1 to 19.*/
int n1_19[19] = {3, 3, 5, 4, 4, 3, 5, 5, 4, 3, 6, 6, 8, 8, 7, 7, 9, 8, 8};
/* Number of letters for "twenty", "thirty", ..., "ninety".*/
int n20_90[8] = {6, 6, 5, 5, 5, 7, 6, 6};
/* Number of letters for "one hundred and", "two hundred and", ...,
* "nine hundred and".*/
int n100_900[9] = {13, 13, 15, 14, 14, 13, 15, 15, 14};
/* Number of letters for 1000.*/
int n1000 = 11;
int sum = 0, i, j;
double elapsed;
struct timespec start, end;
clock_gettime(CLOCK_MONOTONIC, &start);
clock_gettime(CLOCK_MONOTONIC, &start);
/* Sum the letters of the first 19 numbers.*/
for(i = 0; i < 19; i++)
{
sum += n1_19[i];
}
/* Sum the letters of the first 19 numbers.*/
for(i = 0; i < 19; i++)
{
sum += n1_19[i];
}
/* Add the letters of the numbers from 20 to 99.*/
for(i = 0; i < 8; i++)
{
/* "Twenty", "thirty", ... "ninety" are used ten times each
* (e.g. "twenty", "twenty one", "twenty two", ..., "twenty nine").*/
n20_90[i] *= 10;
/* Add "one", "two", ..., "nine".*/
for(j = 0; j < 9; j++)
{
n20_90[i] += n1_19[j];
}
sum += n20_90[i];
}
/* Add the letters of the numbers from 20 to 99.*/
for(i = 0; i < 8; i++)
{
/* "Twenty", "thirty", ... "ninety" are used ten times each
* (e.g. "twenty", "twenty one", "twenty two", ..., "twenty nine").*/
n20_90[i] *= 10;
/* Add "one", "two", ..., "nine".*/
for(j = 0; j < 9; j++)
{
n20_90[i] += n1_19[j];
}
sum += n20_90[i];
}
/* Add the letters of the numbers from 100 to 999.*/
for(i = 0; i < 9; i++)
{
/* "One hundred and", "two hundred and",... are used 100 times each.*/
n100_900[i] *= 100;
/* Add "one" to "nineteen".*/
for(j = 0; j < 19; j++)
{
n100_900[i] += n1_19[j];
}
/* Add "twenty" to "ninety nine", previously calculated.*/
for(j = 0; j < 8; j++)
{
n100_900[i] += n20_90[j];
}
/* "One hundred", "two hundred", ... don't have the "and", so remove
* three letters for each of them.*/
sum += n100_900[i] - 3;
}
/* Add the letters of the numbers from 100 to 999.*/
for(i = 0; i < 9; i++)
{
/* "One hundred and", "two hundred and",... are used 100 times each.*/
n100_900[i] *= 100;
/* Add "one" to "nineteen".*/
for(j = 0; j < 19; j++)
{
n100_900[i] += n1_19[j];
}
/* Add "twenty" to "ninety nine", previously calculated.*/
for(j = 0; j < 8; j++)
{
n100_900[i] += n20_90[j];
}
/* "One hundred", "two hundred", ... don't have the "and", so remove
* three letters for each of them.*/
sum += n100_900[i] - 3;
}
/* Add "one thousand".*/
sum += n1000;
/* Add "one thousand".*/
sum += n1000;
clock_gettime(CLOCK_MONOTONIC, &end);
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 17\n");
printf("Answer: %d\n", sum);
printf("Project Euler, Problem 17\n");
printf("Answer: %d\n", sum);
printf("Elapsed time: %.9lf seconds\n", elapsed);
printf("Elapsed time: %.9lf seconds\n", elapsed);
return 0;
return 0;
}

View File

@ -1,5 +1,5 @@
/* By starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top to bottom is 23.
*
*
* 3
* 7 4
* 2 4 6
@ -25,9 +25,11 @@
* 63 66 04 68 89 53 67 30 73 16 69 87 40 31
* 04 62 98 27 23 09 70 98 73 93 38 53 60 04 23
*
* NOTE: As there are only 16384 routes, it is possible to solve this problem by trying every route. However, Problem 67, is the same challenge
* NOTE: As there are only 16384 routes, it is possible to solve this problem by trying every route. However, Problem 67, is the same challenge
* with a triangle containing one-hundred rows; it cannot be solved by brute force, and requires a clever method! ;o)*/
#define _POSIX_C_SOURCE 199309L
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
@ -35,63 +37,63 @@
int main(int argc, char **argv)
{
int i, j, max;
int **triang;
double elapsed;
FILE *fp;
struct timespec start, end;
int i, j, max;
int **triang;
double elapsed;
FILE *fp;
struct timespec start, end;
clock_gettime(CLOCK_MONOTONIC, &start);
clock_gettime(CLOCK_MONOTONIC, &start);
if((triang = (int **)malloc(15*sizeof(int *))) == NULL)
{
fprintf(stderr, "Error while allocating memory\n");
return 1;
}
if((triang = (int **)malloc(15*sizeof(int *))) == NULL)
{
fprintf(stderr, "Error while allocating memory\n");
return 1;
}
for(i = 1; i <= 15; i++)
{
if((triang[i-1] = (int *)malloc(i*sizeof(int))) == NULL)
{
fprintf(stderr, "Error while allocating memory\n");
return 1;
}
}
for(i = 1; i <= 15; i++)
{
if((triang[i-1] = (int *)malloc(i*sizeof(int))) == NULL)
{
fprintf(stderr, "Error while allocating memory\n");
return 1;
}
}
if((fp = fopen("triang.txt", "r")) == NULL)
{
fprintf(stderr, "Error while opening file %s\n", "triang.txt");
return 1;
}
if((fp = fopen("triang.txt", "r")) == NULL)
{
fprintf(stderr, "Error while opening file %s\n", "triang.txt");
return 1;
}
for(i = 1; i <= 15; i++)
{
for(j = 0; j < i; j++)
{
fscanf(fp, "%d", &triang[i-1][j]);
}
}
for(i = 1; i <= 15; i++)
{
for(j = 0; j < i; j++)
{
fscanf(fp, "%d", &triang[i-1][j]);
}
}
fclose(fp);
fclose(fp);
/* Use the function implemented in projecteuler.c to find the maximum path.*/
max = find_max_path(triang, 15);
/* Use the function implemented in projecteuler.c to find the maximum path.*/
max = find_max_path(triang, 15);
for(i = 0; i < 15; i++)
{
free(triang[i]);
}
for(i = 0; i < 15; i++)
{
free(triang[i]);
}
free(triang);
free(triang);
clock_gettime(CLOCK_MONOTONIC, &end);
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 18\n");
printf("Answer: %d\n", max);
printf("Project Euler, Problem 18\n");
printf("Answer: %d\n", max);
printf("Elapsed time: %.9lf seconds\n", elapsed);
printf("Elapsed time: %.9lf seconds\n", elapsed);
return 0;
return 0;
}

126
C/p019.c
View File

@ -11,6 +11,8 @@
*
* How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?*/
#define _POSIX_C_SOURCE 199309L
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
@ -20,75 +22,75 @@ typedef enum {mon, tue, wed, thu, fri, sat, sun} days;
int main(int argc, char **argv)
{
int year, i, limit, count = 0;
double elapsed;
struct timespec start, end;
months month;
days day;
int year, i, limit, count = 0;
double elapsed;
struct timespec start, end;
months month;
days day;
clock_gettime(CLOCK_MONOTONIC, &start);
clock_gettime(CLOCK_MONOTONIC, &start);
day = mon;
month = jan;
year = 1900;
day = mon;
month = jan;
year = 1900;
while(year < 2001)
{
/* February has 29 days on leap years, otherwise 28. Leap years are those
* divisible by 4, but not if they're divisible by 100, except when they're
* divisible by 400.*/
if(month == feb)
{
if(year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
{
limit = 29;
}
else
{
limit = 28;
}
}
/* April, June, September and November have 30 days.*/
else if(month == apr || month == jun || month == sep || month == nov)
{
limit = 30;
}
/* All other months have 31 days.*/
else
{
limit = 31;
}
/* Loop on every day of the month.*/
for(i = 1; i <= limit; i++)
{
/* If it's the first day of the month and it's Sunday, increase
* counter, except if year=1900 (we need to count Sundays from
* 1901 to 2000.*/
if(year > 1900 && i == 1 && day == sun)
{
count++;
}
/* Change day of the week.*/
day = (day + 1) % 7;
}
/* At the end of the month, go to next month.*/
month = (month + 1) % 12;
while(year < 2001)
{
/* February has 29 days on leap years, otherwise 28. Leap years are those
* divisible by 4, but not if they're divisible by 100, except when they're
* divisible by 400.*/
if(month == feb)
{
if(year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
{
limit = 29;
}
else
{
limit = 28;
}
}
/* April, June, September and November have 30 days.*/
else if(month == apr || month == jun || month == sep || month == nov)
{
limit = 30;
}
/* All other months have 31 days.*/
else
{
limit = 31;
}
/* Loop on every day of the month.*/
for(i = 1; i <= limit; i++)
{
/* If it's the first day of the month and it's Sunday, increase
* counter, except if year=1900 (we need to count Sundays from
* 1901 to 2000.*/
if(year > 1900 && i == 1 && day == sun)
{
count++;
}
/* Change day of the week.*/
day = (day + 1) % 7;
}
/* At the end of the month, go to next month.*/
month = (month + 1) % 12;
/* If we're back to january, increase the year.*/
if(month == jan)
{
year++;
}
}
/* If we're back to january, increase the year.*/
if(month == jan)
{
year++;
}
}
clock_gettime(CLOCK_MONOTONIC, &end);
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 19\n");
printf("Answer: %d\n", count);
printf("Project Euler, Problem 19\n");
printf("Answer: %d\n", count);
printf("Elapsed time: %.9lf seconds\n", elapsed);
printf("Elapsed time: %.9lf seconds\n", elapsed);
return 0;
return 0;
}

View File

@ -5,6 +5,8 @@
*
* Find the sum of the digits in the number 100!*/
#define _POSIX_C_SOURCE 199309L
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
@ -12,35 +14,35 @@
int main(int argc, char **argv)
{
double elapsed;
struct timespec start, end;
mpz_t fact, r, sum;
double elapsed;
struct timespec start, end;
mpz_t fact, r, sum;
clock_gettime(CLOCK_MONOTONIC, &start);
clock_gettime(CLOCK_MONOTONIC, &start);
/* Calculate the factorial using the GMP Library and sum the digits.*/
mpz_inits(fact, r, sum, NULL);
/* Calculate the factorial using the GMP Library and sum the digits.*/
mpz_inits(fact, r, sum, NULL);
mpz_fac_ui(fact, 100);
mpz_fac_ui(fact, 100);
mpz_set_ui(sum, 0);
mpz_set_ui(sum, 0);
while(mpz_cmp_ui(fact, 0))
{
mpz_tdiv_qr_ui(fact, r, fact, 10);
mpz_add(sum, sum, r);
}
while(mpz_cmp_ui(fact, 0))
{
mpz_tdiv_qr_ui(fact, r, fact, 10);
mpz_add(sum, sum, r);
}
clock_gettime(CLOCK_MONOTONIC, &end);
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 20\n");
gmp_printf("Answer: %Zd\n", sum);
printf("Project Euler, Problem 20\n");
gmp_printf("Answer: %Zd\n", sum);
printf("Elapsed time: %.9lf seconds\n", elapsed);
printf("Elapsed time: %.9lf seconds\n", elapsed);
mpz_clears(fact, r, sum, NULL);
mpz_clears(fact, r, sum, NULL);
return 0;
return 0;
}

View File

@ -6,6 +6,8 @@
*
* Evaluate the sum of all the amicable numbers under 10000.*/
#define _POSIX_C_SOURCE 199309L
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
@ -14,35 +16,35 @@
int main(int argc, char **argv)
{
int i, n, sum = 0;
double elapsed;
struct timespec start, end;
int i, n, sum = 0;
double elapsed;
struct timespec start, end;
clock_gettime(CLOCK_MONOTONIC, &start);
clock_gettime(CLOCK_MONOTONIC, &start);
for(i = 2; i < 10000; i++)
{
/* Calculate the sum of proper divisors with the function
* implemented in projecteuler.c.*/
n = sum_of_divisors(i, 1);
/* If i!=n and the sum of proper divisors of n=i,
* sum the pair of numbers and add it to the total.*/
if(i != n && sum_of_divisors(n, 1) == i)
{
sum += i + n;
}
}
for(i = 2; i < 10000; i++)
{
/* Calculate the sum of proper divisors with the function
* implemented in projecteuler.c.*/
n = sum_of_divisors(i, 1);
/* If i!=n and the sum of proper divisors of n=i,
* sum the pair of numbers and add it to the total.*/
if(i != n && sum_of_divisors(n, 1) == i)
{
sum += i + n;
}
}
sum /= 2;
sum /= 2;
clock_gettime(CLOCK_MONOTONIC, &end);
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 21\n");
printf("Answer: %d\n", sum);
printf("Project Euler, Problem 21\n");
printf("Answer: %d\n", sum);
printf("Elapsed time: %.9lf seconds\n", elapsed);
printf("Elapsed time: %.9lf seconds\n", elapsed);
return 0;
return 0;
}

120
C/p022.c
View File

@ -6,6 +6,8 @@
*
* What is the total of all the name scores in the file?*/
#define _POSIX_C_SOURCE 199309L
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -16,86 +18,86 @@ int compare(void *string1, void *string2);
int main(int argc, char **argv)
{
FILE *fp;
int i, j, n, len, score, sum = 0;
double elapsed;
char *line, **names;
struct timespec start, end;
FILE *fp;
int i, j, n, len, score, sum = 0;
double elapsed;
char *line, **names;
struct timespec start, end;
clock_gettime(CLOCK_MONOTONIC, &start);
clock_gettime(CLOCK_MONOTONIC, &start);
if((fp = fopen("names.txt", "r")) == NULL)
{
fprintf(stderr, "Error while opening file %s\n", "names.txt");
return 1;
}
if((fp = fopen("names.txt", "r")) == NULL)
{
fprintf(stderr, "Error while opening file %s\n", "names.txt");
return 1;
}
fscanf(fp, "%ms", &line);
fscanf(fp, "%ms", &line);
fclose(fp);
fclose(fp);
len = strlen(line);
n = 1;
len = strlen(line);
n = 1;
/* Count the names in the file.*/
for(i = 0; i < len; i++)
{
if(line[i] == ',')
{
n++;
}
}
/* Count the names in the file.*/
for(i = 0; i < len; i++)
{
if(line[i] == ',')
{
n++;
}
}
if((names = (char **)malloc(n*sizeof(char *))) == NULL)
{
fprintf(stderr, "Error while allocating memory\n");
return 1;
}
if((names = (char **)malloc(n*sizeof(char *))) == NULL)
{
fprintf(stderr, "Error while allocating memory\n");
return 1;
}
/* Save each name in a string.*/
names[0] = strtok(line, ",\"");
/* Save each name in a string.*/
names[0] = strtok(line, ",\"");
for(i = 1; i < n; i++)
{
names[i] = strtok(NULL, ",\"");
}
for(i = 1; i < n; i++)
{
names[i] = strtok(NULL, ",\"");
}
/* Use quick_sort algorithm implemented in projecteuler.c to sort the names.*/
quick_sort((void **)names, 0, n-1, compare);
/* Use quick_sort algorithm implemented in projecteuler.c to sort the names.*/
quick_sort((void **)names, 0, n-1, compare);
/* Calculate the score of each name an multiply by its position.*/
for(i = 0; i < n; i++)
{
len = strlen(names[i]);
score = 0;
for(j = 0; j < len; j++)
{
score += names[i][j] - 'A' + 1;
}
score *= (i + 1);
sum += score;
}
/* Calculate the score of each name an multiply by its position.*/
for(i = 0; i < n; i++)
{
len = strlen(names[i]);
score = 0;
for(j = 0; j < len; j++)
{
score += names[i][j] - 'A' + 1;
}
score *= (i + 1);
sum += score;
}
free(line);
free(line);
clock_gettime(CLOCK_MONOTONIC, &end);
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 22\n");
printf("Answer: %d\n", sum);
printf("Elapsed time: %.9lf seconds\n", elapsed);
printf("Project Euler, Problem 22\n");
printf("Answer: %d\n", sum);
printf("Elapsed time: %.9lf seconds\n", elapsed);
return 0;
return 0;
}
/* Function to compare two strings, to pass to the quick_sort function.*/
int compare(void *string1, void *string2)
{
char *s1, *s2;
char *s1, *s2;
s1 = (char *)string1;
s2 = (char *)string2;
s1 = (char *)string1;
s2 = (char *)string2;
return strcmp(s1, s2);
return strcmp(s1, s2);
}

100
C/p023.c
View File

@ -1,4 +1,4 @@
/* A perfect number is a number for which the sum of its proper divisors is exactly equal to the number.
/* A perfect number is a number for which the sum of its proper divisors is exactly equal to the number.
* For example, the sum of the proper divisors of 28 would be 1 + 2 + 4 + 7 + 14 = 28, which means that 28 is a perfect number.
*
* A number n is called deficient if the sum of its proper divisors is less than n and it is called abundant if this sum exceeds n.
@ -10,6 +10,8 @@
*
* Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers.*/
#define _POSIX_C_SOURCE 199309L
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
@ -20,69 +22,69 @@ int is_abundant(int n);
int main(int argc, char **argv)
{
int ab_nums[28123], sums[28123] = {0};
int i, j, sum;
double elapsed;
struct timespec start, end;
int ab_nums[28123], sums[28123] = {0};
int i, j, sum;
double elapsed;
struct timespec start, end;
clock_gettime(CLOCK_MONOTONIC, &start);
clock_gettime(CLOCK_MONOTONIC, &start);
for(i = 0; i < 28123; i++)
{
/* Find all abundant numbers smaller than 28123.*/
ab_nums[i] = is_abundant(i+1);
}
for(i = 0; i < 28123; i++)
{
/* Find all abundant numbers smaller than 28123.*/
ab_nums[i] = is_abundant(i+1);
}
/* For every abundant number, sum every other abundant number greater
* than itself, until the sum exceeds 28123. Record that the resulting
* number is the sum of two abundant numbers.*/
for(i = 0; i < 28123; i++)
{
if(ab_nums[i])
{
for(j = i; j < 28123; j++)
{
if(ab_nums[j])
/* For every abundant number, sum every other abundant number greater
* than itself, until the sum exceeds 28123. Record that the resulting
* number is the sum of two abundant numbers.*/
for(i = 0; i < 28123; i++)
{
if(ab_nums[i])
{
for(j = i; j < 28123; j++)
{
sum = i + j + 2;
if(ab_nums[j])
{
sum = i + j + 2;
if(sum <= 28123)
{
sums[sum-1] = 1;
}
else
{
break;
}
if(sum <= 28123)
{
sums[sum-1] = 1;
}
else
{
break;
}
}
}
}
}
}
}
}
sum = 0;
sum = 0;
/* Sum every number that was not found as a sum of two abundant numbers.*/
for(i = 0; i < 28123; i++)
{
if(!sums[i])
{
sum += i + 1;
}
}
/* Sum every number that was not found as a sum of two abundant numbers.*/
for(i = 0; i < 28123; i++)
{
if(!sums[i])
{
sum += i + 1;
}
}
clock_gettime(CLOCK_MONOTONIC, &end);
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 23\n");
printf("Answer: %d\n", sum);
printf("Project Euler, Problem 23\n");
printf("Answer: %d\n", sum);
printf("Elapsed time: %.9lf seconds\n", elapsed);
printf("Elapsed time: %.9lf seconds\n", elapsed);
return 0;
return 0;
}
int is_abundant(int n)
{
return sum_of_divisors(n, 1) > n;
return sum_of_divisors(n, 1) > n;
}

View File

@ -5,6 +5,8 @@
*
* What is the millionth lexicographic permutation of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9?*/
#define _POSIX_C_SOURCE 199309L
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
@ -14,69 +16,69 @@ int compare(void *a, void *b);
int main(int argc, char **argv)
{
int i, res[10];
int **perm;
double elapsed;
struct timespec start, end;
int i, res[10];
int **perm;
double elapsed;
struct timespec start, end;
clock_gettime(CLOCK_MONOTONIC, &start);
clock_gettime(CLOCK_MONOTONIC, &start);
if((perm = (int **)malloc(10*sizeof(int *))) == NULL)
{
fprintf(stderr, "Error while allocating memory\n");
return 1;
}
if((perm = (int **)malloc(10*sizeof(int *))) == NULL)
{
fprintf(stderr, "Error while allocating memory\n");
return 1;
}
for(i = 0; i < 10; i++)
{
if((perm[i] = (int *)malloc(sizeof(int))) == NULL)
{
fprintf(stderr, "Error while allocating memory\n");
return 1;
}
*perm[i] = i;
}
for(i = 0; i < 10; i++)
{
if((perm[i] = (int *)malloc(sizeof(int))) == NULL)
{
fprintf(stderr, "Error while allocating memory\n");
return 1;
}
*perm[i] = i;
}
for(i = 0; i < 999999; i++)
{
/* Function that generates permutations in lexicographic order.
* Finish when the 1000000th is found.*/
next_permutation((void **)perm, 10, compare);
}
for(i = 0; i < 999999; i++)
{
/* Function that generates permutations in lexicographic order.
* Finish when the 1000000th is found.*/
next_permutation((void **)perm, 10, compare);
}
for(i = 0; i < 10; i++)
{
res[i] = *perm[i];
free(perm[i]);
}
for(i = 0; i < 10; i++)
{
res[i] = *perm[i];
free(perm[i]);
}
free(perm);
free(perm);
clock_gettime(CLOCK_MONOTONIC, &end);
clock_gettime(CLOCK_MONOTONIC, &end);
printf("Project Euler, Problem 24\n");
printf("Answer: ");
printf("Project Euler, Problem 24\n");
printf("Answer: ");
for(i = 0; i < 10; i++)
{
printf("%d", res[i]);
}
for(i = 0; i < 10; i++)
{
printf("%d", res[i]);
}
printf("\n");
printf("\n");
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("Elapsed time: %.9lf seconds\n", elapsed);
printf("Elapsed time: %.9lf seconds\n", elapsed);
return 0;
return 0;
}
int compare(void *a, void *b)
{
int *n1, *n2;
int *n1, *n2;
n1 = (int *)a;
n2 = (int *)b;
n1 = (int *)a;
n2 = (int *)b;
return *n1 - *n2;
return *n1 - *n2;
}

View File

@ -19,6 +19,8 @@
*
* What is the index of the first term in the Fibonacci sequence to contain 1000 digits?*/
#define _POSIX_C_SOURCE 199309L
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -27,61 +29,61 @@
int main(int argc, char **argv)
{
int i;
double elapsed;
struct timespec start, end;
mpz_t f1, f2, fn;
char *num;
size_t size;
int i;
double elapsed;
struct timespec start, end;
mpz_t f1, f2, fn;
char *num;
size_t size;
clock_gettime(CLOCK_MONOTONIC, &start);
clock_gettime(CLOCK_MONOTONIC, &start);
mpz_init_set_ui(f1, 1);
mpz_init_set_ui(f2, 1);
mpz_init(fn);
mpz_init_set_ui(f1, 1);
mpz_init_set_ui(f2, 1);
mpz_init(fn);
i = 2;
i = 2;
while(1)
{
/* Use the GMP Library to calculate the Fibonacci numbers.*/
mpz_add(fn, f1, f2);
i++;
while(1)
{
/* Use the GMP Library to calculate the Fibonacci numbers.*/
mpz_add(fn, f1, f2);
i++;
/* The function mpz_sizeinbase gives the number of digits of
* the number in the given base, but the result is either exact
* or one too big. To check the exact size, the number is
* converted to string and the strlen function is used.*/
if((size = mpz_sizeinbase(fn, 10)) >= 1000)
{
if((num = (char *)malloc((2+size)*sizeof(char))) == NULL)
{
fprintf(stderr, "Error while allocating memory\n");
return 1;
}
gmp_sprintf(num, "%Zd", fn);
size = strlen(num);
free(num);
if(size == 1000)
{
break;
}
}
/* The function mpz_sizeinbase gives the number of digits of
* the number in the given base, but the result is either exact
* or one too big. To check the exact size, the number is
* converted to string and the strlen function is used.*/
if((size = mpz_sizeinbase(fn, 10)) >= 1000)
{
if((num = (char *)malloc((2+size)*sizeof(char))) == NULL)
{
fprintf(stderr, "Error while allocating memory\n");
return 1;
}
gmp_sprintf(num, "%Zd", fn);
size = strlen(num);
free(num);
if(size == 1000)
{
break;
}
}
mpz_set(f1, f2);
mpz_set(f2, fn);
}
mpz_set(f1, f2);
mpz_set(f2, fn);
}
mpz_clears(f1, f2, fn, NULL);
mpz_clears(f1, f2, fn, NULL);
clock_gettime(CLOCK_MONOTONIC, &end);
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 25\n");
printf("Answer: %d\n", i);
printf("Project Euler, Problem 25\n");
printf("Answer: %d\n", i);
printf("Elapsed time: %.9lf seconds\n", elapsed);
printf("Elapsed time: %.9lf seconds\n", elapsed);
return 0;
return 0;
}

100
C/p026.c
View File

@ -14,6 +14,8 @@
*
* Find the value of d < 1000 for which 1/d contains the longest recurring cycle in its decimal fraction part.*/
#define _POSIX_C_SOURCE 199309L
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
@ -22,67 +24,67 @@
int main(int argc, char **argv)
{
int i, j, n, max = 0, max_n = 0;
double elapsed;
struct timespec start, end;
mpz_t k, div;
int i, j, n, max = 0, max_n = 0;
double elapsed;
struct timespec start, end;
mpz_t k, div;
clock_gettime(CLOCK_MONOTONIC, &start);
clock_gettime(CLOCK_MONOTONIC, &start);
mpz_init(k);
mpz_init(div);
mpz_init(k);
mpz_init(div);
for(i = 2; i < 1000; i++)
{
j = i;
for(i = 2; i < 1000; i++)
{
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;
/* 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;
while(j % 5 == 0 && j > 1)
j /= 5;
/* 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);
/* 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 is the length of the repeating cycle.*/
while(!mpz_divisible_p(k, div))
{
n++;
mpz_mul_ui(k, k, 10);
mpz_add_ui(k, k, 9);
}
/* 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 is the length of the repeating cycle.*/
while(!mpz_divisible_p(k, div))
{
n++;
mpz_mul_ui(k, k, 10);
mpz_add_ui(k, k, 9);
}
if(n > max)
{
max = n;
max_n = i;
}
}
}
if(n > max)
{
max = n;
max_n = i;
}
}
}
mpz_clears(k, div, NULL);
mpz_clears(k, div, NULL);
clock_gettime(CLOCK_MONOTONIC, &end);
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);
printf("Project Euler, Problem 26\n");
printf("Answer: %d\n", max_n);
printf("Elapsed time: %.9lf seconds\n", elapsed);
printf("Elapsed time: %.9lf seconds\n", elapsed);
return 0;
return 0;
}

View File

@ -17,6 +17,8 @@
*
* 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.*/
#define _POSIX_C_SOURCE 199309L
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
@ -25,56 +27,56 @@
int main(int argc, char **argv)
{
int a, b, n, p, count, max = 0, save_a, save_b;
double elapsed;
struct timespec start, end;
int a, b, n, p, count, max = 0, save_a, save_b;
double elapsed;
struct timespec start, end;
clock_gettime(CLOCK_MONOTONIC, &start);
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++)
{
/* For n=0, n^2+an+b=b, so b must be prime.*/
if(is_prime(b))
{
n = 0;
count = 0;
while(1)
/* 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++)
{
/* 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))
{
count++;
n++;
}
else
{
break;
}
while(1)
{
p = n * n + a * n + b;
if(p > 1 && is_prime(p))
{
count++;
n++;
}
else
{
break;
}
}
if(count > max)
{
max = count;
save_a = a;
save_b = b;
}
}
}
}
if(count > max)
{
max = count;
save_a = a;
save_b = b;
}
}
}
}
clock_gettime(CLOCK_MONOTONIC, &end);
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 27\n");
printf("Answer: %d\n", save_a * save_b);
printf("Project Euler, Problem 27\n");
printf("Answer: %d\n", save_a * save_b);
printf("Elapsed time: %.9lf seconds\n", elapsed);
printf("Elapsed time: %.9lf seconds\n", elapsed);
return 0;
return 0;
}

View File

@ -10,6 +10,8 @@
*
* What is the sum of the numbers on the diagonals in a 1001 by 1001 spiral formed in the same way?*/
#define _POSIX_C_SOURCE 199309L
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
@ -18,35 +20,35 @@
int main(int argc, char **argv)
{
int i, j, step = 0, limit = N * N, sum = 1;
double elapsed;
struct timespec start, end;
int i, j, step = 0, limit = N * N, sum = 1;
double elapsed;
struct timespec start, end;
clock_gettime(CLOCK_MONOTONIC, &start);
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)
{
step += 2;
}
/* 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)
{
step += 2;
}
j += step;
sum += j;
}
j += step;
sum += j;
}
clock_gettime(CLOCK_MONOTONIC, &end);
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 28\n");
printf("Answer: %d\n", sum);
printf("Project Euler, Problem 28\n");
printf("Answer: %d\n", sum);
printf("Elapsed time: %.9lf seconds\n", elapsed);
printf("Elapsed time: %.9lf seconds\n", elapsed);
return 0;
return 0;
}

122
C/p029.c
View File

@ -11,6 +11,8 @@
*
* How many distinct terms are in the sequence generated by ab for 2 a 100 and 2 b 100?*/
#define _POSIX_C_SOURCE 199309L
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
@ -21,86 +23,86 @@ int compare(void *a, void *b);
int main(int argc, char **argv)
{
mpz_t a;
mpz_t **powers;
int i, j, count;
double elapsed;
struct timespec start, end;
mpz_t a;
mpz_t **powers;
int i, j, count;
double elapsed;
struct timespec start, end;
clock_gettime(CLOCK_MONOTONIC, &start);
clock_gettime(CLOCK_MONOTONIC, &start);
if((powers = (mpz_t **)malloc(9801*sizeof(mpz_t *))) == NULL)
{
fprintf(stderr, "Error while allocating memory\n");
return 1;
}
if((powers = (mpz_t **)malloc(9801*sizeof(mpz_t *))) == NULL)
{
fprintf(stderr, "Error while allocating memory\n");
return 1;
}
for(i = 0; i < 9801; i++)
{
if((powers[i] = (mpz_t *)malloc(sizeof(mpz_t))) == NULL)
{
fprintf(stderr, "Error while allocating memory\n");
return 1;
}
}
for(i = 0; i < 9801; i++)
{
if((powers[i] = (mpz_t *)malloc(sizeof(mpz_t))) == NULL)
{
fprintf(stderr, "Error while allocating memory\n");
return 1;
}
}
mpz_init(a);
mpz_init(a);
for(i = 0; i < 9801; i++)
{
mpz_init(*powers[i]);
}
for(i = 0; i < 9801; i++)
{
mpz_init(*powers[i]);
}
/* Using the GMP Library to calculate all the powers.*/
for(i = 2; i <= 100; i++)
{
mpz_set_ui(a, i);
for(j = 2; j <= 100; j++)
{
mpz_pow_ui(*powers[(i-2)*99+j-2], a, j);
}
}
/* Using the GMP Library to calculate all the powers.*/
for(i = 2; i <= 100; i++)
{
mpz_set_ui(a, i);
for(j = 2; j <= 100; j++)
{
mpz_pow_ui(*powers[(i-2)*99+j-2], a, j);
}
}
mpz_clear(a);
mpz_clear(a);
/* Sort the values and count the different values.*/
quick_sort((void **)powers, 0, 9800, compare);
count = 1;
/* Sort the values and count the different values.*/
quick_sort((void **)powers, 0, 9800, compare);
count = 1;
for(i = 1; i < 9801; i++)
{
if(mpz_cmp(*powers[i], *powers[i-1]))
{
count++;
}
}
for(i = 1; i < 9801; i++)
{
if(mpz_cmp(*powers[i], *powers[i-1]))
{
count++;
}
}
for(i = 0; i < 9801; i++)
{
mpz_clear(*powers[i]);
free(powers[i]);
}
for(i = 0; i < 9801; i++)
{
mpz_clear(*powers[i]);
free(powers[i]);
}
free(powers);
free(powers);
clock_gettime(CLOCK_MONOTONIC, &end);
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 29\n");
printf("Answer: %d\n", count);
printf("Project Euler, Problem 29\n");
printf("Answer: %d\n", count);
printf("Elapsed time: %.9lf seconds\n", elapsed);
printf("Elapsed time: %.9lf seconds\n", elapsed);
return 0;
return 0;
}
int compare(void *a, void *b)
{
mpz_t *n1, *n2;
mpz_t *n1, *n2;
n1 = (mpz_t *)a;
n2 = (mpz_t *)b;
n1 = (mpz_t *)a;
n2 = (mpz_t *)b;
return mpz_cmp(*n1, *n2);
return mpz_cmp(*n1, *n2);
}

View File

@ -10,6 +10,8 @@
*
* Find the sum of all the numbers that can be written as the sum of fifth powers of their digits.*/
#define _POSIX_C_SOURCE 199309L
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
@ -17,41 +19,41 @@
int main(int argc, char **argv)
{
int i, j, digit, sum, sum_tot = 0;
double elapsed;
struct timespec start, end;
int i, j, digit, sum, sum_tot = 0;
double elapsed;
struct timespec start, end;
clock_gettime(CLOCK_MONOTONIC, &start);
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;
sum = 0;
/* 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;
sum = 0;
while(j > 0)
{
digit = j % 10;
sum += (pow(digit, 5));
j /= 10;
}
while(j > 0)
{
digit = j % 10;
sum += (pow(digit, 5));
j /= 10;
}
if(sum == i)
{
sum_tot += i;
}
}
if(sum == i)
{
sum_tot += i;
}
}
clock_gettime(CLOCK_MONOTONIC, &end);
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 30\n");
printf("Answer: %d\n", sum_tot);
printf("Project Euler, problem 30\n");
printf("Answer: %d\n", sum_tot);
printf("Elapsed time: %.9lf seconds\n", elapsed);
printf("Elapsed time: %.9lf seconds\n", elapsed);
return 0;
return 0;
}

View File

@ -8,6 +8,8 @@
*
* How many different ways can £2 be made using any number of coins?*/
#define _POSIX_C_SOURCE 199309L
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
@ -18,49 +20,49 @@ int coins[8] = {1, 2, 5, 10, 20, 50, 100, 200};
int main(int argc, char **argv)
{
int n;
double elapsed;
struct timespec start, end;
int n;
double elapsed;
struct timespec start, end;
clock_gettime(CLOCK_MONOTONIC, &start);
clock_gettime(CLOCK_MONOTONIC, &start);
n = count(0, 0, 0);
n = count(0, 0, 0);
clock_gettime(CLOCK_MONOTONIC, &end);
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 31\n");
printf("Answer: %d\n", n);
printf("Project Euler, Problem 31\n");
printf("Answer: %d\n", n);
printf("Elapsed time: %.9lf seconds\n", elapsed);
printf("Elapsed time: %.9lf seconds\n", elapsed);
return 0;
return 0;
}
/* Simple recursive function that tries every combination.*/
int count(int value, int n, int i)
{
int j;
int j;
for(j = i; j < 8; j++)
{
value += coins[j];
for(j = i; j < 8; j++)
{
value += coins[j];
if(value == 200)
{
return n + 1;
}
else if(value > 200)
{
return n;
}
else
{
n = count(value, n, j);
value -= coins[j];
}
}
if(value == 200)
{
return n + 1;
}
else if(value > 200)
{
return n;
}
else
{
n = count(value, n, j);
value -= coins[j];
}
}
return n;
return n;
}

200
C/p032.c
View File

@ -7,6 +7,8 @@
*
* HINT: Some products can be obtained in more than one way so be sure to only include it once in your sum.*/
#define _POSIX_C_SOURCE 199309L
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -17,129 +19,129 @@ int compare(void *a, void *b);
int main(int argc, char **argv)
{
int i, j, p, sum, n = 0, num;
int **products;
char num_s[10];
double elapsed;
struct timespec start, end;
int i, j, p, sum, n = 0, num;
int **products;
char num_s[10];
double elapsed;
struct timespec start, end;
clock_gettime(CLOCK_MONOTONIC, &start);
clock_gettime(CLOCK_MONOTONIC, &start);
/* Initially I used a bigger array, but printing the resulting products
* shows that 10 values are sufficient.*/
if((products = (int **)malloc(10*sizeof(int *))) == NULL)
{
fprintf(stderr, "Error while allocating memory\n");
return 1;
}
/* Initially I used a bigger array, but printing the resulting products
* shows that 10 values are sufficient.*/
if((products = (int **)malloc(10*sizeof(int *))) == NULL)
{
fprintf(stderr, "Error while allocating memory\n");
return 1;
}
for(i = 0; i < 10; i++)
{
if((products[i] = (int *)malloc(sizeof(int))) == NULL)
{
fprintf(stderr, "Error while allocating memory\n");
return 1;
}
}
for(i = 0; i < 10; i++)
{
if((products[i] = (int *)malloc(sizeof(int))) == NULL)
{
fprintf(stderr, "Error while allocating memory\n");
return 1;
}
}
/* To get a 1 to 9 pandigital concatenation of the two factors and product,
* we need to multiply a 1 digit number times a 4 digit numbers (the biggest
* one digit number 9 times the biggest 3 digit number 999 multiplied give
* 8991 and the total digit count is 8, which is not enough), or a 2 digit
* number times a 3 digit number (the smallest two different 3 digits number,
* 100 and 101, multiplied give 10100, and the total digit count is 11, which
* is too many). The outer loop starts at 2 because 1 times any number gives
* the same number, so its digit will be repeated and the result can't be
* pandigital. The nested loop starts from 1234 because it's the smallest
* 4-digit number with no repeated digits, and it ends at 4987 because it's
* the biggest number without repeated digits that multiplied by 2 gives a
* 4 digit number. */
for(i = 2; i < 9; i++)
{
for(j = 1234; j < 4987; j++)
{
p = i * j;
sprintf(num_s, "%d%d%d", i, j, p);
/* To get a 1 to 9 pandigital concatenation of the two factors and product,
* we need to multiply a 1 digit number times a 4 digit numbers (the biggest
* one digit number 9 times the biggest 3 digit number 999 multiplied give
* 8991 and the total digit count is 8, which is not enough), or a 2 digit
* number times a 3 digit number (the smallest two different 3 digits number,
* 100 and 101, multiplied give 10100, and the total digit count is 11, which
* is too many). The outer loop starts at 2 because 1 times any number gives
* the same number, so its digit will be repeated and the result can't be
* pandigital. The nested loop starts from 1234 because it's the smallest
* 4-digit number with no repeated digits, and it ends at 4987 because it's
* the biggest number without repeated digits that multiplied by 2 gives a
* 4 digit number. */
for(i = 2; i < 9; i++)
{
for(j = 1234; j < 4987; j++)
{
p = i * j;
sprintf(num_s, "%d%d%d", i, j, p);
if(strlen(num_s) > 9)
{
break;
}
if(strlen(num_s) > 9)
{
break;
}
num = atoi(num_s);
num = atoi(num_s);
if(is_pandigital(num, 9))
{
*products[n] = p;
n++;
}
}
}
if(is_pandigital(num, 9))
{
*products[n] = p;
n++;
}
}
}
/* The outer loop starts at 12 because 10 has a 0 and 11 has two 1s, so
* the result can't be pandigital. The nested loop starts at 123 because
* it's the smallest 3-digit number with no digit repetitions and ends at
* 833, because 834*12 has 5 digits.*/
for(i = 12; i < 99; i++)
{
for(j = 123; j < 834; j++)
{
p = i * j;
sprintf(num_s, "%d%d%d", i, j, p);
/* The outer loop starts at 12 because 10 has a 0 and 11 has two 1s, so
* the result can't be pandigital. The nested loop starts at 123 because
* it's the smallest 3-digit number with no digit repetitions and ends at
* 833, because 834*12 has 5 digits.*/
for(i = 12; i < 99; i++)
{
for(j = 123; j < 834; j++)
{
p = i * j;
sprintf(num_s, "%d%d%d", i, j, p);
if(strlen(num_s) > 9)
{
break;
}
if(strlen(num_s) > 9)
{
break;
}
num = atoi(num_s);
num = atoi(num_s);
if(is_pandigital(num, 9))
{
*products[n] = p;
n++;
}
}
}
/* Sort the found products to easily see if there are duplicates.*/
insertion_sort((void **)products, 0, n-1, compare);
if(is_pandigital(num, 9))
{
*products[n] = p;
n++;
}
}
}
sum = *products[0];
/* Sort the found products to easily see if there are duplicates.*/
insertion_sort((void **)products, 0, n-1, compare);
for(i = 1; i < n; i++)
{
if(*products[i] != *products[i-1])
{
sum += *products[i];
}
}
sum = *products[0];
for(i = 0; i < 10; i++)
{
free(products[i]);
}
for(i = 1; i < n; i++)
{
if(*products[i] != *products[i-1])
{
sum += *products[i];
}
}
free(products);
for(i = 0; i < 10; i++)
{
free(products[i]);
}
clock_gettime(CLOCK_MONOTONIC, &end);
free(products);
elapsed = (end.tv_sec - start.tv_sec) + (double)(end.tv_nsec - start.tv_nsec) / 1000000000;
clock_gettime(CLOCK_MONOTONIC, &end);
printf("Project Euler, Problem 32\n");
printf("Answer: %d\n", sum);
elapsed = (end.tv_sec - start.tv_sec) + (double)(end.tv_nsec - start.tv_nsec) / 1000000000;
printf("Elapsed time: %.9lf seconds\n", elapsed);
printf("Project Euler, Problem 32\n");
printf("Answer: %d\n", sum);
return 0;
printf("Elapsed time: %.9lf seconds\n", elapsed);
return 0;
}
int compare(void *a, void *b)
{
int *n1, *n2;
int *n1, *n2;
n1 = (int *)a;
n2 = (int *)b;
n1 = (int *)a;
n2 = (int *)b;
return *n1 - *n2;
return *n1 - *n2;
}

View File

@ -8,6 +8,8 @@
*
* If the product of these four fractions is given in its lowest common terms, find the value of the denominator.*/
#define _POSIX_C_SOURCE 199309L
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
@ -15,47 +17,47 @@
int main(int argc, char **argv)
{
int i, j, n, d, prod_n = 1, prod_d = 1, div;
float f1, f2;
double elapsed;
struct timespec start, end;
int i, j, n, d, prod_n = 1, prod_d = 1, div;
float f1, f2;
double elapsed;
struct timespec start, end;
clock_gettime(CLOCK_MONOTONIC, &start);
clock_gettime(CLOCK_MONOTONIC, &start);
for(i = 11; i < 100; i++)
{
for(j = 11; j < 100; j++)
{
/* If the example is non-trivial, check if cancelling the digit that's equal
* in numerator and denominator gives the same fraction.*/
if(i % 10 && j % 10 && i != j && i % 10 == j / 10)
{
n = i / 10;
d = j % 10;
f1 = (float)i / j;
f2 = (float)n / d;
if(f1 == f2)
for(i = 11; i < 100; i++)
{
for(j = 11; j < 100; j++)
{
/* If the example is non-trivial, check if cancelling the digit that's equal
* in numerator and denominator gives the same fraction.*/
if(i % 10 && j % 10 && i != j && i % 10 == j / 10)
{
prod_n *= i;
prod_d *= j;
n = i / 10;
d = j % 10;
f1 = (float)i / j;
f2 = (float)n / d;
if(f1 == f2)
{
prod_n *= i;
prod_d *= j;
}
}
}
}
}
}
}
/* Find the greater common divisor of the fraction found.*/
div = gcd(prod_n, prod_d);
/* Find the greater common divisor of the fraction found.*/
div = gcd(prod_n, prod_d);
clock_gettime(CLOCK_MONOTONIC, &end);
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 33\n");
printf("Answer: %d\n", prod_d/div);
printf("Project Euler, Problem 33\n");
printf("Answer: %d\n", prod_d/div);
printf("Elapsed time: %.9lf seconds\n", elapsed);
printf("Elapsed time: %.9lf seconds\n", elapsed);
return 0;
return 0;
}

View File

@ -4,6 +4,8 @@
*
* Note: as 1! = 1 and 2! = 2 are not sums they are not included.*/
#define _POSIX_C_SOURCE 199309L
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
@ -11,66 +13,66 @@
int main(int argc, char **argv)
{
int i;
unsigned long int digit;
double elapsed;
struct timespec start, end;
mpz_t a, b, q, sum_f, sum, factorials[10];
int i;
unsigned long int digit;
double elapsed;
struct timespec start, end;
mpz_t a, b, q, sum_f, sum, factorials[10];
clock_gettime(CLOCK_MONOTONIC, &start);
clock_gettime(CLOCK_MONOTONIC, &start);
mpz_init_set_ui(a, 10);
mpz_init_set_ui(sum, 0);
mpz_inits(b, q, sum_f, sum, NULL);
mpz_init_set_ui(a, 10);
mpz_init_set_ui(sum, 0);
mpz_inits(b, q, sum_f, sum, NULL);
for(i = 0; i < 10; i++)
{
mpz_init_set_ui(factorials[i], 1);
}
for(i = 0; i < 10; i++)
{
mpz_init_set_ui(factorials[i], 1);
}
/* Pre-calculate factorials of each digit from 0 to 9.*/
for(i = 2; i < 10; i++)
{
mpz_fac_ui(factorials[i], i);
}
/* Pre-calculate factorials of each digit from 0 to 9.*/
for(i = 2; i < 10; i++)
{
mpz_fac_ui(factorials[i], i);
}
/* 9!*7<9999999, so 9999999 is certainly un upper bound.*/
while(mpz_cmp_ui(a, 9999999) < 0)
{
mpz_set(b, a);
mpz_set_ui(sum_f, 0);
/* 9!*7<9999999, so 9999999 is certainly un upper bound.*/
while(mpz_cmp_ui(a, 9999999) < 0)
{
mpz_set(b, a);
mpz_set_ui(sum_f, 0);
while(mpz_cmp_ui(b, 0))
{
digit = mpz_fdiv_qr_ui(b, q, b, 10);
mpz_add(sum_f, sum_f, factorials[digit]);
}
while(mpz_cmp_ui(b, 0))
{
digit = mpz_fdiv_qr_ui(b, q, b, 10);
mpz_add(sum_f, sum_f, factorials[digit]);
}
if(!mpz_cmp(a, sum_f))
{
mpz_add(sum, sum, a);
}
if(!mpz_cmp(a, sum_f))
{
mpz_add(sum, sum, a);
}
mpz_add_ui(a, a, 1);
}
mpz_add_ui(a, a, 1);
}
mpz_clears(a, b, q, sum_f, NULL);
mpz_clears(a, b, q, sum_f, NULL);
for(i = 0; i < 10; i++)
{
mpz_clear(factorials[i]);
}
for(i = 0; i < 10; i++)
{
mpz_clear(factorials[i]);
}
clock_gettime(CLOCK_MONOTONIC, &end);
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 34\n");
gmp_printf("Answer: %Zd\n", sum);
printf("Project Euler, Problem 34\n");
gmp_printf("Answer: %Zd\n", sum);
printf("Elapsed time: %.9lf seconds\n", elapsed);
printf("Elapsed time: %.9lf seconds\n", elapsed);
mpz_clear(sum);
mpz_clear(sum);
return 0;
return 0;
}

122
C/p035.c
View File

@ -4,6 +4,8 @@
*
* How many circular primes are there below one million?*/
#define _POSIX_C_SOURCE 199309L
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
@ -18,83 +20,83 @@ int *primes;
int main(int argc, char **argv)
{
int i, count = 13;
double elapsed;
struct timespec start, end;
int i, count = 13;
double elapsed;
struct timespec start, end;
clock_gettime(CLOCK_MONOTONIC, &start);
clock_gettime(CLOCK_MONOTONIC, &start);
/* Calculate all primes below one million, then check if they're circular.*/
if((primes = sieve(N)) == NULL)
{
fprintf(stderr, "Error! Sieve function returned NULL\n");
return 1;
}
/* Calculate all primes below one million, then check if they're circular.*/
if((primes = sieve(N)) == NULL)
{
fprintf(stderr, "Error! Sieve function returned NULL\n");
return 1;
}
/* Starting from 101 because we already know that there are 13 circular primes below 100.*/
for(i = 101; i < 1000000; i += 2)
{
if(is_circular_prime(i))
{
count++;
}
}
/* Starting from 101 because we already know that there are 13 circular primes below 100.*/
for(i = 101; i < 1000000; i += 2)
{
if(is_circular_prime(i))
{
count++;
}
}
free(primes);
free(primes);
clock_gettime(CLOCK_MONOTONIC, &end);
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 35\n");
printf("Answer: %d\n", count);
printf("Project Euler, Problem 35\n");
printf("Answer: %d\n", count);
printf("Elapsed time: %.9lf seconds\n", elapsed);
printf("Elapsed time: %.9lf seconds\n", elapsed);
return 0;
return 0;
}
int is_circular_prime(int n)
{
int i, tmp, count;
int i, tmp, count;
/* If n is not prime, it's obviously not a circular prime.*/
if(primes[n] == 0)
{
return 0;
}
/* If n is not prime, it's obviously not a circular prime.*/
if(primes[n] == 0)
{
return 0;
}
/* The primes below 10 are circular primes.*/
if(primes[n] == 1 && n < 10)
{
return 1;
}
/* The primes below 10 are circular primes.*/
if(primes[n] == 1 && n < 10)
{
return 1;
}
tmp = n;
count = 0;
tmp = n;
count = 0;
while(tmp > 0)
{
/* If the number has one or more even digits, it can't be a circular prime.
* because at least one of the rotations will be even.*/
if(tmp % 2 == 0)
{
return 0;
}
/* Count the number of digits.*/
count++;
tmp /= 10;
}
while(tmp > 0)
{
/* If the number has one or more even digits, it can't be a circular prime.
* because at least one of the rotations will be even.*/
if(tmp % 2 == 0)
{
return 0;
}
/* Count the number of digits.*/
count++;
tmp /= 10;
}
for(i = 1; i < count; i++)
{
/* Generate rotations and check if they're prime.*/
n = n % (int)pow(10, count-1) * 10 + n / (int)pow(10, count-1);
if(primes[n] == 0)
{
return 0;
}
}
for(i = 1; i < count; i++)
{
/* Generate rotations and check if they're prime.*/
n = n % (int)pow(10, count-1) * 10 + n / (int)pow(10, count-1);
if(primes[n] == 0)
{
return 0;
}
}
return 1;
return 1;
}

View File

@ -4,6 +4,8 @@
*
* (Please note that the palindromic number, in either base, may not include leading zeros.)*/
#define _POSIX_C_SOURCE 199309L
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
@ -13,31 +15,31 @@
int main(int argc, char **argv)
{
int i, sum = 0;
double elapsed;
struct timespec start, end;
int i, sum = 0;
double elapsed;
struct timespec start, end;
clock_gettime(CLOCK_MONOTONIC, &start);
clock_gettime(CLOCK_MONOTONIC, &start);
/* 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))
{
sum += i;
}
}
/* 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))
{
sum += i;
}
}
clock_gettime(CLOCK_MONOTONIC, &end);
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 36\n");
printf("Answer: %d\n", sum);
printf("Project Euler, Problem 36\n");
printf("Answer: %d\n", sum);
printf("Elapsed time: %.9lf seconds\n", elapsed);
printf("Elapsed time: %.9lf seconds\n", elapsed);
return 0;
return 0;
}

110
C/p037.c
View File

@ -5,6 +5,8 @@
*
* NOTE: 2, 3, 5, and 7 are not considered to be truncatable primes.*/
#define _POSIX_C_SOURCE 199309L
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
@ -15,75 +17,75 @@ int is_tr_prime(int n);
int main(int argc, char **argv)
{
int i = 0, n = 1, sum = 0;
double elapsed;
struct timespec start, end;
int i = 0, n = 1, sum = 0;
double elapsed;
struct timespec start, end;
clock_gettime(CLOCK_MONOTONIC, &start);
clock_gettime(CLOCK_MONOTONIC, &start);
/* Check every number until 11 truncatable primes are found.*/
while(i < 11)
{
if(is_tr_prime(n))
{
sum += n;
i++;
}
n++;
}
/* Check every number until 11 truncatable primes are found.*/
while(i < 11)
{
if(is_tr_prime(n))
{
sum += n;
i++;
}
n++;
}
clock_gettime(CLOCK_MONOTONIC, &end);
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 37\n");
printf("Answer: %d\n", sum);
printf("Project Euler, Problem 37\n");
printf("Answer: %d\n", sum);
printf("Elapsed time: %.9lf seconds\n", elapsed);
printf("Elapsed time: %.9lf seconds\n", elapsed);
return 0;
return 0;
}
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))
{
return 0;
}
/* 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;
/* 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)
{
if(!is_prime(tmp))
{
return 0;
}
tmp /= 10;
}
while(tmp > 0)
{
if(!is_prime(tmp))
{
return 0;
}
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;
/* 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;
while(tmp != n)
{
if(!is_prime(tmp))
{
return 0;
}
i *= 10;
tmp = n % i;
}
while(tmp != n)
{
if(!is_prime(tmp))
{
return 0;
}
i *= 10;
tmp = n % i;
}
/* If it gets here, the number is truncatable prime.*/
return 1;
/* If it gets here, the number is truncatable prime.*/
return 1;
}

106
C/p038.c
View File

@ -11,6 +11,8 @@
*
* 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?*/
#define _POSIX_C_SOURCE 199309L
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
@ -18,64 +20,64 @@
int main(int argc, char **argv)
{
int i, j, tmp;
long int n, max = 0;
double elapsed;
struct timespec start, end;
int i, j, tmp;
long int n, max = 0;
double elapsed;
struct timespec start, end;
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++)
{
n = 0;
j = 1;
do
{
tmp = i * j;
n += tmp;
j++;
/* 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;
j = 1;
do
{
tmp = i * j;
n += tmp;
j++;
if(n > max && is_pandigital(n, 9))
{
max = n;
}
if(i * j < 10)
{
n *= 10;
}
else if(i * j < 100)
{
n *= 100;
}
else if(i * j < 1000)
{
n *= 1000;
}
else if(i * j < 10000)
{
n *= 10000;
}
else if(i * j < 100000)
{
n *= 100000;
}
}while(n <= 987654321);
}
if(n > max && is_pandigital(n, 9))
{
max = n;
}
if(i * j < 10)
{
n *= 10;
}
else if(i * j < 100)
{
n *= 100;
}
else if(i * j < 1000)
{
n *= 1000;
}
else if(i * j < 10000)
{
n *= 10000;
}
else if(i * j < 100000)
{
n *= 100000;
}
} while(n <= 987654321);
}
clock_gettime(CLOCK_MONOTONIC, &end);
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 38\n");
printf("Answer: %ld\n", max);
printf("Project Euler, Problem 38\n");
printf("Answer: %ld\n", max);
printf("Elapsed time: %.9lf seconds\n", elapsed);
printf("Elapsed time: %.9lf seconds\n", elapsed);
return 0;
return 0;
}

134
C/p039.c
View File

@ -4,87 +4,89 @@
*
* For which value of p 1000, is the number of solutions maximised?*/
#define _POSIX_C_SOURCE 199309L
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, char **argv)
{
int p, m, n, a, b, c, count, max = 0, res = 0, tmpa, tmpb, tmpc, i;
int savedc[1000] = {0};
double elapsed;
struct timespec start, end;
int p, m, n, a, b, c, count, max = 0, res = 0, tmpa, tmpb, tmpc, i;
int savedc[1000] = {0};
double elapsed;
struct timespec start, end;
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++)
{
count = 0;
a = 0;
b = 0;
c = 0;
/* 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;
a = 0;
b = 0;
c = 0;
/* Generate pythagorean triplets.*/
for(m = 2; m * m < p; m++)
{
for(n = 1; n < m; n++)
{
a = m * m - n * n;
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])
/* Generate pythagorean triplets.*/
for(m = 2; m * m < p; m++)
{
for(n = 1; n < m; n++)
{
savedc[c] = 1;
count++;
a = m * m - n * n;
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;
count++;
}
i = 2;
tmpa = a;
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;
count++;
}
i++;
}
}
}
i = 2;
tmpa = a;
tmpb = b;
tmpc = c;
/* If the current value is greater than the maximum,
* save the new maximum and the value of p.*/
if(count > max)
{
max = count;
res = p;
}
}
/* 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;
clock_gettime(CLOCK_MONOTONIC, &end);
/* Increase counter if the new a, b and c give a perimeter=p.*/
if(tmpa + tmpb + tmpc == p && !savedc[tmpc])
{
savedc[tmpc] = 1;
count++;
}
elapsed = (end.tv_sec - start.tv_sec) + (double)(end.tv_nsec - start.tv_nsec) / 1000000000;
i++;
}
}
}
printf("Project Euler, Problem 39\n");
printf("Answer: %d\n", res);
/* If the current value is greater than the maximum,
* save the new maximum and the value of p.*/
if(count > max)
{
max = count;
res = p;
}
}
printf("Elapsed time: %.9lf seconds\n", elapsed);
clock_gettime(CLOCK_MONOTONIC, &end);
elapsed = (end.tv_sec - start.tv_sec) + (double)(end.tv_nsec - start.tv_nsec) / 1000000000;
printf("Project Euler, Problem 39\n");
printf("Answer: %d\n", res);
printf("Elapsed time: %.9lf seconds\n", elapsed);
return 0;
return 0;
}

136
C/p040.c
View File

@ -8,86 +8,88 @@
*
* d_1 × d_10 × d_100 × d_1000 × d_10000 × d_100000 × d_1000000*/
#define _POSIX_C_SOURCE 199309L
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, char **argv)
{
int i, value, n;
int digits[1000005];
double elapsed;
struct timespec start, end;
int i, value, n;
int digits[1000005];
double elapsed;
struct timespec start, end;
clock_gettime(CLOCK_MONOTONIC, &start);
clock_gettime(CLOCK_MONOTONIC, &start);
i = 1;
value = 1;
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)
{
digits[i-1] = value;
i++;
}
else if(value < 100)
{
digits[i-1] = value / 10;
digits[i] = value % 10;
i += 2;
}
else if(value < 1000)
{
digits[i-1] = value / 100;
digits[i] = (value / 10) % 10;
digits[i+1] = value % 10;
i += 3;
}
else if(value < 10000)
{
digits[i-1] = value / 1000;
digits[i] = (value / 100) % 10;
digits[i+1] = (value / 10) % 10;
digits[i+2] = value % 10;
i += 4;
}
else if(value < 100000)
{
digits[i-1] = value / 10000;
digits[i] = (value / 1000) % 10;
digits[i+1] = (value / 100) % 10;
digits[i+2] = (value / 10) % 10;
digits[i+3] = value % 10;
i += 5;
}
else if(value < 1000000)
{
digits[i-1] = value / 100000;
digits[i] = (value / 10000) % 10;
digits[i+1] = (value / 1000) % 10;
digits[i+2] = (value / 100) % 10;
digits[i+3] = (value / 10) % 10;
digits[i+4] = value % 10;
i += 6;
}
value++;
}
/* 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)
{
digits[i-1] = value;
i++;
}
else if(value < 100)
{
digits[i-1] = value / 10;
digits[i] = value % 10;
i += 2;
}
else if(value < 1000)
{
digits[i-1] = value / 100;
digits[i] = (value / 10) % 10;
digits[i+1] = value % 10;
i += 3;
}
else if(value < 10000)
{
digits[i-1] = value / 1000;
digits[i] = (value / 100) % 10;
digits[i+1] = (value / 10) % 10;
digits[i+2] = value % 10;
i += 4;
}
else if(value < 100000)
{
digits[i-1] = value / 10000;
digits[i] = (value / 1000) % 10;
digits[i+1] = (value / 100) % 10;
digits[i+2] = (value / 10) % 10;
digits[i+3] = value % 10;
i += 5;
}
else if(value < 1000000)
{
digits[i-1] = value / 100000;
digits[i] = (value / 10000) % 10;
digits[i+1] = (value / 1000) % 10;
digits[i+2] = (value / 100) % 10;
digits[i+3] = (value / 10) % 10;
digits[i+4] = value % 10;
i += 6;
}
value++;
}
/* Calculate the product.*/
n = digits[0] * digits[9] * digits[99] * digits[999] * digits[9999] * digits[99999] * digits[999999];
/* Calculate the product.*/
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);
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 40\n");
printf("Answer: %d\n", n);
printf("Project Euler, Problem 40\n");
printf("Answer: %d\n", n);
printf("Elapsed time: %.9lf seconds\n", elapsed);
printf("Elapsed time: %.9lf seconds\n", elapsed);
return 0;
return 0;
}

View File

@ -1,8 +1,10 @@
/* We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once. For example, 2143 is a 4-digit pandigital
/* We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once. For example, 2143 is a 4-digit pandigital
* and is also prime.
*
* What is the largest n-digit pandigital prime that exists?*/
#define _POSIX_C_SOURCE 199309L
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
@ -13,53 +15,53 @@ int count_digits(int n);
int main(int argc, char **argv)
{
/* 8- and 9-digit pandigital numbers can't be prime, because
* 1+2+...+8=36, which is divisible by 3, and 36+9=45 which is
* also divisible by 3, and therefore the whole number is divisible
* by 3. So we can start from the largest 7-digit pandigital number,
* until we find a prime.*/
int i = 7654321;
double elapsed;
struct timespec start, end;
/* 8- and 9-digit pandigital numbers can't be prime, because
* 1+2+...+8=36, which is divisible by 3, and 36+9=45 which is
* also divisible by 3, and therefore the whole number is divisible
* by 3. So we can start from the largest 7-digit pandigital number,
* until we find a prime.*/
int i = 7654321;
double elapsed;
struct timespec start, end;
clock_gettime(CLOCK_MONOTONIC, &start);
clock_gettime(CLOCK_MONOTONIC, &start);
while(i > 0)
{
if(is_pandigital(i, count_digits(i)) && is_prime(i))
{
break;
}
/*Skipping the even numbers.*/
i -= 2;
}
while(i > 0)
{
if(is_pandigital(i, count_digits(i)) && is_prime(i))
{
break;
}
/*Skipping the even numbers.*/
i -= 2;
}
clock_gettime(CLOCK_MONOTONIC, &end);
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 41\n");
printf("Answer: %d\n", i);
printf("Project Euler, Problem 41\n");
printf("Answer: %d\n", i);
printf("Elapsed time: %.9lf seconds\n", elapsed);
printf("Elapsed time: %.9lf seconds\n", elapsed);
return 0;
return 0;
}
int count_digits(int n)
{
int i = 0;
if(n == 0)
{
return 1;
}
int i = 0;
while(n > 0)
{
i++;
n /= 10;
}
if(n == 0)
{
return 1;
}
return i;
while(n > 0)
{
i++;
n /= 10;
}
return i;
}

132
C/p042.c
View File

@ -8,6 +8,8 @@
* Using words.txt (right click and 'Save Link/Target As...'), a 16K text file containing nearly two-thousand common English words,
* how many are triangle words?*/
#define _POSIX_C_SOURCE 199309L
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -17,93 +19,93 @@ int is_triang(int n);
int main(int argc, char **argv)
{
int i, j, n, len, value, count = 0;
char *line, **words;
double elapsed;
FILE *fp;
struct timespec start, end;
int i, j, n, len, value, count = 0;
char *line, **words;
double elapsed;
FILE *fp;
struct timespec start, end;
clock_gettime(CLOCK_MONOTONIC, &start);
clock_gettime(CLOCK_MONOTONIC, &start);
if((fp = fopen("words.txt", "r")) == NULL)
{
fprintf(stderr, "Error while opening file %s\n", "words.txt");
return 1;
}
if((fp = fopen("words.txt", "r")) == NULL)
{
fprintf(stderr, "Error while opening file %s\n", "words.txt");
return 1;
}
fscanf(fp, "%ms", &line);
fscanf(fp, "%ms", &line);
fclose(fp);
fclose(fp);
n = 1;
len = strlen(line);
n = 1;
len = strlen(line);
/* Count the words.*/
for(i = 0; i < len; i++)
{
if(line[i] == ',')
{
n++;
}
}
/* Count the words.*/
for(i = 0; i < len; i++)
{
if(line[i] == ',')
{
n++;
}
}
if((words = (char **)malloc(n*sizeof(char *))) == NULL)
{
fprintf(stderr, "Error while allocating memory\n");
return 1;
}
if((words = (char **)malloc(n*sizeof(char *))) == NULL)
{
fprintf(stderr, "Error while allocating memory\n");
return 1;
}
/* Save the words in an array of strings.*/
words[0] = strtok(line, ",\"");
/* Save the words in an array of strings.*/
words[0] = strtok(line, ",\"");
for(i = 1; i < n; i++)
{
words[i] = strtok(NULL, ",\"");
}
for(i = 1; i < n; i++)
{
words[i] = strtok(NULL, ",\"");
}
/* For each word, calculate its value and check if it's a triangle number.*/
for(i = 0; i < n; i++)
{
value = 0;
len = strlen(words[i]);
/* For each word, calculate its value and check if it's a triangle number.*/
for(i = 0; i < n; i++)
{
value = 0;
len = strlen(words[i]);
for(j = 0; j < len; j++)
{
value += (words[i][j] - 'A' + 1);
}
for(j = 0; j < len; j++)
{
value += (words[i][j] - 'A' + 1);
}
if(is_triang(value))
{
count++;
}
}
if(is_triang(value))
{
count++;
}
}
free(line);
free(words);
free(line);
free(words);
clock_gettime(CLOCK_MONOTONIC, &end);
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 42\n");
printf("Answer: %d\n", count);
printf("Project Euler, Problem 42\n");
printf("Answer: %d\n", count);
printf("Elapsed time: %.9lf seconds\n", elapsed);
printf("Elapsed time: %.9lf seconds\n", elapsed);
return 0;
return 0;
}
int is_triang(int n)
{
int i, j;
int i, j;
for(i = 1, j = 1; j <= n; i++, j += i)
{
if(n == j)
{
return 1;
}
}
for(i = 1, j = 1; j <= n; i++, j += i)
{
if(n == j)
{
return 1;
}
}
return 0;
return 0;
}

158
C/p043.c
View File

@ -13,6 +13,8 @@
*
* Find the sum of all 0 to 9 pandigital numbers with this property.*/
#define _POSIX_C_SOURCE 199309L
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
@ -23,116 +25,116 @@ int has_property(int **n);
int main(int argc, char **argv)
{
int i;
int **n;
long int sum = 0;
double elapsed;
struct timespec start, end;
int i;
int **n;
long int sum = 0;
double elapsed;
struct timespec start, end;
clock_gettime(CLOCK_MONOTONIC, &start);
clock_gettime(CLOCK_MONOTONIC, &start);
if((n = (int **)malloc(10*sizeof(int *))) == NULL)
{
fprintf(stderr, "Error while allocating memory\n");
return 1;
}
for(i = 0; i < 10; i++)
{
if((n[i] = (int *)malloc(sizeof(int))) == NULL)
{
fprintf(stderr, "Error while allocating memory\n");
return 1;
}
*n[i] = i;
}
if((n = (int **)malloc(10*sizeof(int *))) == NULL)
{
fprintf(stderr, "Error while allocating memory\n");
return 1;
}
/* Find the next permutation and check if it has the required property.
* Repeat until all the permutations have been found.*/
while(next_permutation((void **)n, 10, compare) != -1)
{
if(has_property(n))
{
sum += *n[0] * 1e9 + *n[1] * 1e8 + *n[2] * 1e7 + *n[3] * 1e6 + *n[4] * 1e5 +
*n[5] * 1e4 + *n[6] * 1e3 + *n[7] * 1e2 + *n[8] * 1e1 + *n[9];
}
}
for(i = 0; i < 10; i++)
{
if((n[i] = (int *)malloc(sizeof(int))) == NULL)
{
fprintf(stderr, "Error while allocating memory\n");
return 1;
}
*n[i] = i;
}
clock_gettime(CLOCK_MONOTONIC, &end);
/* Find the next permutation and check if it has the required property.
* Repeat until all the permutations have been found.*/
while(next_permutation((void **)n, 10, compare) != -1)
{
if(has_property(n))
{
sum += *n[0] * 1e9 + *n[1] * 1e8 + *n[2] * 1e7 + *n[3] * 1e6 + *n[4] * 1e5 +
*n[5] * 1e4 + *n[6] * 1e3 + *n[7] * 1e2 + *n[8] * 1e1 + *n[9];
}
}
elapsed = (end.tv_sec - start.tv_sec) + (double)(end.tv_nsec - start.tv_nsec) / 1000000000;
clock_gettime(CLOCK_MONOTONIC, &end);
printf("Project Euler, Problem 43\n");
printf("Answer: %ld\n", sum);
elapsed = (end.tv_sec - start.tv_sec) + (double)(end.tv_nsec - start.tv_nsec) / 1000000000;
printf("Elapsed time: %.9lf seconds\n", elapsed);
printf("Project Euler, Problem 43\n");
printf("Answer: %ld\n", sum);
return 0;
printf("Elapsed time: %.9lf seconds\n", elapsed);
return 0;
}
int compare(void *a, void *b)
{
int *n1, *n2;
int *n1, *n2;
n1 = (int *)a;
n2 = (int *)b;
n1 = (int *)a;
n2 = (int *)b;
return *n1 - *n2;
return *n1 - *n2;
}
/* Function to check if the value has the desired property.*/
int has_property(int **n)
{
long int value;
long int value;
value = *n[1] * 100 + *n[2] * 10 + *n[3];
value = *n[1] * 100 + *n[2] * 10 + *n[3];
if(value % 2 != 0)
{
return 0;
}
if(value % 2 != 0)
{
return 0;
}
value = *n[2] * 100 + *n[3] * 10 + *n[4];
value = *n[2] * 100 + *n[3] * 10 + *n[4];
if(value % 3 != 0)
{
return 0;
}
if(value % 3 != 0)
{
return 0;
}
value = *n[3] * 100 + *n[4] * 10 + *n[5];
value = *n[3] * 100 + *n[4] * 10 + *n[5];
if(value % 5 != 0)
{
return 0;
}
if(value % 5 != 0)
{
return 0;
}
value = *n[4] * 100 + *n[5] * 10 + *n[6];
value = *n[4] * 100 + *n[5] * 10 + *n[6];
if(value % 7 != 0)
{
return 0;
}
if(value % 7 != 0)
{
return 0;
}
value = *n[5] * 100 + *n[6] * 10 + *n[7];
value = *n[5] * 100 + *n[6] * 10 + *n[7];
if(value % 11 != 0)
{
return 0;
}
if(value % 11 != 0)
{
return 0;
}
value = *n[6] * 100 + *n[7] * 10 + *n[8];
value = *n[6] * 100 + *n[7] * 10 + *n[8];
if(value %13 != 0)
{
return 0;
}
if(value %13 != 0)
{
return 0;
}
value = *n[7] * 100 + *n[8] * 10 + *n[9];
value = *n[7] * 100 + *n[8] * 10 + *n[9];
if(value % 17 != 0)
{
return 0;
}
if(value % 17 != 0)
{
return 0;
}
return 1;
return 1;
}

View File

@ -7,6 +7,8 @@
* Find the pair of pentagonal numbers, Pj and Pk, for which their sum and difference are pentagonal and D = |Pk Pj| is minimised;
* what is the value of D?*/
#define _POSIX_C_SOURCE 199309L
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
@ -15,42 +17,42 @@
int main(int argc, char **argv)
{
int n, m, pn, pm, found = 0;
double elapsed;
struct timespec start, end;
int n, m, pn, pm, found = 0;
double elapsed;
struct timespec start, end;
clock_gettime(CLOCK_MONOTONIC, &start);
clock_gettime(CLOCK_MONOTONIC, &start);
n = 2;
n = 2;
/* Check all couples of pentagonal numbers until the right one
* is found. Use the function implemented in projecteuler.c to
* check if the sum and difference ot the two numbers is pentagonal.*/
while(!found)
{
pn = n * (3 * n - 1) / 2;
/* Check all couples of pentagonal numbers until the right one
* is found. Use the function implemented in projecteuler.c to
* check if the sum and difference ot the two numbers is pentagonal.*/
while(!found)
{
pn = n * (3 * n - 1) / 2;
for(m = 1; m < n; m++)
{
pm = m * (3 * m - 1) / 2;
for(m = 1; m < n; m++)
{
pm = m * (3 * m - 1) / 2;
if(is_pentagonal(pn+pm) && is_pentagonal(pn-pm))
{
found = 1;
break;
}
}
n++;
}
if(is_pentagonal(pn+pm) && is_pentagonal(pn-pm))
{
found = 1;
break;
}
}
n++;
}
clock_gettime(CLOCK_MONOTONIC, &end);
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 44\n");
printf("Answer: %d\n", pn-pm);
printf("Project Euler, Problem 44\n");
printf("Answer: %d\n", pn-pm);
printf("Elapsed time: %.9lf seconds\n", elapsed);
printf("Elapsed time: %.9lf seconds\n", elapsed);
return 0;
return 0;
}

View File

@ -8,6 +8,8 @@
*
* Find the next triangle number that is also pentagonal and hexagonal.*/
#define _POSIX_C_SOURCE 199309L
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
@ -16,37 +18,37 @@
int main(int argc, char **argv)
{
int found = 0;
long int i, n;
double elapsed;
struct timespec start, end;
int found = 0;
long int i, n;
double elapsed;
struct timespec start, end;
clock_gettime(CLOCK_MONOTONIC, &start);
clock_gettime(CLOCK_MONOTONIC, &start);
i = 143;
i = 143;
while(!found)
{
i++;
/* Hexagonal numbers are also triangle numbers, so it's sufficient
* to generate hexagonal numbers (starting from H_144) and check if
* they're also pentagonal.*/
n = i * (2 * i - 1);
if(is_pentagonal(n))
{
found = 1;
}
}
while(!found)
{
i++;
/* Hexagonal numbers are also triangle numbers, so it's sufficient
* to generate hexagonal numbers (starting from H_144) and check if
* they're also pentagonal.*/
n = i * (2 * i - 1);
clock_gettime(CLOCK_MONOTONIC, &end);
if(is_pentagonal(n))
{
found = 1;
}
}
elapsed = (end.tv_sec - start.tv_sec) + (double)(end.tv_nsec - start.tv_nsec) / 1000000000;
clock_gettime(CLOCK_MONOTONIC, &end);
printf("Project Euler, Problem 45\n");
printf("Answer: %ld\n", n);
elapsed = (end.tv_sec - start.tv_sec) + (double)(end.tv_nsec - start.tv_nsec) / 1000000000;
printf("Elapsed time: %.9lf seconds\n", elapsed);
printf("Project Euler, Problem 45\n");
printf("Answer: %ld\n", n);
return 0;
printf("Elapsed time: %.9lf seconds\n", elapsed);
return 0;
}

106
C/p046.c
View File

@ -11,6 +11,8 @@
*
* What is the smallest odd composite that cannot be written as the sum of a prime and twice a square?*/
#define _POSIX_C_SOURCE 199309L
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
@ -25,73 +27,73 @@ int *primes;
int main(int argc, char **argv)
{
int i, found = 0;
double elapsed;
struct timespec start, end;
int i, found = 0;
double elapsed;
struct timespec start, end;
clock_gettime(CLOCK_MONOTONIC, &start);
clock_gettime(CLOCK_MONOTONIC, &start);
if((primes = sieve(N)) == NULL)
{
fprintf(stderr, "Error! Sieve function returned NULL\n");
return 1;
}
if((primes = sieve(N)) == NULL)
{
fprintf(stderr, "Error! Sieve function returned NULL\n");
return 1;
}
/* For every odd number, check if it's prime, if it is check
* if it satisfies the Goldbach property. Continue until the
* first number that doesn't is found.*/
for(i = 3; !found && i < N; i += 2)
{
if(!primes[i])
{
if(!goldbach(i))
{
found = 1;
}
}
}
/* For every odd number, check if it's prime, if it is check
* if it satisfies the Goldbach property. Continue until the
* first number that doesn't is found.*/
for(i = 3; !found && i < N; i += 2)
{
if(!primes[i])
{
if(!goldbach(i))
{
found = 1;
}
}
}
free(primes);
clock_gettime(CLOCK_MONOTONIC, &end);
free(primes);
elapsed = (end.tv_sec - start.tv_sec) + (double)(end.tv_nsec - start.tv_nsec) / 1000000000;
clock_gettime(CLOCK_MONOTONIC, &end);
printf("Project Euler, Problem 46\n");
printf("Answer: %d\n", i-2);
elapsed = (end.tv_sec - start.tv_sec) + (double)(end.tv_nsec - start.tv_nsec) / 1000000000;
printf("Elapsed time: %.9lf seconds\n", elapsed);
printf("Project Euler, Problem 46\n");
printf("Answer: %d\n", i-2);
return 0;
printf("Elapsed time: %.9lf seconds\n", elapsed);
return 0;
}
int goldbach(int n)
{
int i, j, tmp;
int i, j, tmp;
/* Check every prime smaller than n.*/
for(i = 3; i < n; i += 2)
{
if(primes[i])
{
j = 1;
/* Check every prime smaller than n.*/
for(i = 3; i < n; i += 2)
{
if(primes[i])
{
j = 1;
/* Check if summing twice a square to the prime number
* gives n. Return 1 when succeeding.*/
do
{
tmp = i + 2 * j * j;
if(tmp == n)
/* Check if summing twice a square to the prime number
* gives n. Return 1 when succeeding.*/
do
{
return 1;
}
tmp = i + 2 * j * j;
j++;
}while(tmp < n);
}
}
if(tmp == n)
{
return 1;
}
/* Return 0 if no solution is found.*/
return 0;
j++;
} while(tmp < n);
}
}
/* Return 0 if no solution is found.*/
return 0;
}

104
C/p047.c
View File

@ -11,6 +11,8 @@
*
* Find the first four consecutive integers to have four distinct prime factors each. What is the first of these numbers?*/
#define _POSIX_C_SOURCE 199309L
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
@ -22,74 +24,74 @@ int *count_factors(int n);
int main(int argc, char **argv)
{
int i, count, res;
int *factors;
double elapsed;
struct timespec start, end;
int i, count, res;
int *factors;
double elapsed;
struct timespec start, end;
clock_gettime(CLOCK_MONOTONIC, &start);
clock_gettime(CLOCK_MONOTONIC, &start);
if((factors = count_factors(N)) == NULL)
{
fprintf(stderr, "Error! Count_factors function returned NULL\n");
return 1;
}
if((factors = count_factors(N)) == NULL)
{
fprintf(stderr, "Error! Count_factors function returned NULL\n");
return 1;
}
count = 0;
count = 0;
for(i = 0; i < N; i++)
{
if(factors[i] == 4)
{
count++;
}
else
{
count = 0;
}
for(i = 0; i < N; i++)
{
if(factors[i] == 4)
{
count++;
}
else
{
count = 0;
}
if(count == 4)
{
res = i - 3;
break;
}
}
if(count == 4)
{
res = i - 3;
break;
}
}
clock_gettime(CLOCK_MONOTONIC, &end);
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 47\n");
printf("Answer: %d\n", res);
printf("Project Euler, Problem 47\n");
printf("Answer: %d\n", res);
printf("Elapsed time: %.9lf seconds\n", elapsed);
printf("Elapsed time: %.9lf seconds\n", elapsed);
return 0;
return 0;
}
/* Function using a modified sieve of Eratosthenes to count
* the distinct prime factors of each number.*/
int *count_factors(int n)
{
int i = 2, j;
int *factors;
int i = 2, j;
int *factors;
if((factors = (int *)calloc(n, sizeof(int))) == NULL)
{
return NULL;
}
if((factors = (int *)calloc(n, sizeof(int))) == NULL)
{
return NULL;
}
while(i < n / 2)
{
if(factors[i] == 0)
{
for(j = i; j < n; j += i)
{
factors[j]++;
}
}
i++;
}
while(i < n / 2)
{
if(factors[i] == 0)
{
for(j = i; j < n; j += i)
{
factors[j]++;
}
}
i++;
}
return factors;
return factors;
}

View File

@ -8,38 +8,40 @@
#include <time.h>
#include <gmp.h>
#define _POSIX_C_SOURCE 199309L
int main(int argc, char **argv)
{
int i;
double elapsed;
struct timespec start, end;
mpz_t power, sum;
char *res;
int i;
double elapsed;
struct timespec start, end;
mpz_t power, sum;
char *res;
clock_gettime(CLOCK_MONOTONIC, &start);
clock_gettime(CLOCK_MONOTONIC, &start);
mpz_init_set_ui(sum, 0);
mpz_init(power);
mpz_init_set_ui(sum, 0);
mpz_init(power);
/* Simply calculate the sum of the powers using the GMP Library.*/
for(i = 1; i <= 1000; i++)
{
mpz_ui_pow_ui(power, i, i);
mpz_add(sum, sum, power);
}
/* Simply calculate the sum of the powers using the GMP Library.*/
for(i = 1; i <= 1000; i++)
{
mpz_ui_pow_ui(power, i, i);
mpz_add(sum, sum, power);
}
res = mpz_get_str(NULL, 10, sum);
mpz_clears(power, sum, NULL);
res = mpz_get_str(NULL, 10, sum);
clock_gettime(CLOCK_MONOTONIC, &end);
mpz_clears(power, sum, NULL);
elapsed = (end.tv_sec - start.tv_sec) + (double)(end.tv_nsec - start.tv_nsec) / 1000000000;
clock_gettime(CLOCK_MONOTONIC, &end);
printf("Project Euler, Problem 48\n");
printf("Answer: %s\n", res+strlen(res)-10);
elapsed = (end.tv_sec - start.tv_sec) + (double)(end.tv_nsec - start.tv_nsec) / 1000000000;
printf("Elapsed time: %.9lf seconds\n", elapsed);
printf("Project Euler, Problem 48\n");
printf("Answer: %s\n", res+strlen(res)-10);
return 0;
printf("Elapsed time: %.9lf seconds\n", elapsed);
return 0;
}

128
C/p049.c
View File

@ -6,6 +6,8 @@
*
* What 12-digit number do you form by concatenating the three terms in this sequence?*/
#define _POSIX_C_SOURCE 199309L
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
@ -20,86 +22,86 @@ int *primes;
int main(int argc, char **argv)
{
int i = 1489, j, found = 0;
double elapsed;
struct timespec start, end;
int i = 1489, j, found = 0;
double elapsed;
struct timespec start, end;
clock_gettime(CLOCK_MONOTONIC, &start);
clock_gettime(CLOCK_MONOTONIC, &start);
if((primes = sieve(N)) == NULL)
{
fprintf(stderr, "Error! Sieve function returned NULL\n");
return 1;
}
if((primes = sieve(N)) == NULL)
{
fprintf(stderr, "Error! Sieve function returned NULL\n");
return 1;
}
/* Starting from i=1489 (bigger than the first number in the sequence given in the problem),
* check odd numbers. If they're prime, loop on even numbers j (odd+even=odd, odd+odd=even and
* we need odd numbers because we're looking for primes) up to 4254 (1489+2*4256=10001 which has
* 5 digits.*/
while(i < N)
{
if(primes[i])
{
for(j = 2; j < 4255; j += 2)
{
/* If i, i+j and i+2*j are all primes and they have
* all the same digits, the result has been found.*/
if(i + 2 * j < N && primes[i+j] && primes[i+2*j] &&
is_permutation(i, i+j) && is_permutation(i, i+2*j))
/* Starting from i=1489 (bigger than the first number in the sequence given in the problem),
* check odd numbers. If they're prime, loop on even numbers j (odd+even=odd, odd+odd=even and
* we need odd numbers because we're looking for primes) up to 4254 (1489+2*4256=10001 which has
* 5 digits.*/
while(i < N)
{
if(primes[i])
{
for(j = 2; j < 4255; j += 2)
{
found = 1;
break;
/* If i, i+j and i+2*j are all primes and they have
* all the same digits, the result has been found.*/
if(i + 2 * j < N && primes[i+j] && primes[i+2*j] &&
is_permutation(i, i+j) && is_permutation(i, i+2*j))
{
found = 1;
break;
}
}
}
}
if(found)
{
break;
}
i += 2;
}
}
if(found)
{
break;
}
i += 2;
}
free(primes);
free(primes);
clock_gettime(CLOCK_MONOTONIC, &end);
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 49\n");
printf("Answer: %d%d%d\n", i, i+j, i+2*j);
printf("Project Euler, Problem 49\n");
printf("Answer: %d%d%d\n", i, i+j, i+2*j);
printf("Elapsed time: %.9lf seconds\n", elapsed);
printf("Elapsed time: %.9lf seconds\n", elapsed);
return 0;
return 0;
}
int is_permutation(int a, int b)
{
int i;
int digits1[10] = {0}, digits2[10] = {0};
int i;
int digits1[10] = {0}, digits2[10] = {0};
/* Get digits of a.*/
while(a > 0)
{
digits1[a%10]++;
a /= 10;
}
/* Get digits of a.*/
while(a > 0)
{
digits1[a%10]++;
a /= 10;
}
/* Get digits of b.*/
while(b > 0)
{
digits2[b%10]++;
b /= 10;
}
/* Get digits of b.*/
while(b > 0)
{
digits2[b%10]++;
b /= 10;
}
/* If they're not the same, return 0.*/
for(i = 0; i < 10; i++)
{
if(digits1[i] != digits2[i])
{
return 0;
}
}
/* If they're not the same, return 0.*/
for(i = 0; i < 10; i++)
{
if(digits1[i] != digits2[i])
{
return 0;
}
}
return 1;
return 1;
}

124
C/p050.c
View File

@ -8,6 +8,8 @@
*
* Which prime, below one-million, can be written as the sum of the most consecutive primes?*/
#define _POSIX_C_SOURCE 199309L
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
@ -18,78 +20,78 @@
int main(int argc, char **argv)
{
int i, j, max = 0, max_p = 0, sum, count;
int *primes;
double elapsed;
struct timespec start, end;
int i, j, max = 0, max_p = 0, sum, count;
int *primes;
double elapsed;
struct timespec start, end;
clock_gettime(CLOCK_MONOTONIC, &start);
clock_gettime(CLOCK_MONOTONIC, &start);
if((primes = sieve(N)) == NULL)
{
fprintf(stderr, "Error! Sieve function returned NULL\n");
return 1;
}
if((primes = sieve(N)) == NULL)
{
fprintf(stderr, "Error! Sieve function returned NULL\n");
return 1;
}
/* Starting from a prime i, add consecutive primes until the
* sum exceeds the limit, every time the sum is also a prime
* save the value and the count if the count is larger than the
* current maximum. Repeat for all primes below N.
* A separate loop is used for i=2, so later only odd numbers are
* checked for primality.*/
i = 2;
count = 1;
sum = i;
/* Starting from a prime i, add consecutive primes until the
* sum exceeds the limit, every time the sum is also a prime
* save the value and the count if the count is larger than the
* current maximum. Repeat for all primes below N.
* A separate loop is used for i=2, so later only odd numbers are
* checked for primality.*/
i = 2;
count = 1;
sum = i;
for(j = i + 1; j < N && sum < N; j++)
{
if(primes[j])
{
sum += j;
count++;
for(j = i + 1; j < N && sum < N; j++)
{
if(primes[j])
{
sum += j;
count++;
if(sum < N && primes[sum] && count > max)
{
max = count;
max_p = sum;
}
}
}
for(i = 3; i < N; i += 2)
{
if(primes[i])
{
count = 1;
sum = i;
for(j = i + 2; j < N && sum < N; j += 2)
{
if(primes[j])
if(sum < N && primes[sum] && count > max)
{
sum += j;
count++;
if(sum < N && primes[sum] && count > max)
{
max = count;
max_p = sum;
}
max = count;
max_p = sum;
}
}
}
}
}
}
free(primes);
for(i = 3; i < N; i += 2)
{
if(primes[i])
{
count = 1;
sum = i;
clock_gettime(CLOCK_MONOTONIC, &end);
for(j = i + 2; j < N && sum < N; j += 2)
{
if(primes[j])
{
sum += j;
count++;
elapsed = (end.tv_sec - start.tv_sec) + (double)(end.tv_nsec - start.tv_nsec) / 1000000000;
if(sum < N && primes[sum] && count > max)
{
max = count;
max_p = sum;
}
}
}
}
}
printf("Project Euler, Problem 50\n");
printf("Answer: %d\n", max_p);
free(primes);
printf("Elapsed time: %.9lf seconds\n", elapsed);
clock_gettime(CLOCK_MONOTONIC, &end);
return 0;
elapsed = (end.tv_sec - start.tv_sec) + (double)(end.tv_nsec - start.tv_nsec) / 1000000000;
printf("Project Euler, Problem 50\n");
printf("Answer: %d\n", max_p);
printf("Elapsed time: %.9lf seconds\n", elapsed);
return 0;
}

178
C/p051.c
View File

@ -7,6 +7,8 @@
* Find the smallest prime which, by replacing part of the number (not necessarily adjacent digits) with the same digit, is part of an eight prime
* value family.*/
#define _POSIX_C_SOURCE 199309L
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -23,119 +25,119 @@ int *primes;
int main(int argc, char **argv)
{
int i;
double elapsed;
struct timespec start, end;
int i;
double elapsed;
struct timespec start, end;
clock_gettime(CLOCK_MONOTONIC, &start);
clock_gettime(CLOCK_MONOTONIC, &start);
/* N set to 1000000 as a reasonable limit, which turns out to be enough.*/
if((primes = sieve(N)) == NULL)
{
fprintf(stderr, "Error! Sieve function returned NULL\n");
return 1;
}
/* N set to 1000000 as a reasonable limit, which turns out to be enough.*/
if((primes = sieve(N)) == NULL)
{
fprintf(stderr, "Error! Sieve function returned NULL\n");
return 1;
}
/* Checking only odd numbers with at least 4 digits.*/
for(i = 1001; i < N; i += 2)
{
/* The family of numbers needs to have at least one of 0, 1 or 2 as
* repeated digits, otherwise we can't get a 8 number family (there
* are only 7 other digits). Also, the number of repeated digits must
* be 3, otherwise at least 3 resulting numbers will be divisible by 3.
* So the smallest number of this family must have three 0s, three 1s or
* three 2s.*/
if(count_digit(i, 0) >= 3 || count_digit(i, 1) >= 3 ||
count_digit(i, 2) >= 3)
{
/* If i is prime, try replacing digits, if obtaining 8 primes, then
* this is the result.*/
if(primes[i] && replace(i) == 8)
{
break;
}
}
}
/* Checking only odd numbers with at least 4 digits.*/
for(i = 1001; i < N; i += 2)
{
/* The family of numbers needs to have at least one of 0, 1 or 2 as
* repeated digits, otherwise we can't get a 8 number family (there
* are only 7 other digits). Also, the number of repeated digits must
* be 3, otherwise at least 3 resulting numbers will be divisible by 3.
* So the smallest number of this family must have three 0s, three 1s or
* three 2s.*/
if(count_digit(i, 0) >= 3 || count_digit(i, 1) >= 3 ||
count_digit(i, 2) >= 3)
{
/* If i is prime, try replacing digits, if obtaining 8 primes, then
* this is the result.*/
if(primes[i] && replace(i) == 8)
{
break;
}
}
}
free(primes);
free(primes);
clock_gettime(CLOCK_MONOTONIC, &end);
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 51\n");
printf("Answer: %d\n", i);
printf("Project Euler, Problem 51\n");
printf("Answer: %d\n", i);
printf("Elapsed time: %.9lf seconds\n", elapsed);
printf("Elapsed time: %.9lf seconds\n", elapsed);
return 0;
return 0;
}
int count_digit(int n, int d)
{
int count = 0;
int count = 0;
while(n > 0)
{
if(n % 10 == d)
{
count++;
}
n /= 10;
}
while(n > 0)
{
if(n % 10 == d)
{
count++;
}
n /= 10;
}
return count;
return count;
}
int replace(int n)
{
int i, j, k, w, l, count, max = 0, s_to_n;
char n_to_s[7];
int i, j, k, w, l, count, max = 0, s_to_n;
char n_to_s[7];
sprintf(n_to_s, "%d", n);
l = strlen(n_to_s);
sprintf(n_to_s, "%d", n);
l = strlen(n_to_s);
for(i = 0; i < l - 3; i++)
{
for(j = i + 1; j < l - 2; j++)
{
/* Replacing the last digit can't give 8 primes, because at least
* six of the numbers obtained will be divisible by 2 and/or 5.*/
for(k = j + 1; k < l - 1; k++)
{
count = 0;
for(w = 0; w < 10; w++)
for(i = 0; i < l - 3; i++)
{
for(j = i + 1; j < l - 2; j++)
{
/* Replacing the last digit can't give 8 primes, because at least
* six of the numbers obtained will be divisible by 2 and/or 5.*/
for(k = j + 1; k < l - 1; k++)
{
if(i == 0 && w == 0)
{
continue;
}
count = 0;
sprintf(n_to_s, "%d", n);
n_to_s[i] = w + '0';
n_to_s[j] = w + '0';
n_to_s[k] = w + '0';
s_to_n = atoi(n_to_s);
for(w = 0; w < 10; w++)
{
if(i == 0 && w == 0)
{
continue;
}
if(primes[s_to_n])
{
if(count == 0 && s_to_n != n)
{
continue;
}
sprintf(n_to_s, "%d", n);
n_to_s[i] = w + '0';
n_to_s[j] = w + '0';
n_to_s[k] = w + '0';
s_to_n = atoi(n_to_s);
count++;
}
if(primes[s_to_n])
{
if(count == 0 && s_to_n != n)
{
continue;
}
count++;
}
}
if(count > max)
{
max = count;
}
}
}
}
if(count > max)
{
max = count;
}
}
}
}
return max;
return max;
}

View File

@ -2,6 +2,8 @@
*
* Find the smallest positive integer, x, such that 2x, 3x, 4x, 5x, and 6x, contain the same digits.*/
#define _POSIX_C_SOURCE 199309L
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
@ -10,65 +12,65 @@ int is_permutation(int a, int b);
int main(int argc, char **argv)
{
int i;
double elapsed;
struct timespec start, end;
int i;
double elapsed;
struct timespec start, end;
clock_gettime(CLOCK_MONOTONIC, &start);
clock_gettime(CLOCK_MONOTONIC, &start);
i = 1;
i = 1;
/* Brute force approach, try every integer number until the desired one is found.*/
while(1)
{
if(is_permutation(i, 2*i) && is_permutation(i, 3*i) && is_permutation(i, 4*i) &&
is_permutation(i, 5*i) && is_permutation(i, 6*i))
{
break;
}
/* Brute force approach, try every integer number until the desired one is found.*/
while(1)
{
if(is_permutation(i, 2*i) && is_permutation(i, 3*i) && is_permutation(i, 4*i) &&
is_permutation(i, 5*i) && is_permutation(i, 6*i))
{
break;
}
i++;
}
i++;
}
clock_gettime(CLOCK_MONOTONIC, &end);
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 52\n");
printf("Answer: %d\n", i);
printf("Project Euler, Problem 52\n");
printf("Answer: %d\n", i);
printf("Elapsed time: %.9lf seconds\n", elapsed);
printf("Elapsed time: %.9lf seconds\n", elapsed);
return 0;
return 0;
}
int is_permutation(int a, int b)
{
int i;
int digits1[10] = {0}, digits2[10] = {0};
int i;
int digits1[10] = {0}, digits2[10] = {0};
/* Get digits of a.*/
while(a > 0)
{
digits1[a%10]++;
a /= 10;
}
/* Get digits of a.*/
while(a > 0)
{
digits1[a%10]++;
a /= 10;
}
/* Get digits of b.*/
while(b > 0)
{
digits2[b%10]++;
b /= 10;
}
/* Get digits of b.*/
while(b > 0)
{
digits2[b%10]++;
b /= 10;
}
/* If they're not the same, return 0.*/
for(i = 0; i < 10; i++)
{
if(digits1[i] != digits2[i])
{
return 0;
}
}
/* If they're not the same, return 0.*/
for(i = 0; i < 10; i++)
{
if(digits1[i] != digits2[i])
{
return 0;
}
}
return 1;
return 1;
}

View File

@ -10,6 +10,8 @@
*
* How many, not necessarily distinct, values of (n r) for 1n100, are greater than one-million?*/
#define _POSIX_C_SOURCE 199309L
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
@ -22,63 +24,63 @@ void binomial(mpz_t bin, unsigned long int n, unsigned long int k, mpz_t *factor
int main(int argc, char **argv)
{
int i, j, count = 0;
double elapsed;
struct timespec start, end;
mpz_t bin;
mpz_t factorials[N+1];
int i, j, count = 0;
double elapsed;
struct timespec start, end;
mpz_t bin;
mpz_t factorials[N+1];
clock_gettime(CLOCK_MONOTONIC, &start);
clock_gettime(CLOCK_MONOTONIC, &start);
mpz_init(bin);
mpz_init(bin);
mpz_init_set_ui(factorials[0], 1);
mpz_init_set_ui(factorials[0], 1);
/* Straightforward brute force approach, using the GMP Library. First
* calculate all factorials, then use them for the binomial coefficients.*/
for(i = 1; i <= N; i++)
{
mpz_init(factorials[i]);
mpz_fac_ui(factorials[i], i);
}
/* Straightforward brute force approach, using the GMP Library. First
* calculate all factorials, then use them for the binomial coefficients.*/
for(i = 1; i <= N; i++)
{
mpz_init(factorials[i]);
mpz_fac_ui(factorials[i], i);
}
for(i = 23; i <= N; i++)
{
for(j = 1; j <= i; j++)
{
binomial(bin, i, j, factorials);
for(i = 23; i <= N; i++)
{
for(j = 1; j <= i; j++)
{
binomial(bin, i, j, factorials);
if(mpz_cmp_ui(bin, LIMIT) > 0)
count++;
}
}
if(mpz_cmp_ui(bin, LIMIT) > 0)
count++;
}
}
mpz_clear(bin);
mpz_clear(bin);
clock_gettime(CLOCK_MONOTONIC, &end);
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 53\n");
printf("Answer: %d\n", count);
printf("Project Euler, Problem 53\n");
printf("Answer: %d\n", count);
printf("Elapsed time: %.9lf seconds\n", elapsed);
printf("Elapsed time: %.9lf seconds\n", elapsed);
return 0;
return 0;
}
void binomial(mpz_t bin, unsigned long n, unsigned long int k, mpz_t *factorials)
{
mpz_t num, d1, d2;
mpz_t num, d1, d2;
mpz_inits(num, d1, d2, NULL);
mpz_inits(num, d1, d2, NULL);
mpz_set(num, factorials[n]);
mpz_set(d1, factorials[k]);
mpz_set(d2, factorials[n-k]);
mpz_set(num, factorials[n]);
mpz_set(d1, factorials[k]);
mpz_set(d2, factorials[n-k]);
mpz_mul(d1, d1, d2);
mpz_tdiv_q(bin, num, d1);
mpz_mul(d1, d1, d2);
mpz_tdiv_q(bin, num, d1);
mpz_clears(num, d1, d2, NULL);
mpz_clears(num, d1, d2, NULL);
}

1118
C/p054.c

File diff suppressed because it is too large Load Diff

102
C/p055.c
View File

@ -9,8 +9,8 @@
* That is, 349 took three iterations to arrive at a palindrome.
*
* Although no one has proved it yet, it is thought that some numbers, like 196, never produce a palindrome. A number that never forms a palindrome
* through the reverse and add process is called a Lychrel number. Due to the theoretical nature of these numbers, and for the purpose of this problem,
* we shall assume that a number is Lychrel until proven otherwise. In addition you are given that for every number below ten-thousand, it will either
* through the reverse and add process is called a Lychrel number. Due to the theoretical nature of these numbers, and for the purpose of this problem,
* we shall assume that a number is Lychrel until proven otherwise. In addition you are given that for every number below ten-thousand, it will either
* (i) become a palindrome in less than fifty iterations, or,
* (ii) no one, with all the computing power that exists, has managed so far to map it to a palindrome. In fact, 10677 is the first number to be shown
* to require over fifty iterations before producing a palindrome: 4668731596684224866951378664 (53 iterations, 28-digits).
@ -21,6 +21,8 @@
*
* NOTE: Wording was modified slightly on 24 April 2007 to emphasise the theoretical nature of Lychrel numbers.*/
#define _POSIX_C_SOURCE 199309L
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
@ -31,73 +33,73 @@ int is_lychrel(mpz_t n);
int main(int argc, char **argv)
{
int i, count = 0;
double elapsed;
struct timespec start, end;
mpz_t n;
int i, count = 0;
double elapsed;
struct timespec start, end;
mpz_t n;
clock_gettime(CLOCK_MONOTONIC, &start);
clock_gettime(CLOCK_MONOTONIC, &start);
mpz_init(n);
mpz_init(n);
/* For each number, use the is_lychrel function to check if the number
* is a Lychrel number.*/
for(i = 1; i < 10000; i++)
{
mpz_set_ui(n, i);
/* For each number, use the is_lychrel function to check if the number
* is a Lychrel number.*/
for(i = 1; i < 10000; i++)
{
mpz_set_ui(n, i);
if(is_lychrel(n))
count++;
}
if(is_lychrel(n))
count++;
}
mpz_clear(n);
clock_gettime(CLOCK_MONOTONIC, &end);
mpz_clear(n);
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 55\n");
printf("Answer: %d\n", count);
printf("Project Euler, Problem 55\n");
printf("Answer: %d\n", count);
printf("Elapsed time: %.9lf seconds\n", elapsed);
printf("Elapsed time: %.9lf seconds\n", elapsed);
return 0;
return 0;
}
int is_lychrel(mpz_t n)
{
int i;
mpz_t tmp, reverse, rem;
int i;
mpz_t tmp, reverse, rem;
mpz_inits(tmp, reverse, rem, NULL);
mpz_set(tmp, n);
mpz_inits(tmp, reverse, rem, NULL);
mpz_set(tmp, n);
/* Run for 50 iterations.*/
for(i = 0; i < 50; i++)
{
mpz_set_ui(reverse, 0);
/* Run for 50 iterations.*/
for(i = 0; i < 50; i++)
{
mpz_set_ui(reverse, 0);
/* Find the reverse of the given number.*/
while(mpz_cmp_ui(tmp, 0) > 0)
{
mpz_mul_ui(reverse, reverse, 10);
mpz_tdiv_qr_ui(tmp, rem, tmp, 10);
mpz_add(reverse, reverse, rem);
}
/* Find the reverse of the given number.*/
while(mpz_cmp_ui(tmp, 0) > 0)
{
mpz_mul_ui(reverse, reverse, 10);
mpz_tdiv_qr_ui(tmp, rem, tmp, 10);
mpz_add(reverse, reverse, rem);
}
/* Add the reverse to the original number.*/
mpz_add(tmp, n, reverse);
/* Add the reverse to the original number.*/
mpz_add(tmp, n, reverse);
/* If the sum is a palindrome, the number is not a Lychrel number.*/
if(is_palindrome_mpz(tmp, 10))
{
mpz_clears(tmp, reverse, rem, NULL);
return 0;
}
/* If the sum is a palindrome, the number is not a Lychrel number.*/
if(is_palindrome_mpz(tmp, 10))
{
mpz_clears(tmp, reverse, rem, NULL);
return 0;
}
mpz_set(n, tmp);
}
mpz_set(n, tmp);
}
mpz_clears(tmp, reverse, rem, NULL);
mpz_clears(tmp, reverse, rem, NULL);
return 1;
return 1;
}

View File

@ -3,6 +3,8 @@
*
* Considering natural numbers of the form, a^b, where a, b < 100, what is the maximum digital sum?*/
#define _POSIX_C_SOURCE 199309L
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
@ -10,41 +12,41 @@
int main(int argc, char **argv)
{
int a, b, sum, max = 0;
double elapsed;
struct timespec start, end;
mpz_t pow;
int a, b, sum, max = 0;
double elapsed;
struct timespec start, end;
mpz_t pow;
clock_gettime(CLOCK_MONOTONIC, &start);
clock_gettime(CLOCK_MONOTONIC, &start);
mpz_init(pow);
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;
/* 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);
while(mpz_cmp_ui(pow, 0))
sum += mpz_tdiv_q_ui(pow, pow, 10);
if(sum > max)
max = sum;
}
}
if(sum > max)
max = sum;
}
}
mpz_clear(pow);
mpz_clear(pow);
clock_gettime(CLOCK_MONOTONIC, &end);
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 56\n");
printf("Answer: %d\n", max);
printf("Project Euler, Problem 56\n");
printf("Answer: %d\n", max);
printf("Elapsed time: %.9lf seconds\n", elapsed);
printf("Elapsed time: %.9lf seconds\n", elapsed);
return 0;
return 0;
}

View File

@ -14,6 +14,8 @@
*
* In the first one-thousand expansions, how many fractions contain a numerator with more digits than the denominator?*/
#define _POSIX_C_SOURCE 199309L
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
@ -23,64 +25,64 @@ 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;
int i, count = 0;
mpz_t n, d, d2;
double elapsed;
struct timespec start, end;
clock_gettime(CLOCK_MONOTONIC, &start);
clock_gettime(CLOCK_MONOTONIC, &start);
mpz_init_set_ui(n, 1);
mpz_init_set_ui(d, 1);
mpz_init(d2);
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 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++;
}
}
if(count_digits(n) > count_digits(d))
{
count++;
}
}
mpz_clears(n, d, d2, NULL);
mpz_clears(n, d, d2, NULL);
clock_gettime(CLOCK_MONOTONIC, &end);
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 57\n");
printf("Answer: %d\n", count);
printf("Project Euler, Problem 57\n");
printf("Answer: %d\n", count);
printf("Elapsed time: %.9lf seconds\n", elapsed);
printf("Elapsed time: %.9lf seconds\n", elapsed);
return 0;
return 0;
}
int count_digits(mpz_t n)
{
int count = 0;
mpz_t value;
int count = 0;
mpz_t value;
if(mpz_cmp_ui(n, 0) == 0)
{
return 1;
}
if(mpz_cmp_ui(n, 0) == 0)
{
return 1;
}
mpz_init_set(value, n);
mpz_init_set(value, n);
while(mpz_cmp_ui(value, 0))
{
mpz_tdiv_q_ui(value, value, 10);
count++;
}
while(mpz_cmp_ui(value, 0))
{
mpz_tdiv_q_ui(value, value, 10);
count++;
}
mpz_clear(value);
mpz_clear(value);
return count;
return count;
}

View File

@ -14,6 +14,8 @@
* 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%?*/
#define _POSIX_C_SOURCE 199309L
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
@ -22,57 +24,57 @@
int main(int argc, char **argv)
{
int i = 1, l = 1, step = 2, count = 0, diag = 5;
double ratio, elapsed;
struct timespec start, end;
int i = 1, l = 1, step = 2, count = 0, diag = 5;
double ratio, elapsed;
struct timespec start, end;
clock_gettime(CLOCK_MONOTONIC, &start);
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;
/* 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++;
}
if(is_prime(i))
{
count++;
}
i += step;
i += step;
if(is_prime(i))
{
count++;
}
if(is_prime(i))
{
count++;
}
i += step;
i += step;
if(is_prime(i))
{
count++;
}
if(is_prime(i))
{
count++;
}
i += step;
ratio = (double)count / diag;
i += step;
ratio = (double)count / diag;
step += 2;
diag += 4;
l += 2;
step += 2;
diag += 4;
l += 2;
}while(ratio >= 0.1);
} while(ratio >= 0.1);
clock_gettime(CLOCK_MONOTONIC, &end);
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 58\n");
printf("Answer: %d\n", l);
printf("Project Euler, Problem 58\n");
printf("Answer: %d\n", l);
printf("Elapsed time: %.9lf seconds\n", elapsed);
printf("Elapsed time: %.9lf seconds\n", elapsed);
return 0;
return 0;
}

View File

@ -16,6 +16,8 @@
* 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.*/
#define _POSIX_C_SOURCE 199309L
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

144
C/p060.c
View File

@ -4,6 +4,8 @@
*
* Find the lowest sum for a set of five primes for which any two primes concatenate to produce another prime.*/
#define _POSIX_C_SOURCE 199309L
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
@ -18,97 +20,97 @@ int *primes;
int main(int argc, char **argv)
{
int found = 0, p1, p2, p3, p4, p5, n;
double elapsed;
struct timespec start, end;
int found = 0, p1, p2, p3, p4, p5, n;
double elapsed;
struct timespec start, end;
clock_gettime(CLOCK_MONOTONIC, &start);
clock_gettime(CLOCK_MONOTONIC, &start);
if((primes = sieve(N)) == NULL)
{
fprintf(stderr, "Error! Sieve function returned NULL\n");
return 1;
}
if((primes = sieve(N)) == NULL)
{
fprintf(stderr, "Error! Sieve function returned NULL\n");
return 1;
}
/* Straightforward brute force approach.*/
for(p1 = 3; p1 < N && !found; p1 += 2)
{
/* If p1 is not prime, go to the 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)))
{
/* Straightforward brute force approach.*/
for(p1 = 3; p1 < N && !found; p1 += 2)
{
/* If p1 is not prime, go to the next number.*/
if(!primes[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)))
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;
continue;
}
for(p4 = p3 + 2; p4 < N && !found; p4 += 2)
for(p3 = p2 + 2; p3 < N && !found; p3 += 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;
}
/* 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(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;
}
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;
}
/* If it gets here, the five values have been found.*/
n = p1 + p2 + p3 + p4 + p5;
found = 1;
}
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);
free(primes);
clock_gettime(CLOCK_MONOTONIC, &end);
elapsed=(end.tv_sec-start.tv_sec)+(double)(end.tv_nsec-start.tv_nsec)/1000000000;
clock_gettime(CLOCK_MONOTONIC, &end);
printf("Project Euler, Problem 60\n");
printf("Answer: %d\n", n);
elapsed=(end.tv_sec-start.tv_sec)+(double)(end.tv_nsec-start.tv_nsec)/1000000000;
printf("Elapsed time: %.9lf seconds\n", elapsed);
printf("Project Euler, Problem 60\n");
printf("Answer: %d\n", n);
return 0;
printf("Elapsed time: %.9lf seconds\n", elapsed);
return 0;
}
int cat(int i, int j)
{
char n[10];
char n[10];
sprintf(n, "%d%d", i, j);
sprintf(n, "%d%d", i, j);
return atoi(n);
return atoi(n);
}

258
C/p061.c
View File

@ -17,6 +17,8 @@
* Find the sum of the only ordered set of six cyclic 4-digit numbers for which each polygonal type: triangle, square, pentagonal,
* hexagonal, heptagonal, and octagonal, is represented by a different number in the set.*/
#define _POSIX_C_SOURCE 199309L
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
@ -28,94 +30,94 @@ int chain[6], flags[6] = {0};
int main(int argc, char **argv)
{
int i, n, sum = 0;
double elapsed;
struct timespec start, end;
int i, n, sum = 0;
double elapsed;
struct timespec start, end;
clock_gettime(CLOCK_MONOTONIC, &start);
clock_gettime(CLOCK_MONOTONIC, &start);
i = 1;
n = 1;
i = 1;
n = 1;
/* Generate all triangle numbers smaller than 10000*/
do
{
polygonal[0][n] = 1;
i++;
n = i * (i + 1) / 2;
}while(n < 10000);
/* Generate all triangle numbers smaller than 10000*/
do
{
polygonal[0][n] = 1;
i++;
n = i * (i + 1) / 2;
} while(n < 10000);
i = 1;
n = 1;
i = 1;
n = 1;
/* Generate all square numbers smaller than 10000.*/
do
{
polygonal[1][n] = 1;
i++;
n = i * i;
}while(n < 10000);
/* Generate all square numbers smaller than 10000.*/
do
{
polygonal[1][n] = 1;
i++;
n = i * i;
} while(n < 10000);
i = 1;
n = 1;
i = 1;
n = 1;
/* Generate all pentagonal numbers smaller than 10000.*/
do
{
polygonal[2][n] = 1;
i++;
n = i * (3 * i - 1) / 2;
}while(n < 10000);
/* Generate all pentagonal numbers smaller than 10000.*/
do
{
polygonal[2][n] = 1;
i++;
n = i * (3 * i - 1) / 2;
} while(n < 10000);
i = 1;
n = 1;
i = 1;
n = 1;
/* Generate all hexagonal numbers smaller than 10000.*/
do
{
polygonal[3][n] = 1;
i++;
n = i * (2 * i - 1);
}while(n < 10000);
/* Generate all hexagonal numbers smaller than 10000.*/
do
{
polygonal[3][n] = 1;
i++;
n = i * (2 * i - 1);
} while(n < 10000);
i = 1;
n = 1