From 1bedcd4c00cb90da91328900e9a7638d0817d538 Mon Sep 17 00:00:00 2001 From: Julien Lengrand-Lambert Date: Tue, 16 Oct 2012 21:46:48 +0200 Subject: [PATCH] Solves problem 56 Didn't expect it to be so simple. I'd like to find the nice way to sum all numbers of an integer --- README.markdown | 6 ++++-- e_40.py | 21 +++++++++++++++++++++ e_56.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 e_40.py create mode 100644 e_56.py diff --git a/README.markdown b/README.markdown index a0acba6..27f541e 100644 --- a/README.markdown +++ b/README.markdown @@ -12,7 +12,7 @@ The scripts are named as e_problemnumber_inc. The higher the inc, the finer the Here is a list of all already solved problems, with a one line explanation. Should be used in order to help future reuse of code :) -**Note :** I try to get my solutions within a one minute timeframe. What I want here is to solve problems, not get them running as fast as possible. +**Note :** I try to get my solutions within a one minute timeframe. What I want here is to solve problems, not get them running as fast as possible. So you may find some of the code here quite ugly. And this is the case :). Why optimize something that completely fits its purpose ? ;) @@ -51,7 +51,7 @@ So you may find some of the code here quite ugly. And this is the case :). Why o 31 - Investigating combinations of English currency denominations. - < 1 sec
33 - Discover all the fractions with an unorthodox cancelling method. < 1 sec
34 - Find the sum of all numbers which are equal to the sum of the factorial of their digits. - 30 sec - 16 sec
-35 - How many circular primes are there below one million? - **too long**
+35 - How many circular primes are there below one million? - **too long**
36 - Find the sum of all numbers less than one million, which are palindromic in base 10 and base 2. - 0.933
37 - Find the sum of all eleven primes that are both truncatable from left to right and right to left. < 1 min
38 - What is the largest 1 to 9 pandigital that can be formed by multiplying a fixed number by 1, 2, 3, ... ? < 1 sec
@@ -67,8 +67,10 @@ So you may find some of the code here quite ugly. And this is the case :). Why o ## In progress: +40 - Finding the nth digit of the fractional part of the irrational number.
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.
+56 - Considering natural numbers of the form, a^b, finding the maximum digital sum.
97 - Find the last ten digits of the non-Mersenne prime: 28433 � 2^7830457 + 1.
## Contact diff --git a/e_40.py b/e_40.py new file mode 100644 index 0000000..746a6d5 --- /dev/null +++ b/e_40.py @@ -0,0 +1,21 @@ +#!/usr/bin/env python +""" + ##--- + # Julien Lengrand-Lambert + #Created on : 16 - 10 - 2012 + # + # DESCRIPTION : Solves problem 40 of Project Euler + An irrational decimal fraction is created by concatenating the positive integers: + +0.123456789101112131415161718192021... + +It can be seen that the 12th digit of the fractional part is 1. + +If d_n represents the nth digit of the fractional part, find the value of the following expression. +d_1 d_10 d_100 d_1000 d_10000 d_100000 d_1000000 + + ##--- + """ + if __name__ == '__main__': + print 1 + #print "Answer : %d " % (last_ten()) \ No newline at end of file diff --git a/e_56.py b/e_56.py new file mode 100644 index 0000000..4b14075 --- /dev/null +++ b/e_56.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python +""" + ##--- + # Julien Lengrand-Lambert + #Created on : 16 - 10 - 2012 + # + # DESCRIPTION : Solves problem 56 of Project Euler +A googol (10^100) is a massive number: one followed by one-hundred zeros; +100^100 is almost unimaginably large: one followed by two-hundred zeros. +Despite their size, the sum of the digits in each number is only 1. + +Considering natural numbers of the form, a^b, where a, b 100, +what is the maximum digital sum? + ##--- + """ + + +def sum_numbers(val): + """ + Given an value, returns the sum of all its numbers + """ + temp = str(val) + res = sum([int(i) for i in temp]) + return res + + +def sum_pow(max_a, max_b=None): + """ + given val_a and val_b, returns the maximum sum of all numbers of form a^b, + where a < max_a and b < max_b + If val_b is None, max_a = max_b + """ + if max_b is None: + max_b = max_a + + max_val = 0 + for cur_a in xrange(1, max_a + 1): + for cur_b in xrange(1, max_b + 1): + cur_val = sum_numbers(pow(cur_a, cur_b)) + if cur_val > max_val: + max_val = cur_val + return max_val + + +if __name__ == '__main__': + print "Answer : %d " % (sum_pow(100))