From 99334f15945720a54eec7ca56bf462ed4f2551a6 Mon Sep 17 00:00:00 2001 From: Julien Lengrand-Lambert Date: Fri, 19 Oct 2012 13:39:15 +0200 Subject: [PATCH] Solves problem 50 This means I know solveed 50 euler problems. Youhou ! --- README.markdown | 2 ++ e_50.py | 60 +++++++++++++++++++++++++++++++++++++++++++++++++ e_55.py | 46 +++++++++++++++++++++++++++++++++++++ 3 files changed, 108 insertions(+) create mode 100644 e_50.py create mode 100644 e_55.py diff --git a/README.markdown b/README.markdown index 0f938a8..53f6f96 100644 --- a/README.markdown +++ b/README.markdown @@ -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
46 - What is the smallest odd composite that cannot be written as the sum of a prime and twice a square? - < 6 sec
48 - Find the last ten digits of 1^1 + 2^2 + ... + 1000^1000. - 0.053
+50 - Which prime, below one-million, can be written as the sum of the most consecutive primes? - **~= 1 hour**
52 - Find the smallest positive integer, x, such that 2x, 3x, 4x, 5x, and 6x, contain the same digits. - 2min
53 - How many values of C(n,r), for 1 <= n <= 100, exceed one-million? - < 1 sec
56 - Considering natural numbers of the form, a^b, finding the maximum digital sum. > 3 sec
@@ -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.
47 - Find the first four consecutive integers to have four distinct primes factors.
+55 - How many Lychrel numbers are there below ten-thousand?
97 - Find the last ten digits of the non-Mersenne prime: 28433 � 2^7830457 + 1.
## Contact diff --git a/e_50.py b/e_50.py new file mode 100644 index 0000000..4979bdc --- /dev/null +++ b/e_50.py @@ -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]) diff --git a/e_55.py b/e_55.py new file mode 100644 index 0000000..bbd484e --- /dev/null +++ b/e_55.py @@ -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)