Ejemplo n.º 1
0
def pe12(n=500):
	'''What is the value of the first triangle number to have 
	over five hundred divisors?'''
	m = 1

	while True:
		if utils.n_div(utils.triangle(m)) > n:
			return utils.triangle(m)
			break
		m += 1
Ejemplo n.º 2
0
def part2() -> int:
    m = mean(DATA)
    pos_low = int(m)
    pos_high = int(m + 1)
    return min(
        sum(utils.triangle(abs(i - pos)) for i in DATA)
        for pos in (pos_high, pos_low))
Ejemplo n.º 3
0
"""
The nth term of the sequence of triangle numbers is given by, tn = (n * (n + 1)) / 2; so the first ten triangle numbers are:

1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...

By converting each letter in a word to a number corresponding to its alphabetical position and adding these values 
we form a word value. For example, the word value for SKY is 19 + 11 + 25 = 55 = t10. If the word value is a 
triangle number then we shall call the word a triangle word.

Using words.txt (right click and 'Save Link/Target As...'), a 16K text file containing nearly two-thousand common 
English words, how many are triangle words?
"""

from utils import triangle
triangles = [triangle(n) for n in range(1,20)]

f = open("Data/words.txt", "r")
words = f.readlines()[0].replace("\"", "").split(",")

print len([word for word in words if sum(ord(c) - 64 for c in word) in triangles])
Ejemplo n.º 4
0
def problem_twelve():
    for tri in triangle():
        num_divisors = len(factors(tri))
        print tri, num_divisors
        if num_divisors > 500:
            return tri
Ejemplo n.º 5
0
        while x % prime == 0:
            if not prime in factors:
                factors[prime] = 0
            factors[prime] += 1
            x //= prime
            if x == 1:
                break
            
    return [(prime,factors[prime]) for prime in factors if factors[prime] > 0]

def factor_count(x):
    factors = prime_factors(x)
    
    if len(factors) == 0:
        return 2
    
    total = 1
    for prime,pow in factors:
        total *= pow + 1
        
    return total

if __name__ == "__main__":    
    for i in xrange(1,200000):
        num = triangle(i)
        #print "Num", num
        count = factor_count(num)
        #print num, ":", count
        if count > 500:
            print num
            break
Ejemplo n.º 6
0
def problem_twelve():
    for tri in triangle():
        num_divisors = len(factors(tri))
        print tri, num_divisors
        if num_divisors > 500:
            return tri