示例#1
0
def find_chain(n):
    ans = 1
    while n not in [169, 871, 872, 145] and sum_digits(n, lambda x: factorial(x)) != n:
        n = sum_digits(n, lambda x: factorial(x))
        if n < limit:
            visited[n] = 1
        ans += 1
    if n == 169:
        return ans + 2
    elif n == 871 or n == 872:
        return ans + 1
    else:
        return ans
示例#2
0
def problem_92(r, c89=0):
    for x in xrange(1, r):
        while x not in [1, 89]:
            x = sum_digits(x, 2)
        if x == 89:
            c89 += 1
    return c89
示例#3
0
def compute():
    limit = 10000000
    m = [0] * limit
    m[0], m[1], m[89] = 2, 2, 1
    for i in range(len(m)):
        if m[i] == 0:
            sequence = set()
            while m[i] == 0:
                sequence.add(i)
                i = sum_digits(i, lambda x: x**2)
            for s in sequence:
                m[s] = m[i]
    return len([s for s in range(limit) if m[s] == 1])
# 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?

from euler import sum_digits

max_sum = 0
for a in range(100):
    for b in range(100):
        max_sum = max(max_sum, sum_digits(a**b))
print(max_sum)

# the whole thing as a one liner... ugh, readability!
print(
    max([
        sum([int(d) for d in str(a**b)]) for a in range(100)
        for b in range(100)
    ]))

# getting crazy now
import itertools
print(max( map(lambda x: sum([int(d) for d in str(x)]), \
    map(lambda n: n[0] ** n[1], itertools.product(range(100), repeat=2)))))
示例#5
0
# Euler 65
# convergents for e are [2; 1,2,1,  1,4,1,  1,6,1,  1,8,1, ... 1,2k,1]
# So the first 10 terms are 2, 3, 8/3, 11/4, 19/7, 82/32 ... 1457/536.
# Find the sum of digits in the numerator of the 100th convergent
# of the continued fraction for e.

from fractions import Fraction
from math import ceil
from euler import sum_digits, convert_convergents_to_fraction


def generate_convergents_for_e(n):
    result = [2]
    groups = ceil((n - 1) / 3)
    for k in range(1, groups + 1):
        result += [1, 2 * k, 1]
    return result[0:n]


if __name__ == '__main__':
    for i in [100]:  #range(1,11):
        convergents = generate_convergents_for_e(i)
        convergent_fraction = convert_convergents_to_fraction(convergents)
        sum_digits_numerator = sum_digits(convergent_fraction.numerator)
        print("{}: {} (sum digits numerator: {})".format(
            i, convergent_fraction, sum_digits_numerator))
示例#6
0
def compute():
    limit = factorial[9] * 7
    return sum([n for n in range(3, limit) if sum_digits(n, lambda x: factorial[x]) == n])
示例#7
0
'''
Created on Jan 10, 2017

The number 512 is interesting because it is equal to the sum of its digits raised to some power: 5 + 1 + 2 = 8, and 8^3 = 512.
Another example of a number with this property is 614656 = 28^4.
We shall define an to be the nth term of this sequence and insist that a number must contain at least two digits to have a sum.
You are given that a2 = 512 and a10 = 614656.
Find a30.

@author: mstackpo
'''

from datetime import datetime

start = datetime.now()

from euler import sum_digits

pows = set()
for base in range(2, 100):
    for exp in range(2, 20):
        x = base**exp
        s = sum_digits(x)
        if x > 10 and s == base:
            pows.add(x)

print(sorted(pows)[29])

end = datetime.now()
print("runtime = %s" % (end - start))
示例#8
0
def compute():
    return max([sum_digits(a**b) for a in range(1,100) for b in range(1,100)])
示例#9
0
def compute():
    return sum_digits(2 ** 1000)
示例#10
0
'''
Created on Jan 2, 2017

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, ab, where a, p < 100, what is the maximum digital sum?

@author: mstackpo
'''

from datetime import datetime
start = datetime.now()

from euler import sum_digits

max_sum = 0

for a in range(100):
    for p in range(100):
        max_sum = max(sum_digits(a**p), max_sum)
print(max_sum)

end = datetime.now()
print("runtime = %s" % (end - start))