Ejemplo n.º 1
0
def primes_between(n, m):
    primes_list = []

    if n > m:
        raise ValueError(
            'The first input number must be greater than the second')

    for i in range(n, m):  # Generate all numbers between (n + 1) and (m - 1)

        if i >= (m - 1):  # The range (i) has reached m - 1
            if not is_prime.is_prime(
                    m - 1
            ):  # If (i OR m-1 - they're the same) is not prime, return the list and function is finished
                return primes_list

            if is_prime.is_prime(
                    m - 1
            ) == True:  # If m-1 is prime, add it to the list and return that
                primes_list.append(m - 1)
                return (primes_list)

        if is_prime.is_prime(
                i
        ) == False:  # If the integer is not prime, continute to the next one
            continue

        if is_prime.is_prime(
                i
        ) == True:  # If the integer is prime, add it to the list and continue
            primes_list.append(
                i)  # We keep doing this until the value of m-1 is reached
            continue
def goldbach(n):
    result = []
    for num in range(2, n):
        temp_list = [(n - num, num)]
        if is_prime(num) and is_prime(n - num) and not is_in_list(result, temp_list):
            result.append((num, n - num))
    return result
Ejemplo n.º 3
0
def test_is_prime():
    '''Make sure we are only getting primes'''

    assert is_prime(2) == True
    assert is_prime(3) == True
    assert is_prime(4) == False
    assert is_prime(23) == True
    assert is_prime(102) == False
Ejemplo n.º 4
0
def goldbach(n):
    divisor = 1
    tuples = []
    while divisor <= n / 2:
        if is_prime(divisor) and is_prime(n - divisor):
            tuples.append((divisor, n - divisor))
        divisor += 1
    return tuples
def goldbach(n):
    result = []
    for num in range(2, n):
        temp_list = [(n - num, num)]
        if is_prime(num) and is_prime(n - num) and not is_in_list(
                result, temp_list):
            result.append((num, n - num))
    return result
Ejemplo n.º 6
0
def goldbach(n):
    divisor = 1
    tuples = []
    while divisor <= n/2:
        if is_prime(divisor) and is_prime(n-divisor):
            tuples.append((divisor, n - divisor))
        divisor += 1
    return tuples
Ejemplo n.º 7
0
def goldbach(n):
    numbers = [(x, y) for x in range(1, n)
               for y in range(1, n)
               if is_prime(x)  # True
               if is_prime(y)  # True
               if x <= y
               if (x + y) == n]
    return numbers
Ejemplo n.º 8
0
def goldbach(even_integer):
    first_addend, second_addend = even_integer // 2
    result = []
    while second_addend <= even_integer:
        if is_prime(first_addend) and is_prime(second_addend):
            result.append((first_addend, second_addend))
        first_addend += 1
        second_addend -= 1
    return result
Ejemplo n.º 9
0
 def test_is_prime(self):
     with self.subTest():
         self.assertEqual(is_prime(1), False)
     with self.subTest():
         self.assertEqual(is_prime(2), True)
     with self.subTest():
         self.assertEqual(is_prime(3), True)
     with self.subTest():
         self.assertEqual(is_prime(0), False)
     with self.subTest():
         self.assertEqual(is_prime(-1), False)
Ejemplo n.º 10
0
    def test_float_input(self):
        self.assertEqual(is_prime(1.0), False)
        self.assertEqual(is_prime(4.0), False)
        self.assertEqual(is_prime(4.5), False)


#for num <= 1 return false
#for num <= 3 return true
#for num > 3 check if modulo == 0
#for any other input type - TypeError
#check what happens when FLOATS are used!!!! - only natural numbers can be checked for beeing a prime
Ejemplo n.º 11
0
def is_quadratic_residue(a, m):
    if is_prime(m):
        flag = legendre.is_quadratic_residue_legendre(a, m)
        return flag
    if not is_prime(m):
        flag = Jacob.is_quadratic_residue_Jacob(a, m)
        return flag


# is_quadratic_residue(11,2011)
# is_quadratic_residue(201111,2011)
# is_quadratic_residue(11,2011*2017)
# is_quadratic_residue(15211,2011*2017)
Ejemplo n.º 12
0
def test_is_prime():
    message = "{} is{} a prime number. Your function returned {}."

    true_tests = (14081, 17909, 11117, 9319, 6781, 6329, 719, 6211, 2473, 1877,
                  11923, 1823, 1409, 1187, 83, 17761, 7561, 4051, 5437, 10949,
                  2)
    for n in true_tests:
        assert is_prime(n), message.format(n, "", False)

    false_tests = (13035, 15547, 8073, 13559, 5921, 12321, 14541, 12543, 14433,
                   16123, 4005, 12939, 17145, 2109, 17415, 13673, 14529, 10917,
                   9571, 5919, 4)
    for n in false_tests:
        assert not is_prime(n), message.format(n, " not", True)
Ejemplo n.º 13
0
def prime_divisors(n):
    max_prime = 2
    if is_prime(n):
        return n
    else:
        t = int(math.sqrt(n))
        for k in range(the_nth_prime(1000), the_nth_prime(10000),
                       2):  #the 1000th prime number is not a divisor of n,
            # the_nth_prime(10000) is some upper bound I've chosen and improves performances significantly but I wonder
            # if there is some closed formula for a lower and upper bound for this problem
            #for k in range(1051, t // 2, 2):  # I've chosen to start from 1051 which is prime and is not a divisor of 600851475143
            if is_prime(k):
                if n / k == int(n / k) and k > max_prime:
                    max_prime = k
        return max_prime
Ejemplo n.º 14
0
def get_next_prime(start_int):
    n = start_int

    while is_prime(n) == False:
        n += 1

    return n
Ejemplo n.º 15
0
def prime_factorisation(n):
    if n == 1:
         return 1
   
    primes = []
    result = []
    
    for i in range(2, n + 1):
        if is_prime(i):
            primes.append(i)

    for p in primes:
        counter = 0
        if p * p > n:
             break

        while n % p == 0:
            counter += 1
            prime_factor = (p, counter)
            n //= p

        result.append(prime_factor)

    if n > 1:
        result.append(n)
 
    return result
def prime_number_of_divisors(n):
    counter = 0
    for i in range(1, n + 1):
        if n % i == 0:
            counter += 1

    return is_prime(counter)
Ejemplo n.º 17
0
def goldbach(n):
    prime_numbers = []
    rev_prime_numbers = []
    list_of_result = []
    result = []
    for i in range(2, n):
        if is_prime(i):
            prime_numbers.append(i)
    i = len(prime_numbers)
    while i > 0:
        i -= 1
        rev_prime_numbers.append(prime_numbers[i])
    for num in prime_numbers:
        for nums in rev_prime_numbers:
            if num + nums == n:
                list_of_result.append((num, nums))
    if len(list_of_result) == 1:
        return list_of_result
    elif len(list_of_result) == 2:
        if max(list_of_result[0]) != max(list_of_result[1]):
            return list_of_result
        else:
            list_of_result.remove(list_of_result[1])
            return list_of_result
    else:
        if len(list_of_result) % 2 == 0:
            for num in range(0, len(list_of_result) / 2):
                result.append(list_of_result[num])
            return result
        else:
            for num in range(0, len(list_of_result) / 2 + 1):
                result.append(list_of_result[num])
            return result
def prime_factorize(num: int) -> dict:
    """Returns a prime-factorization tree of a given integer.

    @param num: the number to factorize
    @type num: int
    @return: a prime factorization tree
    @rtype: dictj
    """

    factorization_tree = Tree({num: {}}, allowed_nodes=2)
    factor_queue = [factorization_tree._root_node]

    # Continuously factorize the numbers until there are none left.
    while len(factor_queue) > 0:
        node = factor_queue.pop(0)
        number = node.value

        # Only non-primes can be further subdivided
        if is_prime.is_prime(number) is True:
            continue

        try:
            factor_a, factor_b = next(iter(pairs.pairs(number, primes=False)))
        except StopIteration:
            break

        node_a = factorization_tree.insert(node, factor_a)
        node_b = factorization_tree.insert(node, factor_b)

        factor_queue.append(node_a)
        factor_queue.append(node_b)

    return factorization_tree
Ejemplo n.º 19
0
def generate_primes(start=2, end=0):
    while True:
        if end and start > end:
            break
        if ip.is_prime(start):
            yield start
        start += 1
Ejemplo n.º 20
0
def zad26(a, b):
    if a > 0 and b > 0:
        num = 1
        a -= 1
        for n in num_gen(num, a, b):
            if not is_prime(n):
                yield n
Ejemplo n.º 21
0
def obtain_prime_factorisation(N):
    """
    Return the prime factorisation of a number.

    Inputs:
        - N: integer

    Outputs:
        - a list of prime factors
        - a list of the exponents of the prime factors
    """
    factors = []
    potential_factor = 1

    while N > 1:
        potential_factor += 1

        if is_prime.is_prime(potential_factor):

            N, exponent = repeat_divide.repeat_divide_number(
                N, potential_factor)

            if exponent > 0:
                factors.append((potential_factor, exponent))

    return factors
def prime_number_of_divisors(n):
    if n == 1:
        return True
    elif n < 1:
        return False
    number_of_divisors = len(find_divisors(n))
    return is_prime(number_of_divisors)
Ejemplo n.º 23
0
def prime_number_of_divisors(n):
    counter = 0
    for i in range(1, n + 1):
        if n % i == 0:
            counter += 1

    return is_prime(counter)
Ejemplo n.º 24
0
 def test_not_prime(self):
     self.assertEqual(is_prime(4), False, "4  is not prime")
     self.assertEqual(is_prime(6), False, "6  is not prime")
     self.assertEqual(is_prime(8), False, "8  is not prime")
     self.assertEqual(is_prime(9), False, "9 is not prime")
     self.assertEqual(is_prime(45), False, "45 is not prime")
     self.assertEqual(is_prime(-5), False, "-5 is not prime")
     self.assertEqual(is_prime(-8), False, "-8 is not prime")
     self.assertEqual(is_prime(-41), False, "-41 is not prime")
Ejemplo n.º 25
0
def largest_prime_factor(n):
    i = 2
    while i != n:
        if is_prime(i) and n%i == 0:
            n = int(n/i)
        else:
            i += 1
    return(i)
def prime_number_of_divisors(number):
    num = number
    numbers = []
    while num >= 1:
        if number % num == 0:
            numbers.append(num)
        num = num - 1
    return is_prime(len(numbers))
Ejemplo n.º 27
0
def list_primes(n):
    '''Lists N number of prime numbers'''
    i, count = 2, 1
    while count <= n:
        if primes.is_prime(i):
            print(count, ':', i)
            count += 1
        i += 1
def previous_prime(n):
    for i in range(n-1, 2, -1): #Descending list from n-1 to 1 in increments of -1.

        if is_prime.is_prime(i) == False: # If not prime, continue the search
            continue

        else: # If this condition is reached, none of the numbers below are prime
            return i
def sum_of_primes(n):
    '''Return the sum of all primes less than or equal to n'''

    ret = 0
    for i in range(2, n + 1):
        if is_prime(i):
            ret += i
    return ret
Ejemplo n.º 30
0
def prime_factors(n):   
    primes = []
    while n != 1:
        for i in range(2, n+1):
            if is_prime(i) and n%i == 0:
                primes.append(i)
                n = n//i
    return primes
Ejemplo n.º 31
0
def phi(n):
    if is_prime(n):
        # if the number is prime, phi(n) is simply n-1
        return n - 1
    # else you have to factor it, calculate phi for each factor and multiply them
    n_factors = prime_factorization(n)
    phi_factors = map(lambda factor: phi_helper(factor[0], factor[1]),
                      n_factors)
    return reduce(lambda x, y: x * y, phi_factors)
Ejemplo n.º 32
0
def next_prime(n):
    #Generate every number bigger than n
    for i in range(n + 1, n**100):

        if is_prime.is_prime(i) == False:
            continue

        else:
            return i
Ejemplo n.º 33
0
def goldbach(n):
    result = []
    prime_addend = 2
    while prime_addend <= n / 2:
        if is_prime(n - prime_addend):
            result.append((prime_addend, n - prime_addend))
        prime_addend = next_prime(prime_addend)

    return result
Ejemplo n.º 34
0
def main():

    for number in range(100000000):
        if is_prime.is_prime(number):
            save_data(number)

    print("Job took: %s" % (datetime.datetime.now() - start))

    input()
Ejemplo n.º 35
0
def prime_number_of_divisors(n):
	numDivisors = 0
	for iterate in range(1,n+1):
  		if n % iterate == 0:
		        numDivisors+=1	
	if is_prime.is_prime(numDivisors):
		return True
	else:	
		return False
Ejemplo n.º 36
0
def goldbach(n):
    result = []
    prime_addend = 2
    while prime_addend <= n/2:
        if is_prime(n - prime_addend):
            result.append((prime_addend, n - prime_addend))
        prime_addend = next_prime(prime_addend)

    return result
def get_public_exponent(m):
    n = m-1

    while n > 0:
        if gcd(n, m) == 1 and is_prime(n):
            return n

        n -= 1

    return -1
Ejemplo n.º 38
0
def find_primes_up_to(n):
    if n < 2:
        return []

    primes = [2]
    currentNumber = 2
    while currentNumber <= n:
        if is_prime(currentNumber):
            primes.append(currentNumber)
        currentNumber = currentNumber + 1
    return primes
def prime_number_of_divisors(n):
    i = 1
    s = 0
    while i <= n:
        if n % i == 0:
            s += 1
        i += 1
    if is_prime(s):
        return True
    else:
        return False
def prime_number_of_divisors(n):
    count_divisors = 0
    divisor = 1
    while divisor <= n:
        if n % divisor == 0:
            count_divisors += 1
        divisor += 1
    if ip.is_prime(count_divisors):
        return True
    else:
        return False
Ejemplo n.º 41
0
def get_prime_factors(n):
    """Tests all divisors of n that are lower or equal than n/2 and if they are prime, returns them"""
    factors = []  # Store prime factors

    # Start testing from 2 up to half of the target number,
    # the maximum possible prime factor
    for i in range(2, n // 2 + 1):
        if n % i == 0 and is_prime.is_prime(
                i):  # If number is a divisor and is prime, return it
            factors.append(i)
    return factors
Ejemplo n.º 42
0
def nth_prime(n):
    '''Return the nth prime number'''

    primes_found = 0
    i = 2
    while primes_found < n:
        if is_prime(i):
            primes_found += 1
        i += 1

    # We return i - 1 here because 1 is added to i at the end of each loop
    return i - 1
Ejemplo n.º 43
0
def test_default():
    assert is_prime(11) == True
    assert is_prime(5) == True
    assert is_prime(30) == False
    assert is_prime(17) == True
    assert is_prime(6) == False
    assert is_prime(10) == False
Ejemplo n.º 44
0
 def test(self):
     self.assertEqual(is_prime(0), False, "0  is not prime")
     self.assertEqual(is_prime(1), False, "1  is not prime")
     self.assertEqual(is_prime(2), True, "2  is prime")
     self.assertEqual(is_prime(73), True, "73 is prime")
     self.assertEqual(is_prime(75), False, "75 is not prime")
     self.assertEqual(is_prime(-1), False, "-1 is not prime")
def prime_factorization(n):
    divisor = 2
    tuples = []
    while n > 1:
        if n % divisor == 0 and is_prime(divisor):
            power = 0
            while n % divisor == 0:
                n //= divisor
                power += 1
            tuples.append((divisor, power))
        else:
            divisor += 1
    return tuples
Ejemplo n.º 46
0
def euler7(n):
    if n == 1:
        return 2

    num = 1
    i = 1

    while i < n:
        num += 2
        if is_prime(num):
            i += 1

    return num
Ejemplo n.º 47
0
def goldbach(n):
    if n == 0:
        raise ZeroDivisionError

    result = []
    limit = n

    for i in range(2, (limit // 2) + 1):
        if is_prime(i) is True:
            first = i
            n -= first

            for j in range(n, 0, -1):
                if is_prime(j) and n - j == 0:
                    result.append((first, j))
                    break

        n = limit

    if len(result) == 0:
        return False

    return result
Ejemplo n.º 48
0
def prime_factorization(n):
    dividers = []
    counter = 2
    p = n
    while n != 1:
        if is_prime(counter) and n % counter == 0:
            dividers.append(counter)
            n /= counter
        else:
            counter += 1
    result = []
    for i in range(2, p + 1):
        if dividers.count(i) > 0:
            result.append((i, dividers.count(i)))
    return result
def prime_factorization(n):
    primes = []
    i = 2
    while i <= n:
        if is_prime(i):
            primes.append(i)
        i += 1
        
    result = []

    for numb in primes:
        while n % numb == 0:
            result.append(numb)
            n /= numb
                
    return result
Ejemplo n.º 50
0
def goldbach(n):
	if n <= 2:
		return False

	primes = []
	result = []

	for i in range(1, n):
		if is_prime(i):
			primes.append(i)

	for prime in primes:
		for another_prime in primes:
			if prime + another_prime == n and prime <= another_prime:
				result.append((prime, another_prime))

	return result
Ejemplo n.º 51
0
def prime_factorization(n):
	arr = []
	pow = 0
	numb = 2
	while n != 1:
		if is_prime(numb):
			if n % numb == 0:
				while n % numb == 0:
					pow = pow + 1
					n = n//numb
				arr = arr + [(numb, pow)]
				numb = numb + 1
				pow = 0
			else:
				numb =numb  + 1
		else:
			numb = numb + 1
	return arr
Ejemplo n.º 52
0
def prime_factorization(n):
    if is_prime(n):
        return [(n, 1)]

    result = []

    for i in range(2, n):
        times_divided = 0
        print("Starting ..")
        print(n)
        if n % i == 0:
            times_divided += 1
            n //= i

        if times_divided > 0:
            print("Appending .. ")
            result.append((i, times_divided))

    return result
Ejemplo n.º 53
0
def consecutive_primes(a, b):
    """
    Return the number of consecutive primes produced by n^2 + an + b

    Args:
        a (int): The 'a' coefficient.
        b (int): The 'b' coefficient.

    Returns:
        The number of consecutive primes; zero if the first number is not prime
    """

    count = 0
    n = 0
    while is_prime(n**2 + a*n + b):
        count += 1
        n += 1

    return count
Ejemplo n.º 54
0
def prime_factors(n, array=None, div=1):
  #if first pass, set array to a new instance
  if array is None:
    array = []

  #if n is negative, make it positive
  if n < 0:
    n = n * -1

  #make div a positive int
  if div < 1: 
    div = 1

  inc = 1
  #if div is > 2 we can increment by 2's, because no primes are even other than 2
  if div > 2:
    inc = 2
  
  # if we're at the end point. divisors won't be larger than the square root of input
  if div > n:
    # if array length is still one, then it's a prime number
    if len(array) == 0:
      array.append(n)

    return array

  # n is not a multiple of div, so proceed
  elif n % div != 0:
    return prime_factors(n, array, div+inc)

  # if n is a multiple of div, add it
  else:
    n = n / div
    if div not in array and is_prime(div):
      array.append(div)
    return prime_factors(n, array, div+inc)
Ejemplo n.º 55
0
# -*- coding: utf-8 -*-
"""
Created on Tue Sep 22 13:33:07 2015

@author: A.KRUG
"""

from is_prime import is_prime

somme = 0

for i in range(1, 1000):
    if is_prime(i):
        somme = somme + i

print(somme)
#PROJECT EULER PROBLEM 46

from is_prime import is_prime

answer = 0

for each in range(1,1000000,2):
    if is_prime(each):
        pass
    else:
        trigger = False
        for number in range(each):
            diff = each - 2*number**2
            if diff < 0:
                trigger = True
                answer = each
                break
            if is_prime(diff):
                break
        if trigger == True:
            print(answer)
            break

Ejemplo n.º 57
0
 def testCase1(self):
     self.assertTrue(is_prime(7), "7 should noot be prime")