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
This commit is contained in:
2012-10-16 21:46:48 +02:00
parent f1f4da4e8f
commit 1bedcd4c00
3 changed files with 71 additions and 2 deletions

View File

@@ -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 <br />
33 - Discover all the fractions with an unorthodox cancelling method. < 1 sec <br />
34 - Find the sum of all numbers which are equal to the sum of the factorial of their digits. - 30 sec - 16 sec <br />
35 - How many circular primes are there below one million? - **too long** <br />
35 - How many circular primes are there below one million? - **too long** <br />
36 - Find the sum of all numbers less than one million, which are palindromic in base 10 and base 2. - 0.933 <br />
37 - Find the sum of all eleven primes that are both truncatable from left to right and right to left. < 1 min <br />
38 - What is the largest 1 to 9 pandigital that can be formed by multiplying a fixed number by 1, 2, 3, ... ? < 1 sec <br />
@@ -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. <br />
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 />
56 - Considering natural numbers of the form, a^b, finding the maximum digital sum. <br />
97 - Find the last ten digits of the non-Mersenne prime: 28433 <20> 2^7830457 + 1. <br />
## Contact

21
e_40.py Normal file
View File

@@ -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())

46
e_56.py Normal file
View File

@@ -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))