示例#1
0
def amicables():
    a = 0
    while True:
        a += 1
        b = sum(divisors(a))
        if b > a:
            a_hat = sum(divisors(b))
            if a == a_hat:
                yield (a, b)
示例#2
0
 def __init__(self, n):
     self.abundant = []
     self.isAbundant = (n + 1) * [False]
     for num in xrange(1, n + 1):
         if sum(e.divisors(num)) > num:
             self.abundant.append(num)
             self.isAbundant[num] = True
示例#3
0
def divisor_permutations(n, largest):
    if n == 1:
        yield []
    divisors = euler.divisors(n)
    divisors = [d for d in divisors if d > 1 and d <= largest]
    for d in divisors:
        for rest in divisor_permutations(n / d, d):
            yield [d] + rest
示例#4
0
def divisor_permutations(n, largest):
    if n == 1:
        yield []
    divisors = euler.divisors(n)
    divisors = [ d for d in divisors if d > 1 and d <= largest]
    for d in divisors:
        for rest in divisor_permutations(n/d, d):
            yield [d] + rest
示例#5
0
def fraction(n):
    total = 0
    divisors = euler.divisors(n)
    divisors.remove(1)
    for i in range(n // 3 + 1, (n + 1) // 2):
        for d in divisors:
            if i % d == 0:
                break
        else:
            total += 1
    return total
示例#6
0
def problem_23(s=0):
    n = set()
    for x in xrange(1, 28123):
        if sum(divisors(x)) > x:
            n.add(x)
        for y in n:
            if x-y in n:
                break
        else:
            s += x
    return s
示例#7
0
def abundants():
    n = 1
    while True:
        if sum(divisors(n)) > n:
            yield n
        n += 1
示例#8
0
def divisors(num):
    if num not in DIVISORS_DICT:
        DIVISORS_DICT[num] = euler.divisors(num)
    return DIVISORS_DICT[num]
示例#9
0
文件: p012.py 项目: dmmfix/pyeuler
#!/usr/bin/python
import euler

tn = 1
c = 1
ld = 1
while ld < 500:
    c += 1
    tn += c
    f = euler.factors(tn)
    if (len(f) > 8):
        d = euler.divisors(f)
        ld = len(d)

print(c, tn, ld)
示例#10
0
def d(n):
    return sum(divisors(n))
import sys
from math import ceil
from euler import divisors

def triangular():
    i = 1
    number = 0
    while True:
        number += i
        i += 1
        yield number

target = int(sys.argv[1])
for number in triangular():
    div = divisors(number)
    if len(div) > target:
        print(number, sorted(list(div)))
        break
示例#12
0
"""
A perfect number is a number for which the sum of its proper divisors is exactly equal to the number. For example, the sum of the proper divisors of 28 would be 1 + 2 + 4 + 7 + 14 = 28, which means that 28 is a perfect number.

A number n is called deficient if the sum of its proper divisors is less than n and it is called abundant if this sum exceeds n.

As 12 is the smallest abundant number, 1 + 2 + 3 + 4 + 6 = 16, the smallest number that can be written as the sum of two abundant numbers is 24. By mathematical analysis, it can be shown that all integers greater than 28123 can be written as the sum of two abundant numbers. However, this upper limit cannot be reduced any further by analysis even though it is known that the greatest number that cannot be expressed as the sum of two abundant numbers is less than this limit.

Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers.
"""
from itertools import combinations_with_replacement
from euler import divisors

abundant_numbers = [x for x in range(1, 28124) if sum(divisors(x)[:-1]) > x]
Ns = set(range(1, 28124))
ns = {x + y for x, y in combinations_with_replacement(abundant_numbers, 2)}
result = sum(Ns - ns)
示例#13
0
文件: 021.py 项目: Snack-X/euler
def d(n):
  return sum(euler.divisors(n))
示例#14
0
import euler

n=1

while euler.divisors(euler.triangle(n)) < 500:
    n += 1

print euler.triangle(n)
示例#15
0
import sys
import euler
import collections
import math
import itertools

L  = 1500000
ks = euler.divisors(L)
ks = list(ks)
ks.sort()

def tri(n, m, k = 1):
    nsq = n*n
    msq = m*m
    a = k * (msq - nsq)
    b = k * 2 * m * n
    c = k * (msq + nsq)
    return (a,b,c)
def perim(n, m, k = 1):
    return 2*k*m*(m+n)
def odd(n):
    return not even(n)
def even(n):
    return n%2==0

def solve(L, k = 1):
    for n in xrange(1,L):
        for m in xrange(n+1,L):
            #t = tri(n,m,k)
            p = perim(n,m,k)
            if p > L:
示例#16
0
文件: p012.py 项目: wimglenn/euler
"""
The sequence of triangle numbers is generated by adding the natural numbers. So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be:

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

Let us list the factors of the first seven triangle numbers:

 1: 1
 3: 1,3
 6: 1,2,3,6
10: 1,2,5,10
15: 1,3,5,15
21: 1,3,7,21
28: 1,2,4,7,14,28
We can see that 28 is the first triangle number to have over five divisors.

What is the value of the first triangle number to have over five hundred divisors?
"""
from itertools import count
from euler import divisors, triangle

for n in count():
    if len(divisors(triangle(n))) > 500:
        result = triangle(n)
        break
示例#17
0
from euler import divisors
from itertools import ifilter

# Let d(n) be defined as the sum of proper divisors of n (numbers less
# than n which divide evenly into n).
#
# If d(a) = b and d(b) = a, where a != b, then a and b are an amicable
# pair and each of a and b are called amicable numbers.
#
# For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20,
# 22, 44, 55 and 110; therefore d(220) = 284. The proper divisors of
# 284 are 1, 2, 4, 71 and 142; so d(284) = 220.
#
# Evaluate the sum of all the amicable numbers under 10000.

MAX = 10000

d = lambda n: sum(divisors(n, proper=True))

def amicable(t, dnums):
    if t[1] > MAX: return False
    if t[0] == t[1]: return False
    return dnums[t[1]][1] == t[0]

dnums = [ (x, d(x)) for x in xrange(0, MAX+1) ]
anums = ( t[0] for t in dnums if amicable(t, dnums) )
print sum(anums)



示例#18
0
文件: 023.py 项目: jaredks/euler
def sum_proper_divisors(n):
    return sum(divisors(n)) - n
示例#19
0
import sys
import euler
import collections
import math
import itertools

L = 1500000
ks = euler.divisors(L)
ks = list(ks)
ks.sort()


def tri(n, m, k=1):
    nsq = n * n
    msq = m * m
    a = k * (msq - nsq)
    b = k * 2 * m * n
    c = k * (msq + nsq)
    return (a, b, c)


def perim(n, m, k=1):
    return 2 * k * m * (m + n)


def odd(n):
    return not even(n)


def even(n):
    return n % 2 == 0
示例#20
0
from euler import trianglenums, divisors

for t in trianglenums():
    divs = divisors(t)
    if len(divs) > 500:
        print('{} has {} divisors'.format(t, len(divs)))
        print(divs)
        break






示例#21
0
import euler

n = 1

while euler.divisors(euler.triangle(n)) < 500:
    n += 1

print euler.triangle(n)
示例#22
0
"""
Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n).
If d(a) = b and d(b) = a, where a != b, then a and b are an amicable pair and each of a and b are called amicable numbers.

For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110; therefore d(220) = 284. The proper divisors of 284 are 1, 2, 4, 71 and 142; so d(284) = 220.

Evaluate the sum of all the amicable numbers under 10000.
"""
from euler import divisors

result = 0
for n in range(1, 10000):
    d = sum(divisors(n)[:-1])
    if d != n and sum(divisors(d)[:-1]) == n:
        result += n
示例#23
0
文件: 021.py 项目: jaredks/euler
def sum_proper_divisors(n):
    return sum(divisors(n)) - n
示例#24
0
import sys
from euler import divisors

amicable = set()
for i in range(4, int(sys.argv[1])+1):
	if i in amicable: continue
	check = sum(sorted(list(divisors(i)))[:-1])
	if check == i: continue
	if i == sum(sorted(list(divisors(check)))[:-1]):
		amicable.add(i)
		amicable.add(check)
		print("Found "+str(i)+" and "+str(check));

print(sum(amicable))
示例#25
0
 def testDivisors(self):
     self.assert_(euler.divisors(24) == set([1,2,3,4,6,8,12,24]))
示例#26
0
from euler import divisors
from math import ceil

# see https://en.wikipedia.org/wiki/Abundant_number for assertions
# see http://oeis.org/A048242/b048242.txt for pre-generated complete list

bound = 28123 # all int > can be written as sum of two abundant
abundant = set()
for i in range(12, bound):
	if i in abundant: continue

	check = sum(sorted(list(divisors(i)))[:-1])
	if check > i:
		abundant.add(i)
	if not check < i:
		# multiples of perfect and abundant numbers are abundant
		j = i+i
		while j <= bound:
			abundant.add(j)
			j += i
abundant = list(abundant)
abundant.sort()

summed = set()
for i in abundant:
	for j in abundant:
		s = i+j
		if s > bound: break;
		summed.add(s)
print(sum(set(range(1, bound+1)) - summed))
示例#27
0
文件: p095.py 项目: ricbit/euler
def iter_chain(n):
    while True:
        yield n
        if n == 0:
            return
        n = sum(euler.divisors(n, primes)) - n