def test_negative(self): self.assertEqual(eratosthenes(50, 50), []) self.assertEqual(eratosthenes( 50, -5), [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]) self.assertEqual(eratosthenes(-5, 10), []) self.assertEqual(eratosthenes(-5, 10, True), (False, []))
def test_negative(self): self.assertEqual(eratosthenes(50, 50), []) self.assertEqual(eratosthenes(50, -5), [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]) self.assertEqual(eratosthenes(-5, 10), []) self.assertEqual(eratosthenes(-5, 10, True), (False, []))
def test_positive(self): self.assertEqual( eratosthenes(50), [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]) self.assertEqual(eratosthenes(51, 10), [11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]) self.assertEqual(eratosthenes(10, 1, True), (True, [2, 3, 5, 7]))
def test_positive(self): self.assertEqual(eratosthenes(50), [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]) self.assertEqual(eratosthenes(51, 10), [11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]) self.assertEqual(eratosthenes(10, 1, True), (True, [2, 3, 5, 7]))
def test_eratosthenes_additional(self): rv1 = eratosthenes(-10) rv2 = eratosthenes(10) rv3 = eratosthenes(100, 5) rv4 = eratosthenes(100, -10) self.assertEqual(rv1, []) self.assertEqual(rv2, [2, 3, 5, 7]) self.assertEqual(rv3, [ 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97 ]) self.assertEqual(rv4, [ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97 ])
def is_prime(number, cache=True): if number < 2: return False global primes_cache_list, primes_cache_bool if cache and len(primes_cache_list) == 0: primes_cache_list, primes_cache_bool = eratosthenes( CACHE_LIMIT, return_boolean=True ) for prime in primes_cache_list: primes_cache_bool[prime] = True if number < len(primes_cache_bool): return primes_cache_bool[number] sqrt_number = sqrt(number) for prime in primes_cache_list: if prime > sqrt_number: return True if number % prime == 0: return False to_check = 2 if len(primes_cache_list) > 0: to_check = primes_cache_list[-1] + 1 while to_check <= sqrt_number: if number % to_check == 0: return False to_check += 1 return True
def is_prime(number, cache=True): if number < 2: return False global primes_cache_list, primes_cache_bool if cache and len(primes_cache_list) == 0: primes_cache_list, primes_cache_bool = eratosthenes( CACHE_LIMIT, return_boolean=True) for prime in primes_cache_list: primes_cache_bool[prime] = True if number < len(primes_cache_bool): return primes_cache_bool[number] sqrt_number = sqrt(number) for prime in primes_cache_list: if prime > sqrt_number: return True if number % prime == 0: return False to_check = 2 if len(primes_cache_list) > 0: to_check = primes_cache_list[-1] + 1 while to_check <= sqrt_number: if number % to_check == 0: return False to_check += 1 return True
def is_prime(number, cache=True): """ Takes `number` and determines if it is prime. :param number: The integer to be tested for primality. :param cache: A boolean to determine if a cache should be used to improve performance. :rtype: A boolean that signifies if `number` is prime. """ if number < 2: return False global primes_cache_list, primes_cache_bool if cache and len(primes_cache_list) == 0: primes_cache_list, primes_cache_bool = eratosthenes( CACHE_LIMIT, return_boolean=True) for prime in primes_cache_list: primes_cache_bool[prime] = True if number < len(primes_cache_bool): return primes_cache_bool[number] sqrt_number = sqrt(number) for prime in primes_cache_list: if prime > sqrt_number: return True if number % prime == 0: return False to_check = 2 if len(primes_cache_list) > 0: to_check = primes_cache_list[-1] + 1 while to_check <= sqrt_number: if number % to_check == 0: return False to_check += 1 return True
def test_eratosthenes(self): rv1 = eratosthenes(-10) rv2 = eratosthenes(10) rv3 = eratosthenes(100, 5) rv4 = eratosthenes(100, -10) self.assertEqual(rv1, []) self.assertEqual(rv2, [2, 3, 5, 7]) self.assertEqual( rv3, [5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97] ) self.assertEqual( rv4, [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97] )
def trial_division(n): prime_factors = [] if n < 2: return prime_factors for p in eratosthenes(int(n**0.5) + 1): if p * p > n: break while n % p == 0: prime_factors.append(p) n //= p if n > 1: prime_factors.append(n) return prime_factors
def mth_operations_1_input(f_code, v): if f_code == 1: res = approx_cdf.cdf(v) return res elif f_code == 4: res = primality_test.is_prime(v) return res elif f_code == 5: res = sieve_atkin.atkin(v) return res elif f_code == 6: res = sieve_eratosthenes.eratosthenes(v) return res elif f_code == 7: res = std_normal_pdf.pdf(v) return res
def trial_division(n): """ Uses trial division to find prime factors of `n`. :param n: An integer to factor. :rtype: The prime factors of `n` """ prime_factors = [] if n < 2: return prime_factors for p in eratosthenes(int(n**0.5) + 1): if p*p > n: break while n % p == 0: prime_factors.append(p) n //= p if n > 1: prime_factors.append(n) return prime_factors
def trial_division(n): """ Uses trial division to find prime factors of `n`. :param n: An integer to factor. :rtype: The prime factors of `n` """ prime_factors = [] if n < 2: return prime_factors for p in eratosthenes(int(n**0.5) + 1): if p * p > n: break while n % p == 0: prime_factors.append(p) n //= p if n > 1: prime_factors.append(n) return prime_factors
def is_prime(number, cache=True): """ Takes `number` and determines if it is prime. :param number: The integer to be tested for primality. :param cache: A boolean to determine if a cache should be used to improve performance. :rtype: A boolean that signifies if `number` is prime. """ if number < 2: return False global primes_cache_list, primes_cache_bool if cache and len(primes_cache_list) == 0: primes_cache_list, primes_cache_bool = eratosthenes( CACHE_LIMIT, return_boolean=True ) for prime in primes_cache_list: primes_cache_bool[prime] = True if number < len(primes_cache_bool): return primes_cache_bool[number] sqrt_number = sqrt(number) for prime in primes_cache_list: if prime > sqrt_number: return True if number % prime == 0: return False to_check = 2 if len(primes_cache_list) > 0: to_check = primes_cache_list[-1] + 1 while to_check <= sqrt_number: if number % to_check == 0: return False to_check += 1 return True