Beispiel #1
0
def is_abundant(n):
    if n == 0:
        return False
    factors = all_factors(n)
    sum = 0
    for factor in factors:
        sum += factor
    sum -= n
    return sum > n
Beispiel #2
0
def smallest_k(n,min=0):
	if gcd(n,10) != 1:
		return None
	if gcd(n-9,n) != 1:
		return None
#		return handle_zero_divisors(n)
	factors = primes.all_factors(primes.factor(primes.euler_phi(n)))
	if factors[-1] < min:
		return None
	for factor in factors:
		factor = int(factor)
		if pow(10,factor,n) == 1:
			if factor < min:
				return None
			return factor
Beispiel #3
0
def handle_zero_divisors(n):
	print "Zero divisors",
	factors = primes.all_factors(primes.factor(primes.euler_phi(n)))
	possibilities = PriorityQueue()
	for factor in factors:
		factor = int(factor)
		if pow(10,factor,n) == 1:
			possibilities.put((factor,factor,1))
	if possibilities.empty():
		return None
	while True: #should eventually terminate
		_,factor,multiple = possibilities.get()
		if pow(100,factor,n) == 1:
			print n,factor*multiple
			return factor*multiple
		possibilities.put((factor*(multiple*1),factor,multiple+1))
Beispiel #4
0
    assert (is_prime(29))
    assert (is_prime(31))
    assert (is_prime(37))
    assert (not is_prime(-1))
    assert (not is_prime(0))
    assert (not is_prime(4))
    assert (not is_prime(6))
    assert (not is_prime(9))
    assert (not is_prime(15))
    assert (not is_prime(25))

with timeit("prime_factors test suite"):
    assert (list(prime_factors(2)) == [2])
    assert (list(prime_factors(3)) == [3])
    assert (list(prime_factors(5)) == [5])
    assert (list(prime_factors(2 * 3 * 5 * 7 * 11 *
                               13)) == [2, 3, 5, 7, 11, 13])
    assert (list(prime_factors(2 * 2 * 3 * 3 * 5 * 5 * 7 *
                               7)) == [2, 2, 3, 3, 5, 5, 7, 7])

with timeit("all_factors test suite"):
    assert (all_factors(512) == {1, 2, 4, 8, 16, 32, 64, 128, 256, 512})
    assert (all_factors(2 * 3 *
                        5) == {1, 2, 3, 5, 2 * 3, 2 * 5, 3 * 5, 2 * 3 * 5})

with timeit("is_prime perf test"):
    assert (is_prime(2**61 - 1))

with timeit("prime_factors perf test"):
    assert (list(prime_factors(2**61 - 1)) == [2**61 - 1])
Beispiel #5
0
def findPair(n):
        sum=0
        for x in primes.all_factors(n):sum +=x
        sum -= n
        return sum
Beispiel #6
0
def factorSum(n):
        sum=0
        for x in primes.all_factors(n):sum +=x
        sum -= n
        return sum
Beispiel #7
0
import primes
MAX = 28124
primes._refresh(MAX/2)
abundants = [n for n in xrange(1, MAX) if sum(primes.all_factors(n)) > n+n]
abundants_dict = dict.fromkeys(abundants, 1)
total = 0
for n in xrange(1, MAX):
    sum_of_abundants = 0
    for a in abundants:
        if a > n: break
        if abundants_dict.get(n - a):
            sum_of_abundants = 1
            break
    if not sum_of_abundants:
        total = total + n
print total
Beispiel #8
0
def get_amicabilibuddy(n):
    factors = primes.all_factors(n)
    sum = 0
    for factor in factors[:-1]:
        sum += factor
    return sum