Add Python solutions for Problems 97, 99, 112, 357
This commit is contained in:
parent
419ba8d44d
commit
b2c2fb2e8e
30
Python/p097.py
Normal file
30
Python/p097.py
Normal file
@ -0,0 +1,30 @@
|
||||
# The first known prime found to exceed one million digits was discovered in 1999, and is a Mersenne prime of the form 26972593−1;
|
||||
# it contains exactly 2,098,960 digits. Subsequently other Mersenne primes, of the form 2p−1, have been found which contain more digits.
|
||||
#
|
||||
# However, in 2004 there was found a massive non-Mersenne prime which contains 2,357,207 digits: 28433×2^7830457+1.
|
||||
#
|
||||
# Find the last ten digits of this prime number.
|
||||
|
||||
from projecteuler import timing
|
||||
|
||||
|
||||
@timing
|
||||
def p097():
|
||||
b = 2
|
||||
e = 7830457
|
||||
e_first = 0
|
||||
m = 10000000000
|
||||
c = 1
|
||||
|
||||
while e_first < e:
|
||||
e_first += 1
|
||||
c = (b * c) % m
|
||||
|
||||
result = (c*28433 + 1) % m
|
||||
|
||||
print('Project Euler, Problem 97')
|
||||
print(f'Answer: {result}')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
p097()
|
46
Python/p099.py
Normal file
46
Python/p099.py
Normal file
@ -0,0 +1,46 @@
|
||||
# Comparing two numbers written in index form like 2^11 and 3^7 is not difficult, as any calculator would confirm that
|
||||
# 2^11 = 2048 < 3^7 = 2187.
|
||||
#
|
||||
# However, confirming that 632382^518061 > 519432^525806 would be much more difficult, as both numbers contain over three million digits.
|
||||
#
|
||||
# Using base_exp.txt, a 22K text file containing one thousand lines with a base/exponent pair on each line,
|
||||
# determine which line number has the greatest numerical value.
|
||||
#
|
||||
# NOTE: The first two lines in the file represent the numbers in the example given above.*/
|
||||
|
||||
import sys
|
||||
from math import log
|
||||
|
||||
from projecteuler import timing
|
||||
|
||||
|
||||
@timing
|
||||
def p099():
|
||||
max_ = 0
|
||||
max_i = -1
|
||||
|
||||
try:
|
||||
with open('p099_base_exp.txt', 'r', encoding='utf-8') as fp:
|
||||
base_exps = fp.readlines()
|
||||
except FileNotFoundError:
|
||||
print('Error while opening file p099_base_exp.txt')
|
||||
|
||||
sys.exit(1)
|
||||
|
||||
for i in range(1, 1001):
|
||||
base_exp = base_exps[i - 1].split(',')
|
||||
base = int(base_exp[0])
|
||||
exp = int(base_exp[1])
|
||||
|
||||
curr = exp * log(base)
|
||||
|
||||
if curr > max_:
|
||||
max_ = curr
|
||||
max_i = i
|
||||
|
||||
print('Project Euler, Problem 99')
|
||||
print(f'Answer: {max_i}')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
p099()
|
1000
Python/p099_base_exp.txt
Normal file
1000
Python/p099_base_exp.txt
Normal file
File diff suppressed because it is too large
Load Diff
64
Python/p112.py
Normal file
64
Python/p112.py
Normal file
@ -0,0 +1,64 @@
|
||||
# Working from left-to-right if no digit is exceeded by the digit to its left it is called an increasing number; for example, 134468.
|
||||
#
|
||||
# Similarly if no digit is exceeded by the digit to its right it is called a decreasing number; for example, 66420.
|
||||
#
|
||||
# We shall call a positive integer that is neither increasing nor decreasing a "bouncy" number; for example, 155349.
|
||||
#
|
||||
# Clearly there cannot be any bouncy numbers below one-hundred, but just over half of the numbers below one-thousand (525) are bouncy.
|
||||
# In fact, the least number for which the proportion of bouncy numbers first reaches 50% is 538.
|
||||
#
|
||||
# Surprisingly, bouncy numbers become more and more common and by the time we reach 21780 the proportion of bouncy numbers is equal to 90%.
|
||||
#
|
||||
# Find the least number for which the proportion of bouncy numbers is exactly 99%.
|
||||
|
||||
from projecteuler import timing
|
||||
|
||||
|
||||
def is_bouncy(n):
|
||||
i = 0
|
||||
|
||||
n = str(n)
|
||||
l = len(n)
|
||||
|
||||
while i < l - 1 and n[i] == n[i + 1]:
|
||||
i += 1
|
||||
|
||||
if i == l - 1:
|
||||
return False
|
||||
|
||||
if n[i] < n[i + 1]:
|
||||
for i in range(i, l - 1):
|
||||
if n[i] > n[i + 1]:
|
||||
return True
|
||||
|
||||
if n[i] > n[i + 1]:
|
||||
for i in range(i, l - 1):
|
||||
if n[i] < n[i + 1]:
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
|
||||
@timing
|
||||
def p112():
|
||||
i = 100
|
||||
n_bouncy = 0
|
||||
ratio = 0.0
|
||||
|
||||
while True:
|
||||
if is_bouncy(i):
|
||||
n_bouncy += 1
|
||||
|
||||
ratio = n_bouncy / i
|
||||
|
||||
if ratio == 0.99:
|
||||
break
|
||||
|
||||
i += 1
|
||||
|
||||
print('Project Euler, Problem 112')
|
||||
print(f'Answer: {i}')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
p112()
|
40
Python/p357.py
Normal file
40
Python/p357.py
Normal file
@ -0,0 +1,40 @@
|
||||
# Consider the divisors of 30: 1,2,3,5,6,10,15,30.
|
||||
# It can be seen that for every divisor d of 30, d+30/d is prime.
|
||||
#
|
||||
# Find the sum of all positive integers n not exceeding 100 000 000
|
||||
# such that for every divisor d of n, d+n/d is prime.
|
||||
|
||||
from math import floor, sqrt
|
||||
|
||||
from projecteuler import sieve, timing
|
||||
|
||||
|
||||
N = 100000000
|
||||
primes = sieve(N + 2)
|
||||
|
||||
|
||||
def check_d_nd_prime(n):
|
||||
limit = floor(sqrt(n))
|
||||
|
||||
for i in range(2, limit + 1):
|
||||
if n % i == 0:
|
||||
if not primes[i + int(n/i)]:
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
|
||||
@timing
|
||||
def p357():
|
||||
sum_ = 1
|
||||
|
||||
for i in range(2, N + 1, 2):
|
||||
if primes[i + 1] and check_d_nd_prime(i):
|
||||
sum_ += i
|
||||
|
||||
print('Project Euler, Problem 357')
|
||||
print(f'Answer: {sum_}')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
p357()
|
@ -3,5 +3,5 @@ These are my solutions in C and Python, not necessarily the best solutions. I've
|
||||
|
||||
# Notes
|
||||
- Solutions for problems 82, 86, 95, and 145 in Python are quite slow.
|
||||
- Solutions for problems 84, 89, 97. 99, 102, 112, 124 and 357 have been implemented in C but not in Python.
|
||||
- Solutions for problems 84, 89, 102, and 124 have been implemented in C but not in Python.
|
||||
- Solutions for problems 88, 90 and 91 have been implemented in Python but not in C.
|
||||
|
Loading…
x
Reference in New Issue
Block a user