def checkDSAparams(p, q, g):
    warnings.simplefilter('ignore')
    check = pyprimes.is_prime(q)
    warnings.simplefilter('default')
    if check == False:
        return -1

    warnings.simplefilter('ignore')
    check = pyprimes.is_prime(p)
    warnings.simplefilter('default')
    if check == False:
        return -2

    r = (p - 1) % q
    if (r != 0):
        return -3

    k = (p - 1) / q

    x = pow(g, k, p)
    if (x == 1):
        return -4
    y = pow(g, q, p)
    if (y != 1):
        return -4

    return 0
예제 #2
0
def add_test(prime_num):
    str_prime_num = str(prime_num)

    for i in range(1, len(str_prime_num)):
        right = pyprimes.is_prime(int(str_prime_num[0:i]))
        left = pyprimes.is_prime(int(str_prime_num[i:len(str_prime_num)]))
        if right and left:
            continue
        else:
            return

    list.append(prime_num)
예제 #3
0
파일: tests.py 프로젝트: uzumaxy/pyprimes
 def test_primes_end_is_exclusive(self):
     # End argument to primes() is exclusive.
     n = 211
     assert pyprimes.is_prime(n)
     it = pyprimes.primes(end=n)
     values = list(it)
     self.assertEqual(values[-1], 199)
     assert pyprimes.next_prime(199) == n
예제 #4
0
파일: tests.py 프로젝트: cesarfm/pyprimes
 def test_primes_end_is_exclusive(self):
     # End argument to primes() is exclusive.
     n = 211
     assert pyprimes.is_prime(n)
     it = pyprimes.primes(end=n)
     values = list(it)
     self.assertEqual(values[-1], 199)
     assert pyprimes.next_prime(199) == n
예제 #5
0
def check_jam_coin(n):
	for base in range(2,10+1):
		new_n = int(n, base)
		warnings.simplefilter('error',RuntimeWarning)
		try:
			if pyprimes.is_prime(new_n):
				return False
		except RuntimeWarning:
			print "Warned", new_n
			return False
	return True
예제 #6
0
파일: DSA.py 프로젝트: cryptobuks1/crycur
def _generate_prime(multi_q, lower_bound, upper_bound, mult=1, shift=0):
    try:
        while True:
            q = randint(lower_bound, upper_bound) * mult + shift
            if _first_prime_check(q):
                with warnings.catch_warnings():
                    warnings.simplefilter('ignore')
                    if is_prime(q):
                        multi_q.put(q)
                        break
    except KeyboardInterrupt:
        multi_q.put(None)
예제 #7
0
파일: e357b.py 프로젝트: orenovadia/euler
def dive(l,idx):
    global sun,N,mul
    if mymul(l)>N: return
    for i,p in enumerate(l):
        pr =  ( mymul([x for x in l if x!=p]) )
        if not  pyprimes.is_prime(p+pr):
            #l.pop(-1)
            #okay = False
            return
    #if l and okay:
    if l: 
        if len(l)>1:
            log.write( '%s %s \n'% (p*pr,l) )
            sun+=p*pr
            assert prod(l)<N
    for i in xrange(lenpp-idx):
        if prod(l+ [pp[idx+i] ] )>N: return
        dive(l+ [pp[idx+i]  ,],idx+i+1)
예제 #8
0
def read_file():
    f = open("C-small-attempt0.in", 'r')
    num_case = int(f.readline())

    for n in range(num_case):
        read_in = f.readline().split()
        num_len = int(read_in[0])
        num_jam = int(read_in[1])

        max_num = 1
        for i in range(num_len - 1):
            max_num = (max_num * 10) + 1

        min_num = 1
        for i in range(num_len - 1):
            min_num = (min_num * 10)
        min_num += 1

        int_to_list = lambda num: map(int, str(
            num))  #changes an int to a list of single digits

        jamcoin_dict = {}
        for jamcoin in range(numtoBase(int_to_list(min_num), 2),
                             numtoBase(int_to_list(max_num), 2) + 1):
            flag = False
            jamcoin = list_to_int(toDigits(jamcoin, 2))
            if jamcoin % 10 == 0:
                flag = True
            else:
                divisors = []

                for i in xrange(2, 11):
                    dec_num_value = numtoBase(int_to_list(jamcoin), i)
                    if pyprimes.is_prime(dec_num_value):
                        flag = True
                        break
                    divisors.append(str(simple_div(dec_num_value)[0]))

            if len(jamcoin_dict) >= num_jam:
                break
            elif not flag:
                jamcoin_dict[jamcoin] = divisors

        print_result(n + 1, jamcoin_dict)
예제 #9
0
파일: e249.py 프로젝트: orenovadia/euler
def run(N=100):
    prm = primes3(N)
    nprm = len(prm)
    countNo=0
    countYes=0
    l=[]
    for i,j,k in combinations(prm,3):
        p = i+j+k 
        if p in prm:
            countYes+=1
            l.append(p)
        elif is_prime(p):
            countYes+=1
            l.append(p)
        else:
            countNo+=1
    print countNo,countYes
    l = sorted(l)
    for key,j in groupby(l):
        print key,'\t',len( list(j))
    print l
    print len(l),len(set(l))
예제 #10
0
파일: factors.py 프로젝트: uzumaxy/pyprimes
def factors(n):
    """factors(integer) -> yield factors of integer lazily

    >>> list(factors(3*7*7*7*11))
    [(3, 1), (7, 3), (11, 1)]

    Yields tuples of (factor, count) where each factor is unique and usually
    prime, and count is an integer 1 or larger.

    The factors are prime, except under the following circumstances: if the
    argument n is negative, -1 is included as a factor; if n is 0 or 1, it
    is given as the only factor. For all other integer n, all of the factors
    returned are prime.
    """
    if n in (0, 1, -1):
        yield (n, 1)
        return
    elif n < 0:
        yield (-1, 1)
        n = -n
    assert n >= 2
    from pyprimes import primes
    for p in primes():
        if p * p > n: break
        count = 0
        while n % p == 0:
            count += 1
            n //= p
        if count:
            yield (p, count)
    if n != 1:
        if __debug__:
            # The following test only occurs if assertions are on.
            if _EXTRA_CHECKS:
                from pyprimes import is_prime
                assert is_prime(n), ('final factor %d is not prime' % n)
        yield (n, 1)
예제 #11
0
파일: factors.py 프로젝트: cesarfm/pyprimes
def factors(n):
    """factors(integer) -> yield factors of integer lazily

    >>> list(factors(3*7*7*7*11))
    [(3, 1), (7, 3), (11, 1)]

    Yields tuples of (factor, count) where each factor is unique and usually
    prime, and count is an integer 1 or larger.

    The factors are prime, except under the following circumstances: if the
    argument n is negative, -1 is included as a factor; if n is 0 or 1, it
    is given as the only factor. For all other integer n, all of the factors
    returned are prime.
    """
    if n in (0, 1, -1):
        yield (n, 1)
        return
    elif n < 0:
        yield (-1, 1)
        n = -n
    assert n >= 2
    from pyprimes import primes
    for p in primes():
        if p*p > n: break
        count = 0
        while n % p == 0:
            count += 1
            n //= p
        if count:
            yield (p, count)
    if n != 1:
        if __debug__:
            # The following test only occurs if assertions are on.
            if _EXTRA_CHECKS:
                from pyprimes import is_prime
                assert is_prime(n), ('final factor %d is not prime' % n)
        yield (n, 1)
예제 #12
0
파일: tests.py 프로젝트: uzumaxy/pyprimes
 def test_primes_start_is_inclusive(self):
     # Start argument to primes() is inclusive.
     n = 211
     assert pyprimes.is_prime(n)
     it = pyprimes.primes(start=n)
     self.assertEqual(next(it), n)
예제 #13
0
 def is_prime(self):
     return pyprimes.is_prime(self.__num)
예제 #14
0
def prime_checker(num):
    ''' This will check to see if a given number is prime '''
    if pyprimes.is_prime(num):
        print 'It is prime!'
    else:
        print 'Nope! Try again.'
예제 #15
0
파일: main.py 프로젝트: mjeromin/ddev-labs
def prime_test(n):
    if is_prime(n):
        return "This is prime: {}\n".format(n)
    else:
        return "This is not so prime: {}\n".format(n)
예제 #16
0
t = int(_input.readline())  # read a line with a single integer
for i in xrange(1, t + 1):
    N, J = [int(s) for s in _input.readline().split(" ")
            ]  # read a list of integers, 2 in this case
    print N, " et ", J
    assert (N >= 2)
    current = "1" + "0" * (N - 2) + "1"
    number_of_found = 0

    _output.write("Case #{}:\n".format(i))
    divisors = []
    while (number_of_found < J):
        for base in range(2, 11):
            number_in_base = int(current, base)
            print current
            if pyprimes.is_prime(number_in_base):
                current = str(bin(int(current, 2) + 2))[2:]
                divisors = []
                break
            else:
                divisors.append(
                    list(pyprimes.factors.factors(number_in_base))[0][0])
        assert (len(divisors) == 9 or len(divisors) == 0)
        if (len(divisors) == 9):
            number_of_found += 1
            print_divisors = ' '.join([str(k) for k in divisors])
            _output.write(current + " " + print_divisors + '\n')
            current = str(bin(int(current, 2) + 2))[2:]
            divisors = []

_output.close()
TEST_CASES_PER_BIT = 2
LOWER_BIT_BOUND = 20
UPPER_BIT_BOUND = 78

TEST_SET_NAME = "test_set5"

small_primes = []
for i in range(LOWER_BIT_BOUND, UPPER_BIT_BOUND + 1):
    # get the first odd i-bit number.
    lower_bound = 2 ** (i - 1) + 1
    
    primes_found = 0
    n = lower_bound
    while primes_found < TEST_CASES_PER_BIT:
        if is_prime(n):
            small_primes.append(n)
            primes_found += 1

        n += 2

test_cases = [BIG_PRIME * p for p in small_primes]

test_set_file = open(TEST_SET_NAME + ".txt", "w")
test_set_file.write('\n'.join(str(n) for n in test_cases))
test_set_file.close()

factor_file = open(TEST_SET_NAME + "_factors.txt", "w")
factor_file.write('\n'.join(str(p) for p in small_primes))
factor_file.close()
def prime_checker(num):
  ''' This will check to see if a given number is prime '''
  if pyprimes.is_prime(num):
    print 'It is prime!'
  else:
    print 'Nope! Try again.'
예제 #19
0
def prime(n):
    if is_prime(n):
        return True, None
    else:
        #print 'checking factors', n
        return False, brent(n)
예제 #20
0
# Project Euler - Problem 3
# The prime factors of 13195 are 5, 7, 13 and 29.
#
# What is the largest prime factor of the number 600851475143 ?

from pyprimes import is_prime
from math import sqrt
test_number = 600851475143
answer = 1

test_sqrt = sqrt(test_number) // 1
if test_sqrt % 2 == 0:
    test_sqrt += 1

for test_value in range(1, int(test_sqrt) + 2, 2):
    if test_number % test_value == 0:
        if is_prime(test_value):
            if test_value > answer:
                answer = test_value
        if is_prime(test_number // test_value):
            print(test_number // test_value)
            if (test_number // test_value) > answer:
                answer = test_number // test_value

print(answer)
예제 #21
0
def is_pair(a, b):
    s1, s2 = str(a), str(b)
    if pyp.is_prime(int(s1+s2)) and \
        pyp.is_prime((int(s2+s1))):
        return True
    return False
예제 #22
0
파일: tests.py 프로젝트: cesarfm/pyprimes
 def test_primes_start_is_inclusive(self):
     # Start argument to primes() is inclusive.
     n = 211
     assert pyprimes.is_prime(n)
     it = pyprimes.primes(start=n)
     self.assertEqual(next(it), n)