Ejemplo n.º 1
0
def cycle(polygons, index=0):
    global sequence
    if index == 0:
        for elem in polygons[index]:
            sequence = [elem] + [None] * 5
            cycle(polygons, index + 1)
        return False
    else:
        if index < 5:
            for elem in polygons[index]:
                if elem // 100 == sequence[index - 1] % 100:
                    sequence[index:] = [elem] + [None] * (5 - index)
                    cycle(polygons, index + 1)
            return False
        else:
            for elem in polygons[index]:
                if elem // 100 == sequence[index - 1] % 100 and \
                   elem % 100 == sequence[0] // 100:
                    sequence[index] = elem
                    print(sum(sequence))
                    timer.stop()
                    quit()
Ejemplo n.º 2
0
Problem:
    Find the sum of digits in the numerator of the 100th convergent of the
    continued fraction for e.

Performance time: ~0.0012s

"""

from fractions import Fraction
from timer import timer
from utils import sum_of_digits


timer.start()

a = [int(n / 3 * 2) if n % 3 == 0 else 1 for n in range(1, 101)]
a[0] = 2

def convergent_e(limit=-1, index=0):
    if limit == 0:
        return a[0]
    elif index == limit - 1:
        return a[index] + Fraction(1, a[index+1])
    else:
        return a[index] + Fraction(1, convergent_e(limit, index+1))

print(sum_of_digits(Fraction(convergent_e(99)).numerator))

timer.stop()
Ejemplo n.º 3
0
"""

Problem :
    working out the alphabetical value for each name,
    multiply this value by its alphabetical position in the list to obtain a
    name score.

    What is the total of all the name scores in the file?

Performance time: ~0.0089s

"""

from timer import timer

timer.start()

with open("./data/names.txt") as f:
    names = sorted(f.read().upper().replace("\"", "").split(","))

print(
    sum((i + 1) * sum(ord(c) - 64 for c in names[i])
        for i in range(len(names))))

timer.stop()