Solves problem 50

This means I know solveed 50 euler problems. Youhou !
This commit is contained in:
2012-10-19 13:39:15 +02:00
parent 957ede8215
commit 99334f1594
3 changed files with 108 additions and 0 deletions

View File

@@ -62,6 +62,7 @@ So you may find some of the code here quite ugly. And this is the case :). Why o
45 - Find the next triangle number that is also pentagonal and hexagonal. - < 1 sec <br />
46 - What is the smallest odd composite that cannot be written as the sum of a prime and twice a square? - < 6 sec <br />
48 - Find the last ten digits of 1^1 + 2^2 + ... + 1000^1000. - 0.053 <br />
50 - Which prime, below one-million, can be written as the sum of the most consecutive primes? - **~= 1 hour** <br />
52 - Find the smallest positive integer, x, such that 2x, 3x, 4x, 5x, and 6x, contain the same digits. - 2min <br />
53 - How many values of C(n,r), for 1 <= n <= 100, exceed one-million? - < 1 sec <br />
56 - Considering natural numbers of the form, a^b, finding the maximum digital sum. > 3 sec <br />
@@ -71,6 +72,7 @@ So you may find some of the code here quite ugly. And this is the case :). Why o
43 - Find the sum of all pandigital numbers with an unusual sub-string divisibility property. <br />
47 - Find the first four consecutive integers to have four distinct primes factors. <br />
55 - How many Lychrel numbers are there below ten-thousand? <br />
97 - Find the last ten digits of the non-Mersenne prime: 28433 <20> 2^7830457 + 1. <br />
## Contact

60
e_50.py Normal file
View File

@@ -0,0 +1,60 @@
#!/usr/bin/env python
"""
##---
# Julien Lengrand-Lambert
#Created on : 17 - 10 - 2012
#
# DESCRIPTION : Solves problem 50 of Project Euler
The prime 41, can be written as the sum of six consecutive primes:
41 = 2 + 3 + 5 + 7 + 11 + 13
This is the longest sum of consecutive primes that adds
to a prime below one-hundred.
The longest sum of consecutive primes below one-thousand
that adds to a prime, contains 21 terms, and is equal to 953.
Which prime, below one-million, can be written as the sum of
the most consecutive primes?
##---
"""
import pickle
# list of primes up to one million.
plist = pickle.load(open("primes_list.dup", "rb"))
def is_prime(val):
"""
Returns True if the number is prime
"""
return (val in plist)
def sum_cons_primes(prime_list, max_val=1000000):
"""
Returns the prime that can be written as the sum of
the most consecutive primes below one-million.
"""
max_cons = 0
max_prime = 0
for el in prime_list:
if (prime_list.index(el)) % 10 == 0:
print "%d/%d" % (prime_list.index(el), len(prime_list))
cur_prime = el
cur_ind = prime_list.index(el)
nb_cons = 1
while cur_ind < len(prime_list) - 1 and cur_prime < max_val:
if is_prime(cur_prime):
if nb_cons > max_cons :
max_cons = nb_cons
max_prime = cur_prime
nb_cons += 1
cur_ind += 1
cur_prime += prime_list[cur_ind]
return max_prime, max_cons
if __name__ == '__main__':
print "Answer : %d " % (sum_cons_primes(plist)[0])

46
e_55.py Normal file
View File

@@ -0,0 +1,46 @@
#!/usr/bin/env python
"""
##---
# Julien Lengrand-Lambert
#Created on : 17 - 10 - 2012
#
# DESCRIPTION : Solves problem 55 of Project Euler
If we take 47, reverse and add, 47 + 74 = 121, which is palindromic.
Not all numbers produce palindromes so quickly. For example,
349 + 943 = 1292,
1292 + 2921 = 4213
4213 + 3124 = 7337
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 (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).
Surprisingly, there are palindromic numbers that are themselves Lychrel
numbers; the first example is 4994.
How many Lychrel numbers are there below ten-thousand?
NOTE: Wording was modified slightly on 24 April 2007 to emphasise
the theoretical nature of Lychrel numbers.
##---
"""
if __name__ == '__main__':
print "Answer : %d " % (1)