Add more solutions
Added solutions for problems 33, 34 and 35 in C and python
This commit is contained in:
parent
f3bd132df2
commit
9240a000ca
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)
|
||||
|
36
Python/p033.py
Normal file
36
Python/p033.py
Normal file
@ -0,0 +1,36 @@
|
||||
#!/usr/bin/python3
|
||||
|
||||
from timeit import default_timer
|
||||
from projecteuler import gcd
|
||||
|
||||
def main():
|
||||
start = default_timer()
|
||||
|
||||
prod_n = 1
|
||||
prod_d = 1
|
||||
|
||||
for i in range(11, 100):
|
||||
for j in range(11, 100):
|
||||
if i % 10 != 0 and j % 10 != 0 and\
|
||||
i != j and i % 10 == j // 10:
|
||||
n = i // 10
|
||||
d = j % 10
|
||||
|
||||
f1 = i / j
|
||||
f2 = n / d
|
||||
|
||||
if f1 == f2:
|
||||
prod_n = prod_n * i
|
||||
prod_d = prod_d * j
|
||||
|
||||
div = gcd(prod_n, prod_d)
|
||||
|
||||
end = default_timer()
|
||||
|
||||
print('Project Euler, Problem 33')
|
||||
print('Answer: {}'.format(prod_d // div))
|
||||
|
||||
print('Elapsed time: {:.9f} seconds'.format(end - start))
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
41
Python/p034.py
Normal file
41
Python/p034.py
Normal file
@ -0,0 +1,41 @@
|
||||
#!/usr/bin/python3
|
||||
|
||||
from math import factorial
|
||||
|
||||
from numpy import ones
|
||||
|
||||
from timeit import default_timer
|
||||
|
||||
def main():
|
||||
start = default_timer()
|
||||
|
||||
a = 10
|
||||
sum_ = 0
|
||||
factorials = ones(10, int)
|
||||
|
||||
for i in range(2, 10):
|
||||
factorials[i] = factorial(i)
|
||||
|
||||
while a < 50000:
|
||||
b = a
|
||||
sum_f = 0
|
||||
|
||||
while b != 0:
|
||||
digit = b % 10
|
||||
b = b // 10
|
||||
sum_f = sum_f + factorials[digit]
|
||||
|
||||
if a == sum_f:
|
||||
sum_ = sum_ + a
|
||||
|
||||
a = a + 1
|
||||
|
||||
end = default_timer()
|
||||
|
||||
print('Project Euler, Problem 34')
|
||||
print('Answer: {}'.format(sum_))
|
||||
|
||||
print('Elapsed time: {:.9f} seconds'.format(end - start))
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
49
Python/p035.py
Normal file
49
Python/p035.py
Normal file
@ -0,0 +1,49 @@
|
||||
#!/usr/bin/python3
|
||||
|
||||
from timeit import default_timer
|
||||
from projecteuler import sieve
|
||||
|
||||
def is_circular_prime(n):
|
||||
global primes
|
||||
|
||||
if primes[n] == 0:
|
||||
return 0
|
||||
|
||||
tmp = n
|
||||
count = 0
|
||||
|
||||
while tmp > 0:
|
||||
if tmp % 2 == 0:
|
||||
return 0
|
||||
count = count + 1
|
||||
tmp = tmp // 10
|
||||
|
||||
for i in range(1, count):
|
||||
n = n % (10 ** (count - 1)) * 10 + n // (10 ** (count - 1))
|
||||
|
||||
if primes[n] == 0:
|
||||
return 0
|
||||
|
||||
return 1
|
||||
|
||||
def main():
|
||||
start = default_timer()
|
||||
|
||||
global primes
|
||||
|
||||
primes = sieve(1000000)
|
||||
count = 13
|
||||
|
||||
for i in range(101, 1000000, 2):
|
||||
if is_circular_prime(i):
|
||||
count = count + 1
|
||||
|
||||
end = default_timer()
|
||||
|
||||
print('Project Euler, Problem 35')
|
||||
print('Answer: {}'.format(count))
|
||||
|
||||
print('Elapsed time: {:.9f} seconds'.format(end - start))
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
Loading…
x
Reference in New Issue
Block a user