def main():
	# for all prime**4ths
	# for all primes**3s
	# if there's a prime**2 add one
	lim = 50000000
	num_list = [False] * lim
	prime_list = prime_sieve( int(lim**0.5) )
	for a in prime_list:
		if a**4 > lim: 
			print "Done at %d" % a
			break
		for b in prime_list:
			if a**4 + b**3 > lim: break
			for c in prime_list:
				stdout.write("%2d, %5d, %10d, %d\r" %(a,b,c,a**4+b**3+c**2))
				stdout.flush()
				num = a**4 + b**3 + c**2 
				if num > lim: break
#				print "%d^4+%d^3+%d^2=%d" %(a,b,c,a**4+b**3+c**2)
				num_list[num] = True
	count = 0
	for n in num_list:
		if n:
			count += 1
	print "There are %d" % count
Esempio n. 2
0
def primes_less_then(n):
    global prime_list
    block = 100000
    for i in count(1):
        if i >= len(prime_list):
            prime_list.extend(prime_sieve(block, prime_list))
        if prime_list[i] > n: break
        yield prime_list[i]
def main():
    count = 1
    n_primes = 0
    primes = prime_sieve(100000)
    for s in spiral_gen():
        if s**0.5 > primes[-1]:
            logging.debug("extending {}: {}".format(primes[-1], s))
            primes.extend(prime_sieve(100000, primes))
        if is_prime(s,primes):
            n_primes += 1
            logging.debug("new prime {}".format(s))
        count += 1
        if float(n_primes)/count < 0.1:
            side_len = 2 * ((count + 2)// 4) + 1
            print("Prime ratio is {:5.4f} when the side length is {}".format(
                100 * float(n_primes)/count, side_len))
            return None
def primes_less_then(n):
	global prime_list
	block = 100000
	for i in count(1):
		if i >= len(prime_list):
			prime_list.extend(prime_sieve(block, prime_list))
		if prime_list[i] > n: break
		yield prime_list[i]
def main():
    block = 10000
    n = 3
    lim = 5000000
    prime_list = prime_sieve(block)
    p_bar = ProgressBar(maxval=lim).start()
    while True:
        ways = changes(n, prime_list)
        #		print "%d: %d: %s" %(n,ways,str(prime_list))
        if ways > lim:
            p_bar.finish()
            print "%d can be written over %d ways" % (n, lim)
            return
        p_bar.update(ways)
        n += 1
        if prime_list[-1] < n:
            new_primes = prime_sieve(i + block, prime_list)
            prime_list.append(new_primes)
def main():
	block = 10000
	n = 3
	lim = 5000000
	prime_list = prime_sieve(block)
	p_bar = ProgressBar(maxval=lim).start()
	while True:
		ways = changes(n,prime_list)
#		print "%d: %d: %s" %(n,ways,str(prime_list))
		if ways > lim:
			p_bar.finish()
			print "%d can be written over %d ways" %(n,lim)
			return
		p_bar.update(ways)
		n += 1
		if prime_list[-1] < n:
			new_primes = prime_sieve(i+block,prime_list)
			prime_list.append(new_primes)
Esempio n. 7
0
def main():
    N = 4000000
    primes = prime_sieve(100)
    m = int(log(2 * N, 3)) + 1
    factors = [1] * m
    best = (quick_num(factors, primes), factors)
    done = [factors]
    for i in reversed(range(m)):
        queue = done
        done = set()
        for f in queue:
            for new in replace(f[:], primes, i, N):
                done.add(new)
                s = quick_num(new, primes)
                best = min(best, (s, new))
    print "%d: %d" % (number(best[1], primes), solutions(best[1]))
def circular_primes():
    sieve = set(common.prime_sieve(1000000))
    num_circular = 0
    for prime in sieve:
        digits = common.get_num_digits(prime)
        circular = True
        for i in xrange(digits):
            first = prime / 10**(digits - 1)
            rest = prime % 10**(digits - 1)
            prime = rest * 10 + first
            if prime not in sieve:
                circular = False
                break
        if circular:
            num_circular += 1
    return num_circular
def main():
	N = 4000000
	primes = prime_sieve(100)
	m = int(log(2*N,3)) + 1
	factors = [1] * m
	best = (quick_num(factors, primes), factors)
	done = [factors]
	for i in reversed(range(m)):
		queue = done
		done = set()
		for f in queue:
			for new in replace(f[:], primes, i, N):
				done.add(new)
				s = quick_num(new, primes)
				best = min(best, (s, new))
	print "%d: %d" %(number(best[1], primes), solutions(best[1]))
Esempio n. 10
0
def truncatable_primes():
    primes = common.prime_sieve(1000000)
    prime_set = set(primes)

    # under_10 = set(primes[:4])
    primes = primes[4:]  # Ignore single digits primes

    # Filter primes where first and last digit are not prime
    # primes = filter(lambda x: first_last_digit_prime(x, under_10), primes)

    truncatable_sum = 0

    for p in primes:
        if truncate(p, prime_set):
            truncatable_sum += p

    return truncatable_sum
Esempio n. 11
0
def main():
    big_num = 100000
    primes = prime_sieve(int(big_num**0.5+1))
    # Find distinct prime factors
    nums = [set([x]) for x in range(big_num+1)]
    for n in range(1,big_num+1):
        if n in primes: continue
        for p in primes:
            if n % p == 0:
                nums[n] = set([p]) | nums[n//p]
                break
            elif n**0.5 < p: break
    # Calculate rad from distinct prime factors
    for i, fact in enumerate(nums[1:], start=1):
        nums[i] = (prod(fact), i)
    nums.pop(0)
    # sort
    nums.sort()
    # print
    print "E(10000)=%d" %(nums[10000-1][1])
Esempio n. 12
0
def main():
    big_num = 100000
    primes = prime_sieve(int(big_num**0.5 + 1))
    # Find distinct prime factors
    nums = [set([x]) for x in range(big_num + 1)]
    for n in range(1, big_num + 1):
        if n in primes: continue
        for p in primes:
            if n % p == 0:
                nums[n] = set([p]) | nums[n // p]
                break
            elif n**0.5 < p:
                break
    # Calculate rad from distinct prime factors
    for i, fact in enumerate(nums[1:], start=1):
        nums[i] = (prod(fact), i)
    nums.pop(0)
    # sort
    nums.sort()
    # print
    print "E(10000)=%d" % (nums[10000 - 1][1])
Esempio n. 13
0
#!/usr/bin/python

from common import split_num, prime_sieve


result = 0
sieve = prime_sieve(87654321)

for i in range(2, len(sieve)):
    if sieve[i] == False:
        continue
    nums = split_num(i)
    if sorted(nums) == list(range(1, len(nums) + 1)):
        if i > result:
            result = i

print(result)
Esempio n. 14
0
#!/usr/bin/python

from common import prime_sieve, factors


def prime_factors(n, sieve):
    f = factors(n)
    f.remove(1)
    f.remove(n)
    return [i for i in f if sieve[i]]


result = 0
sieve = prime_sieve()

for i in range(647, 1000000):
    found = True
    for j in range(4):
        n = i + j
        pf = prime_factors(n, sieve)
        if len(pf) != 4:
            found = False

    if found is True:
        result = i
        break

print(result)
Esempio n. 15
0
 def test_truncate_3792(self):
     self.assertFalse(truncate(3792, prime_sieve(1000000)))
Esempio n. 16
0
 def test_truncate_3797(self):
     self.assertTrue(truncate(3797, prime_sieve(1000000)))
Esempio n. 17
0
 def test_truncate_748317(self):
     self.assertFalse(truncate(748317, prime_sieve(1000000)))
Esempio n. 18
0
 def test_truncate_311(self):
     self.assertFalse(truncate(311, prime_sieve(1000000)))