예제 #1
0
def find_all_pairs(a):
    for p in primes:
        if p <= a:
            continue
        if p > a and is_prime(int(str(a) + str(p))) and is_prime(
                int(str(p) + str(a))):
            yield p
예제 #2
0
 def count_answer(self):
     n = 1
     num = 1
     while n != self.limit:
         num += 2
         if is_prime(num):
             n += 1
     return num
예제 #3
0
def factor(n, cache):
    nn = n
    if cache[n] != -1:
        return cache[n]
    for i in primes:
        while n % i == 0 and n != i:
            n = n // i
        if is_prime(n):
            break
    cache[nn] = n
    return n
예제 #4
0
def cycles_of(s):
    list_of_cycles = []
    s = str(s)
    n = str(s)
    """
    shifts the sequence to the left to cycle them
    """
    for i in range(0,len(n)):
        n = n[1:] + n[0]
        if is_prime(int(n)):
            list_of_cycles.append(n)
        else: return False
    if len(list_of_cycles) == len(str(s)):
        return list_of_cycles
    else: return False
예제 #5
0
from Prime import is_prime

result = []
num = 13
while len(result) < 11:
    truncprime = True
    if num % 10 == 1:
        num += 2
    for i in range(len(str(num))):
        num1, num2 = num, num
        if i > 0:
            num1 = int(str(num)[:-i])
            num2 = int(str(num)[i:])
        if (not is_prime(num1)) or (not is_prime(num2)):
            truncprime = False
            break
    if truncprime:
        result.append(num)
    num += 4

print(sum(result))
예제 #6
0
from Prime import is_prime
from itertools import permutations


def permute(num):
    ls = set()
    num = str(num)
    for i in range(len(num)):
        ls.add(num[i])
    nums = list(permutations(num))

    ls = set()
    for n in nums[1:]:
        item = ''
        for x in n:
            item += x
        if item[0] != '0':
            ls.add(int(item))
    return ls


for num in range(1001, 9999, 2):
    if is_prime(num):
        ls = permute(num)
        for n in ls:
            diff = n - num
            if is_prime(n) and is_prime(n + diff) and (n + diff
                                                       in ls) and diff > 0:
                print(str(num) + str(n) + str(n + diff))
예제 #7
0
                l = 0
                m = 0
                while l < digits:
                    if l != j and l != k and l != o:
                        cek[l] = str(i)[m]
                        m += 1
                    l += 1

                cnt = 0
                for n in range(10):
                    cek[j] = str(n)
                    cek[k] = str(n)
                    cek[o] = str(n)
                    cekint = int(''.join(cek))
                    if len(str(cekint)) == digits:
                        if not is_prime(cekint):
                            cnt += 1
                    else:
                        cnt += 1

                    if cnt > (10 - target):
                        break

                if cnt == (10 - target):
                    for x in range(digits):
                        if x == j or x == k or x == o:
                            print('1', end='')
                        else:
                            print(cek[x], end='')
                    print()
                    exit()
예제 #8
0
n = 1000000
#n = 100



# rule out any prime which could contains an even number because its 
# has a permutation which is even.
kEvenDigits = ['0', '2', '4', '6', '8']
def primeDoesntContainEvenDigit(number_str):
    # if its prime and a single digit return (2 is still prime)
    if len(number_str) == 1: return True
    for even in kEvenDigits:
        if even in number_str: return False
    return True

all_potential_primes = set([str(i) for i in range(n) if primeDoesntContainEvenDigit(str(i)) and is_prime(i)])

#all_potential_primes = set([prime for prime in all_primes if primeDoesntContainEvenDigit(prime)])

#all_potential_primes = set(all_primes)

print len(all_potential_primes)


solutions = []

def setPeek(s):
    elem = s.pop()
    s.add(elem)
    return elem
예제 #9
0
def strong_harshad(num):
    if is_harshad(num):
        return is_prime(num // sum_of_digits(num))
예제 #10
0
from Prime import is_prime
print(sum(i for i in range(3, 2000000, 2) if is_prime(i)) + 2)
예제 #11
0
파일: P010.py 프로젝트: joeharney1985/Euler
#
# The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
#
# Find the sum of all the primes below two million.

from Prime import is_prime

# starting summation at 2 because thats the first prime
summation = 2

# starting i at 3 so we do not have to check all the even number to see if they
# are prime.
i = 3
while i < 2000000:
  if is_prime(i):
    summation += i
  i += 2
  
print summation
예제 #12
0
# x*x - d*y*y = 1
# y = sqrt(x*x - 1/d)
# x = sqrt(1+d*y*y)

from math import sqrt
from Prime import is_prime

limit = 100
max = 0
ans = []

for d in range(1, limit + 1, 2):
    y = 1
    if int(sqrt(d)) == sqrt(d) or not is_prime(d):
        continue
    found = False
    while not found:
        # y = sqrt((x*x - 1)/d)
        x = sqrt(1 + d * y * y)

        if int(x) == x:
            found = True
            if x > max:
                max = x

        y += 1
    print(d, x)

print(max)
예제 #13
0
 def __init__(self, limit=20, answer=0):
     self.limit = limit
     self.primes = list(n for n in range(self.limit) if is_prime(n))
     self.jump = self.mult(self.primes)
     self.answer = self.count_answer()
예제 #14
0
from Prime import is_prime
import time


def is_circular(num):
    for _ in range(len(str(num)) - 1):
        num = int(str(num)[-1] + str(num)[:-1])
        if not is_prime(num):
            return False
    return True


t1 = time.time()
limit = 1000000
ans = 0
for num in range(limit):
    if is_prime(num) and is_circular(num):
        ans += 1
print(ans)
t2 = time.time()

print(t2 - t1, " seconds")
예제 #15
0
def find_all_pairs(a):
    for p in primes:
        if p <= a:
            continue
        if p > a and is_prime(int(str(a) + str(p))) and is_prime(
                int(str(p) + str(a))):
            yield p


t1 = time.time()

upper = 10000
primes = set()
for i in range(upper):
    is_prime(i, primes)
primes = sorted(primes)

ans = upper * 5
pairs = {}
for i in range(len(primes)):
    pairs[primes[i]] = set()

for a in primes:
    if a * 5 >= ans:
        break
    if len(pairs[a]) == 0:
        pairs[a] = set(find_all_pairs(a))

    for b in primes:
        if a + b * 4 >= ans:
예제 #16
0
def is_composite(num):
    return num > 1 and not is_prime(num, cache)
예제 #17
0
from socket import *       # Contains everything you need to set up sockets
from Prime import is_prime

# Create server port address and socket
server_port = 400
server_socket = socket(AF_INET, SOCK_DGRAM)     # Uses IPV4. SOCK_DGRAM indicates UDP

# Assign the port number server_port to the server's socket
server_socket.bind(('', server_port))

print "The server is ready to receive..."

# While loop to continuously to constantly be available for messages
while 1:
    # Store message and client address
    message, client_address = server_socket.recvfrom(2048)

    # Check if message is prime
    num_to_check = int(message)     # Cast data to integer

    if is_prime(num_to_check):
        result = "Number is prime! :-)"
    else:
        result = "Number is NOT prime. :-("

    # Send message back to client using client address
    server_socket.sendto(result, client_address)
예제 #18
0
from itertools import permutations
from Prime import is_prime


digit = 9
found = False
while not found:
    num = list(range(1, digit + 1))
    allnum = list(permutations(num))

    for i in range(1, len(allnum) + 1):
        x = allnum[-i]
        t = ''
        for n in range(len(x)):
            t += str(x[n])
        t = int(t)
        if is_prime(t):
            print(t)
            found = True
            break

    digit -= 1
예제 #19
0
def check(num):
    global count
    if is_prime(abs(n - num)):
        count += 1
예제 #20
0
from math import ceil
from Prime import is_prime

limit = 50000000
cand = set()

for x4 in range(2, ceil(limit**(1 / 4))):
    if not is_prime(x4):
        continue
    x4pow = x4**4
    for x3 in range(2, ceil((limit - x4pow)**(1 / 3))):
        if not is_prime(x3):
            continue
        x3pow = x3**3
        for x2 in range(2, ceil((limit - x4pow - x3pow)**(1 / 2))):
            if not is_prime(x2):
                continue
            x2pow = x2**2
            cand.add(x2pow + x3pow + x4pow)

print(len(cand))
예제 #21
0
from Prime import is_prime


def mult(ls):
    ans = 1
    for item in ls:
        ans *= item
    return ans


limit = 20
primes = list(n for n in range(limit) if is_prime(n))
jump = mult(primes)

num = 0
found = False
while not found:
    found = True
    num += jump
    for i in range(2, limit + 1):
        if int(num / i) != num / i:
            found = False
            break
print("Done! Answer:", num)
예제 #22
0
        if len(ls[i]) != n:
            return False
    return True


def isecall(ls):
    if len(ls) == 1:
        return set()
    return ls[0].intersection(isecall(ls[1:]))


n = 4

primes = []
for i in range(10**(n + 1)):
    if is_prime(i):
        primes.append(i)
print(primes[-10:])

facs = []
for i in range(1, n + 1):
    facs.append(factorize(i))

cur = n
while True:
    # print(cur, facs)
    if len(isecall(facs)) == 0 and equallen(facs, n):
        print(cur - n)
        break
    facs[cur % n] = factorize(cur)
    cur += 1
예제 #23
0
from Prime import is_prime

n = 1
num = 1
while n != 10001:
    num += 2
    if is_prime(num):
        n += 1
print(num)
예제 #24
0
from Prime import is_prime

r = 0
a = 1
n = 1
limit = 10
while r <= 10**limit:
    found = False
    while not found or n % 2 == 0:
        a += 2
        if is_prime(a):
            n += 1
            if n > 21000:
                found = True
    r = ((a - 1)**n + (a + 1)**n) % (a * a)

print("Done! Answer:", n)
예제 #25
0
def strong_right_truncatable_harshard(num):
    return is_prime(num) and strong_harshad(
        num // 10) and right_truncatable_harshad(num // 10)
예제 #26
0
def is_circular(num):
    for _ in range(len(str(num)) - 1):
        num = int(str(num)[-1] + str(num)[:-1])
        if not is_prime(num):
            return False
    return True
예제 #27
0
from Prime import is_prime
from math import sqrt, ceil

x = 8

cnt = 1 + 4
for i in range(2, 10**x, 2):
    if i % 100000 == 0:
        print(i)
    if is_prime(i + 1) and is_prime(i // 2 + 2):
        jago = True
        for j in range(2, ceil(sqrt(i))):
            if i % j == 0 and not is_prime(i // j + j):
                jago = False
                break
        if jago:
            # print(i)
            cnt += i
print(cnt)
예제 #28
0
# Bind the server port address to the socket to give it an identity
server_socket.bind(('', server_port))

# Tells socket to listen for TCP connection requests from the client
server_socket.listen(1)  # Maximum number of connections given by parameter "1"

print "The server is ready to receive"

while 1:
    # Creates another socket dedicated for the pipe between client and server
    connection_socket, client_address = server_socket.accept()

    # Store data from client
    number = connection_socket.recv(1024)

    # Cast number to an int
    number = int(number)

    # Check if number from client is prime
    if is_prime(number):
        result = "Number is prime! :-)"
    else:
        result = "Number is NOT prime. :-("

    # Send data back to client
    connection_socket.send(result)

    # Close pipe connection between client and server. Note that the server can still accept new connections from the
    # initial socket
    connection_socket.close()