Add more solutions
Added solutions for problems 33, 34 and 35 in C and python
This commit is contained in:
48
C/p033.c
Normal file
48
C/p033.c
Normal file
@ -0,0 +1,48 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include "projecteuler.h"
|
||||
|
||||
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;
|
||||
|
||||
clock_gettime(CLOCK_MONOTONIC, &start);
|
||||
|
||||
for(i = 11; i < 100; i++)
|
||||
{
|
||||
for(j = 11; j < 100; j++)
|
||||
{
|
||||
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)
|
||||
{
|
||||
prod_n *= i;
|
||||
prod_d *= j;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
div = gcd(prod_n, prod_d);
|
||||
|
||||
clock_gettime(CLOCK_MONOTONIC, &end);
|
||||
|
||||
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("Elapsed time: %.9lf seconds\n", elapsed);
|
||||
|
||||
return 0;
|
||||
}
|
68
C/p034.c
Normal file
68
C/p034.c
Normal file
@ -0,0 +1,68 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <gmp.h>
|
||||
|
||||
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];
|
||||
|
||||
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);
|
||||
|
||||
for(i = 0; i < 10; i++)
|
||||
{
|
||||
mpz_init_set_ui(factorials[i], 1);
|
||||
}
|
||||
|
||||
for(i = 2; i < 10; i++)
|
||||
{
|
||||
mpz_fac_ui(factorials[i], i);
|
||||
}
|
||||
|
||||
while(mpz_cmp_ui(a, 50000) < 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]);
|
||||
}
|
||||
|
||||
if(!mpz_cmp(a, sum_f))
|
||||
{
|
||||
mpz_add(sum, sum, a);
|
||||
}
|
||||
|
||||
mpz_add_ui(a, a, 1);
|
||||
}
|
||||
|
||||
mpz_clears(a, b, q, sum_f, NULL);
|
||||
|
||||
for(i = 0; i < 10; i++)
|
||||
{
|
||||
mpz_clear(factorials[i]);
|
||||
}
|
||||
|
||||
clock_gettime(CLOCK_MONOTONIC, &end);
|
||||
|
||||
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("Elapsed time: %.9lf seconds\n", elapsed);
|
||||
|
||||
mpz_clear(sum);
|
||||
|
||||
return 0;
|
||||
}
|
77
C/p035.c
Normal file
77
C/p035.c
Normal file
@ -0,0 +1,77 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
#include <time.h>
|
||||
#include "projecteuler.h"
|
||||
|
||||
#define N 1000000
|
||||
|
||||
int is_circular_prime(int n);
|
||||
|
||||
int *primes;
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int i, count = 13;
|
||||
double elapsed;
|
||||
struct timespec start, end;
|
||||
|
||||
clock_gettime(CLOCK_MONOTONIC, &start);
|
||||
|
||||
primes = sieve(N);
|
||||
|
||||
for(i = 101; i < 1000000; i += 2)
|
||||
{
|
||||
if(is_circular_prime(i))
|
||||
{
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
free(primes);
|
||||
|
||||
clock_gettime(CLOCK_MONOTONIC, &end);
|
||||
|
||||
elapsed = (end.tv_sec - start.tv_sec) + (double)(end.tv_nsec - start.tv_nsec) / 1000000000;
|
||||
|
||||
printf("Project Euler, Problem 35\n");
|
||||
printf("Answer: %d\n", count);
|
||||
|
||||
printf("Elapsed time: %.9lf seconds\n", elapsed);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int is_circular_prime(int n)
|
||||
{
|
||||
int i, tmp, count;
|
||||
|
||||
if(primes[n] == 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
tmp = n;
|
||||
count = 0;
|
||||
|
||||
while(tmp > 0)
|
||||
{
|
||||
if(tmp % 2 == 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
count++;
|
||||
tmp /= 10;
|
||||
}
|
||||
|
||||
for(i = 1; i < count; i++)
|
||||
{
|
||||
n = n % (int)pow(10, count-1) * 10 + n / (int)pow(10, count-1);
|
||||
if(primes[n] == 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
@ -34,9 +34,11 @@ int is_prime(long int num)
|
||||
long int gcd(long int a, long int b)
|
||||
{
|
||||
if(b == 0)
|
||||
{
|
||||
return a;
|
||||
else
|
||||
return gcd(b, a%b);
|
||||
}
|
||||
|
||||
return gcd(b, a%b);
|
||||
}
|
||||
|
||||
long int lcm(long int a, long int b)
|
||||
|
Reference in New Issue
Block a user