Example #1
0
def euler5b(list):

    k = max(list)

    p = primes.primes(k)
    a = [0] * len(p)

    for j in list:

        # get prime factors
        f = primes.primeFactors(j)

        # iterate over p[i]
        for i in range(0, len(p)):

            # count of this prime in the prime factors
            c = len(filter(lambda x: x == p[i], f))

            a[i] = max(c, a[i])

    # compute the final answer
    r = 1
    for i in range(0, len(p)):
        r *= p[i] ** a[i]

    return r
Example #2
0
def phi(n):
    #Using Euler's formula: phi(n) = n \prod_{p|n} (1 - 1 / p),
    #where p|n are primes that divide n
    if n % 100000 == 0: print(n)

    factors = set(primeFactors(n))
    prime_div = np.array(list(factors))
    return n * np.prod(1 - 1 / prime_div)
Example #3
0
def doTest(n) :
	d = {}
	nFactors = primes.primeFactors(n)
	sp = specialFactors.specialFactors(nFactors)
	for i in sp :
		y = int((n * i)/(i - n))
		if (i+y) == (i * y) / n :
			if i not in d :
				d[y] = i
	return len(d)
Example #4
0
def doTest(n):
    d = {}
    nFactors = primes.primeFactors(n)
    sp = specialFactors.specialFactors(nFactors)
    for i in sp:
        y = int((n * i) / (i - n))
        if (i + y) == (i * y) / n:
            if i not in d:
                d[y] = i
    return len(d)
Example #5
0
def reducedFraction(t):
    num, denom = t[0], t[1]
    #if prime_cache.isPrime(num) or prime_cache.isPrime(denom) :
    if primes.isPrime(num) or primes.isPrime(denom):
        return t
    else:
        pn = primes.primeFactors(num)
        pd = primes.primeFactors(denom)
        i, j = 0, 0
        remove = []
        while i < len(pn) and j < len(pd):
            if pn[i] < pd[j]: i += 1
            elif pn[i] > pd[j]: j += 1
            else:
                remove.append((i, j))
                i += 1
                j += 1
        L = len(remove)
        for j in range(L):
            t = remove.pop()
            pn.pop(t[0])
            pd.pop(t[1])
        return (product(pn), product(pd))
Example #6
0
def reducedFraction(t) :
	num, denom = t[0], t[1]
	#if prime_cache.isPrime(num) or prime_cache.isPrime(denom) :
	if primes.isPrime(num) or primes.isPrime(denom) :
		return t
	else :
		pn = primes.primeFactors(num)
		pd = primes.primeFactors(denom)
		i, j = 0, 0
		remove = []
		while i < len(pn) and j < len(pd) :
			if pn[i] < pd[j] : i += 1
			elif pn[i] > pd[j] : j += 1
			else :
				remove.append( (i, j) )
				i += 1
				j += 1
		L = len(remove)
		for j in range(L) :
			t = remove.pop()
			pn.pop(t[0])
			pd.pop(t[1])
		return ( product(pn) , product(pd) )
Example #7
0
def numDivisors(n):
    fatores = {}

    if (n<=2):
        return
    
    for i in primes.primeFactors(n, 2):
        if i in fatores:
            fatores[i] = fatores[i] + 1
        else:
            fatores[i] = 1
    
    numDiv = 1
    for i in fatores:
        numDiv = numDiv*(fatores[i]+1)
        
    return numDiv
Example #8
0
import specialFactors
import primes


j = 4
max_so_far = -1
k = -1
while 1 :
	if not primes.isPrime(j) :
		f = primes.factors(j)
		sp = specialFactors.specialFactors(f, j)	
		L = len(sp)
		if L > max_so_far :
			pf = primes.primeFactors(j)
			max_so_far, k = L, j
			print k, max_so_far, pf
		if L > 1000 : break
		print j, L, "\r", 
	j += 1
print
print k, max_so_far
Example #9
0
2991 2991 1992 1.5015060241 [3, 997]
4435 4435 3544 1.25141083521 [5, 887]
20617 20617 20176 1.02185765266 [53, 389]
45421 45421 44512 1.02042145938 [53, 857]
69271 69271 67912 1.02001119095 [53, 1307]
"""

d = {}
min_so_far = 20000
k = 0
for i in xrange(1000 * 100 * 95, 10, -1):
    ##for i in xrange(1000*1000*10 - 1, 10, -1) :
    ##for i in xrange(10, 1000*1000*10) :
    print i, k, min_so_far, "\r",
    if not prime_cache.isPrime(i):
        p = primes.relativePrimes(i)
        phi = len(p)
        a = sorted(c for c in str(i))
        b = sorted(c for c in str(phi))
        if a == b:
            score = float(i) / float(phi)
            d[i] = phi
            if score < min_so_far:
                k, min_so_far = i, score
                print i, k, phi, min_so_far, primes.primeFactors(i)

print
min_score = min(float(k) / float(v) for k, v in d.iteritems())
for k, v in d.iteritems():
    if v == min_score:
        print k, v
import primes
import matplotlib.pyplot as plt
X = range(2, 1001)
Y = []
for x in X:
    Y.append(len(primes.primeFactors(x)))
plt.plot(Y, 'x')
plt.xlabel('n')
plt.ylabel('number of factors of n')
plt.show()
Example #11
0
'''
The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143?
'''
import primes

print max(primes.primeFactors(600851475143, 2))
Example #12
0
def doTest(n) :
	d = {}
	nFactors = primes.primeFactors(n)
	sp = specialFactors.specialFactors(nFactors)
	for i in sp :
		y = int((n * i)/(i - n))
		if (i+y) == (i * y) / n :
			if i not in d :
				d[y] = i
	return len(d)

if __name__ == "__main__" :
	max_so_far = 0
	j = 0
	for i in [3,4,6,12,24,30,60,120,180,210,360,420,720,840,1260,1680,2520,3780,4620] :
		x = doTest(i)
		print i, x, "\r",
		if x > max_so_far :
			max_so_far, j = x, i
			print max_so_far, j, primes.primeFactors(i)

	for i in xrange(j+1, 100*1000*1000) :
		x = doTest(i)
		print i, x, "\r",
		if x > max_so_far :
			max_so_far, j = x, i
			print max_so_far, j, primes.primeFactors(i)
			if max_so_far > 4*1000*1000 : break
	print
	print max_so_far, j
Example #13
0
2991 2991 1992 1.5015060241 [3, 997]
4435 4435 3544 1.25141083521 [5, 887]
20617 20617 20176 1.02185765266 [53, 389]
45421 45421 44512 1.02042145938 [53, 857]
69271 69271 67912 1.02001119095 [53, 1307]
"""

d = {}
min_so_far = 20000
k = 0
for i in xrange(1000*100*95, 10, -1) :
##for i in xrange(1000*1000*10 - 1, 10, -1) :
##for i in xrange(10, 1000*1000*10) :
	print i, k, min_so_far, "\r",
	if not prime_cache.isPrime(i) :
		p = primes.relativePrimes(i)
		phi = len(p)
		a = sorted(c for c in str(i))
		b = sorted(c for c in str(phi))
		if a == b :
			score = float(i)/float(phi)
			d[i] = phi
			if score < min_so_far :
				k, min_so_far = i, score
				print i, k, phi, min_so_far, primes.primeFactors(i)

print
min_score = min(float(k) / float(v) for k,v in d.iteritems())
for k,v in d.iteritems() :
	if v == min_score :
		print k, v
Example #14
0
from primes import primeFactors

max_num = 1000
num_distinct_factors = 4
run_length = 4

current_num = 2
factor_history = []
current_run = 0
while True:
    current_factors = primeFactors(current_num)
    distinct_factors = set(current_factors)

    if len(distinct_factors) == num_distinct_factors:
        current_run += 1
    else:
        current_run = 0

    if current_run == run_length:
        break

    current_num += 1

print(list(range(current_num - run_length + 1, current_num + 1)))
Example #15
0
    for i in sp:
        y = int((n * i) / (i - n))
        if (i + y) == (i * y) / n:
            if i not in d:
                d[y] = i
    return len(d)


if __name__ == "__main__":
    max_so_far = 0
    j = 0
    for i in [
            3, 4, 6, 12, 24, 30, 60, 120, 180, 210, 360, 420, 720, 840, 1260,
            1680, 2520, 3780, 4620
    ]:
        x = doTest(i)
        print i, x, "\r",
        if x > max_so_far:
            max_so_far, j = x, i
            print max_so_far, j, primes.primeFactors(i)

    for i in xrange(j + 1, 100 * 1000 * 1000):
        x = doTest(i)
        print i, x, "\r",
        if x > max_so_far:
            max_so_far, j = x, i
            print max_so_far, j, primes.primeFactors(i)
            if max_so_far > 4 * 1000 * 1000: break
    print
    print max_so_far, j