예제 #1
0
def check(n):
        if primes.isprime(n): return (n+2)
        
        x=0
        while primes.prime(x)<n:
                square=math.sqrt((n-primes.prime(x))/2.0)
                if square==int(square):
                        return (n+2)
                x+=1
        print n
예제 #2
0
def nextPrime(lastPrimeIndex,depth,prime):
        for i in range(lastPrimeIndex-1,depth-1,-1):
                prime[NO_NEEDED-1-depth]=primes.prime(i)
                if depth==1:
                        yield prime
                else:
                        yield nextPrime(i, depth-1,prime)
예제 #3
0
파일: rsa.py 프로젝트: ekalosak/encryption
def make_keys(n_primes = 100, pq_bound = 100):
    '''
    takes: int n
    returns: tuples_int pub, prv
    '''

    n, p, q = 1, 1, 1
    prms = primes.prime(n_primes)
    while math.log(n, 2) <= 1:
        while n < pq_bound:
            p, q = np.random.choice(prms, size = 2, replace = False)
            n = p * q
    print "Key bit length: {}".format(math.log(n, 2))
    print "Generated by p: {} and q: {}".format(p, q)

    t = (p - 1) * (q - 1) # =: |{k | k < n && (k, n) == 0}|
    print "Euler's totient function of {}x{}={}: {}".format(p, q, n, t)

    e = t
    while primes.gcd(e, t) != 1 or e == 1:
        e = int(np.random.uniform(low = 1, high = t))
    print "Public key: {}".format(e)

    d = primes.inv(e, t)
    print "Private key: {}".format(d)

    return (n, e), (n, d)
예제 #4
0
파일: p118.py 프로젝트: jokkebk/euler
def ok(s, prev=0):
    if not s: return 1
    v, w = 0, 0
    for i,d in enumerate(s):
        v = v*10 + d
        if v > prev and prime(v):
            w += ok(s[i+1:], v)
    return w
예제 #5
0
파일: prime_serv.py 프로젝트: tuipik/ITEA
def prime_handler(conn, addr):
    with conn:
        while True:
            data = conn.recv(BUFSIZE)
            if not data:
                break
            n = int(data)
            p = prime(n)
            conn.sendall(str(p).encode() + b'\n')
예제 #6
0
def find():
    primes = primes_list(10**4)
    for length in xrange(1000, 0, -1):
        first = 0
        while True:
            summ = sum(primes[first:first+length])
            # print summ
            if summ >= 10**6: break
            if prime(summ): return summ
            first += 1
예제 #7
0
#!/usr/bin/env python2
import itertools
import primes


def conjecture(n):
    for i in xrange(int((n / 2) ** 0.5), 0, -1):
        if primes.prime(n - 2 * i ** 2):
            return True
    return False


for number in itertools.count(33, 2):
    if primes.prime(number):
        continue
    if not conjecture(number):
        break

print "result: %s" % number
예제 #8
0
import primes

s = [primes.prime(x) for x in range(50)]

def find(target):
        ways = [1]+[0]*target
        
        for s_i in range(1,target):
                if s_i < target:
                        for i in range(s_i,target+1):
                                ways[i] += ways[i - s_i]
                                
                                if (ways[s_i]+1)%10**6==0:
                                        return s_i+1
                                


print find(10**6)           
예제 #9
0
파일: euler049.py 프로젝트: hasanain/euler
from primes import is_prime as prime
p = [
    x for x in range(1000, 10000)
    if prime(x) and prime(x + 3330) and prime(x + 2 * 3330)
]


def isperm(n1, n2):
    return sorted(list(str(n1))) == sorted(list(str(n2)))


def isperml(n):
    n1 = sorted(list(str(n[0])))
    for i in n:
        if n1 != sorted(list(str(i))):
            return False
    return True


for i in p:
    if isperml([i, i + 3330, i + 2 * 3330]):
        print(str(i) + str(i + 3330) + str(i + 2 * 3330))
예제 #10
0
파일: euler049.py 프로젝트: hjamal/euler
from primes import is_prime as prime
p = [x for x in range(1000, 10000) if prime(x) and prime(x + 3330) and prime(x + 2*3330)]

def isperm(n1, n2):
    return sorted(list(str(n1))) == sorted(list(str(n2)))
def isperml(n):
    n1 = sorted(list(str(n[0])))
    for i in n:
        if n1 != sorted(list(str(i))):
            return False
    return True

for i in p:
    if isperml([i, i+3330, i+2*3330]):
        print(str(i) + str(i+3330) + str(i + 2*3330))
예제 #11
0
                        # list2=list(prime)
                        # count=0
                        # for k in range(10):
                                # list2[i+1]=list2[j]=k
                                # if primes.isprime(listToNum(list2)) and listToNum(list2)>999:
                                        # count+=1
                                        # if count==Amount:
                                                # print listToNum(prime)
                                                # contin=0
        # x+=1
        
##for 3 digits
contin=1
x=25
while contin:
        prime=[int(i) for i in str(primes.prime(x))]
        for i in range(len(prime)-2):
                for j in range(i+2):
                        for jj in range(j+1):
                                # if jj==0:
                                        # am=Amount+1
                                # else
                                        
                                list2=list(prime)
                                count=0
                                for k in range(10):
                                        list2[i+2]=list2[j+1]=list2[jj]=k
                                        if primes.isprime(listToNum(list2)) and list2[0]!=0:
                                                count+=1
                                                if count==Amount:
                                                        print listToNum(prime)
예제 #12
0
import time
startTime=time.clock()

import primes
n=4
count=0
sum =0
tprimes=[]

while count < 1000000000000000:
        primeList=[int(x) for x in str(primes.prime(n))]
        good=1
        for x in range(1,len(primeList)):
                primeL=0
                primeR=0
                
                for i in range(len(primeList)-x):
                        # print primeList
                        primeL+=(10**(len(primeList)-x-i-1))*primeList[x+i]
                        primeR+=(10**(len(primeList)-x-i-1))*primeList[i]
                        # print primeR
                if not (primes.isprime(primeL) and primes.isprime(primeR)):
                        good=0
                        break
        if good:
                tprimes.append(primes.prime(n))
                count +=1
                sum += primes.prime(n)
                print tprimes
                print sum
        
예제 #13
0
파일: p146.py 프로젝트: jokkebk/euler
from primes import initpoulet, prime

initpoulet()

add = (1, 3, 7, 9, 13, 27)
S = 10

for n in range(11, 150000000):
    if n%100000==0: print(n, S)
    if all(prime(n*n+a) for a in add): S += n

print(S)
#print(list(prime(n*n+a) for a in add))
예제 #14
0
def isPerm(x,y):
        x = str(x)
        y = str(y)
        if len(x)!=len(y):
                return False
        for ch in x:
                if x.count(ch)!=y.count(ch):
                        return False
        return True

maxim = 10**7      
minNOverPhy = 10**9


x=2
while primes.prime(x)<maxim:
        for y in range(1,x):
                if primes.prime(y)*primes.prime(x) > maxim:
                        break
                nOverPhy = float(primes.prime(x)*primes.prime(y))/float((primes.prime(x)-1)*(primes.prime(y) -1))
                if minNOverPhy > nOverPhy:
                        if isPerm(primes.prime(y)*primes.prime(x),(primes.prime(x)-1)*(primes.prime(y) -1) ):
                                minNOverPhy = nOverPhy
                                print primes.prime(y)*primes.prime(x), nOverPhy
                        
                        
        x+=1
        

예제 #15
0
def conjecture(n):
    for i in xrange(int((n / 2) ** 0.5), 0, -1):
        if primes.prime(n - 2 * i ** 2):
            return True
    return False
예제 #16
0
import time
import primes
startTime=time.clock()

n=0
MAX=10**6
noPrimes=0
x=0

while primes.prime(x)<MAX/2:
        sum=primes.prime(x)
        n=1
        while sum<MAX:
                sum+=primes.prime(x+n)
                n+=1
                if primes.isprime(sum):
                        if n>noPrimes:
                                noPrimes=n
                                print noPrimes
                                print sum
        x+=1

endTime=time.clock()
print endTime-startTime
예제 #17
0
# project euler 07

from primes import prime

print prime(10001)