Ejemplo n.º 1
0
def main():
    
    start = time()
    primeFactorizationDict = {}
    p = Prime()
    firstOfFour = 0
    candidate = 210     # 210 is the product of the smallest 4 unique primes
    while firstOfFour == 0:
        
        if p.factor(candidate):
        
            if candidate not in primeFactorizationDict:
                primeFactorizationDict[candidate] = len(set([prime for prime in p.factorize(candidate)]))
            if primeFactorizationDict[candidate] == PRIME_LIMIT:
                if candidate + 1 not in primeFactorizationDict:
                    primeFactorizationDict[candidate + 1] = len(set([prime for prime in p.factorize(candidate + 1)]))
                if primeFactorizationDict[candidate + 1] == PRIME_LIMIT:
                    if candidate + 2 not in primeFactorizationDict:
                        primeFactorizationDict[candidate + 2] = len(set([prime for prime in p.factorize(candidate + 2)]))
                    if primeFactorizationDict[candidate + 2] == PRIME_LIMIT:
                        if candidate + 3 not in primeFactorizationDict:
                           primeFactorizationDict[candidate + 3] = len(set([prime for prime in p.factorize(candidate + 3)]))
                        if primeFactorizationDict[candidate + 3] == PRIME_LIMIT:
                            firstOfFour = candidate
       
        candidate += 1
    end = time()
    print "First of four integers: ", firstOfFour
    print "Runtime: ", end - start, " seconds. "
Ejemplo n.º 2
0
def getHash(x):
    if type(x) is str:
        s = x
    elif type(x) is int:
        s = str(x)
    elif type(x) is float:
        s = str(int(x * 1000000))
    elif isinstance(x, Hashable):
        return x.getHash()
    else:
        raise Exception('getHash: Hash of  ' + str(x) + ' is unknown')

    if "NewHash" in Params.params:

        if (len(Prime.primes) == 0):
            Prime.init()

        S = 1
        for i in range(len(s)):
            S = addHash(S, Prime.getPrime(256 * i + ord(s[i])))


#			S = addHash( S, Prime.getPrime(i+256) );

#		print "NEW HASH("+s + ")="+str(S)

    else:
        #		print "OLD HASH"

        S = 1
        for i in range(len(s)):
            S = addHash(S, ord(s[i]))

    return S
Ejemplo n.º 3
0
def getHash(x):
	if type(x) is str: 
		s=x;
	elif  type(x) is int: 
		s=str(x);
	elif type(x) is float:
		s=str(int(x*1000000))
	elif isinstance(x,Hashable):
		return x.getHash();
	else:
		raise Exception('getHash: Hash of  ' + str(x) +' is unknown')


	if "NewHash" in Params.params:


		if(len(Prime.primes)==0):
			Prime.init();

		S=1;
		for i in range(len(s)):
			S = addHash( S, Prime.getPrime(256*i + ord(s[i])) );
#			S = addHash( S, Prime.getPrime(i+256) );

#		print "NEW HASH("+s + ")="+str(S)

		
	else:
#		print "OLD HASH"

		S=1;
		for i in range(len(s)):
			S = addHash( S, ord(s[i]) );

	return S;
Ejemplo n.º 4
0
def main():

    start = time()
    primeFactorizationDict = {}
    p = Prime()
    firstOfFour = 0
    candidate = 210  # 210 is the product of the smallest 4 unique primes
    while firstOfFour == 0:

        if p.factor(candidate):

            if candidate not in primeFactorizationDict:
                primeFactorizationDict[candidate] = len(
                    set([prime for prime in p.factorize(candidate)]))
            if primeFactorizationDict[candidate] == PRIME_LIMIT:
                if candidate + 1 not in primeFactorizationDict:
                    primeFactorizationDict[candidate + 1] = len(
                        set([prime for prime in p.factorize(candidate + 1)]))
                if primeFactorizationDict[candidate + 1] == PRIME_LIMIT:
                    if candidate + 2 not in primeFactorizationDict:
                        primeFactorizationDict[candidate + 2] = len(
                            set([
                                prime for prime in p.factorize(candidate + 2)
                            ]))
                    if primeFactorizationDict[candidate + 2] == PRIME_LIMIT:
                        if candidate + 3 not in primeFactorizationDict:
                            primeFactorizationDict[candidate + 3] = len(
                                set([
                                    prime
                                    for prime in p.factorize(candidate + 3)
                                ]))
                        if primeFactorizationDict[candidate +
                                                  3] == PRIME_LIMIT:
                            firstOfFour = candidate

        candidate += 1
    end = time()
    print "First of four integers: ", firstOfFour
    print "Runtime: ", end - start, " seconds. "
Ejemplo n.º 5
0
 def run(self):
     prime = Prime(self.num)
     count = prime.count()
     endAt = int(round(time.time() * 1000)) - self.startAt
     print('Python thread id #%d: executes %d prime numbers in %d ms' % 
         (self.threadId, count, endAt))
Ejemplo n.º 6
0
#!/usr/bin/env python
# Pravin Paratey (http://pravin.insanitybegins.com)
#
# The number, 197, is called a circular prime because all rotations of 
# the digits: 197, 971, and 719, are themselves prime. There are 
# thirteen such primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 
# 73, 79, and 97. How many circular primes are there below one million?
#
# Answer: 55

from Prime import Prime

# Generate prime numbers under 1 million
primes_list = Prime.generate_primes(1000000)
primes_dict = {}
for p in primes_list:
    primes_dict[p] = None

# Just brute force 
# 13 primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, 97
count = 13 
for p in primes_list:
    if p < 100: 
        continue
    is_circular = True

    str_p = str(p)
    for i in xrange(len(str_p)):
        int_p = int(str_p)
        if not primes_dict.has_key(int_p):
            is_circular = False
Ejemplo n.º 7
0
 def __init__(self):
     Prime.__init__(self)
Ejemplo n.º 8
0
#!/usr/bin/env python
# Pravin Paratey (http://pravin.insanitybegins.com)
#
# The number, 197, is called a circular prime because all rotations of
# the digits: 197, 971, and 719, are themselves prime. There are
# thirteen such primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71,
# 73, 79, and 97. How many circular primes are there below one million?
#
# Answer: 55

from Prime import Prime

# Generate prime numbers under 1 million
primes_list = Prime.generate_primes(1000000)
primes_dict = {}
for p in primes_list:
    primes_dict[p] = None

# Just brute force
# 13 primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, 97
count = 13
for p in primes_list:
    if p < 100:
        continue
    is_circular = True

    str_p = str(p)
    for i in xrange(len(str_p)):
        int_p = int(str_p)
        if not primes_dict.has_key(int_p):
            is_circular = False
Ejemplo n.º 9
0
from Prime import Prime
import math
import time

PAGE = 1_0000
REPEAT = 1_000

prime = Prime(PAGE * REPEAT)


def prime_by_eratosthenes_interval(pos, limit):
    top = 0
    num = [True] * limit
    for i in range(0, prime.size()):
        p = prime.get(i)
        if p * p >= pos + limit: break
        for j in range(math.ceil(pos / p), int((pos + limit - 1) / p) + 1):
            num[j * p - pos] = False

    for i in range(0, limit):
        if num[i]:
            prime.add(pos + i)
            top = top + 1
    return top


def prime_by_euler(limit):
    top = 0
    num = [True] * limit
    for i in range(2, limit):
        if num[i]:
Ejemplo n.º 10
0
from Prime import Prime

print ("Which number you want to test is prime or not")
num = int(input())
p1 = Prime(num)
print ("%d is prime?"% num)
print (p1.isPrime())
Ejemplo n.º 11
0
'''
Write a code to check whether no is prime or not. Condition use function check()
to find whether entered no is positive or negative ,if negative then enter the no, And if
yes pas no as a parameter to prime() and check whether no is prime or not?
Whether the number is positive or not, if it is negative then print the message “please
enter the positive number”
It is positive then call the function prime and check whether the take positive number is
prime or not
'''
from Prime import Prime
n = int(input())
print(Prime.isPrime(n))
Ejemplo n.º 12
0
'''
Created on 2014. 4. 2.

Summation of primes
Problem 10
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.

Find the sum of all the primes below two million(2,000,000).


Solution: 142913828922

@author: Ungsik Yun
'''

if __name__ == '__main__':
    from Prime import Prime
    p = Prime()
    l = p.list_under_improved(2000000)
    print sum(l)