Add more solutions
Added solutions for problems 33, 34 and 35 in C and python
This commit is contained in:
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()
|
Reference in New Issue
Block a user