def exp_representaion(factors): uniq_primes = np.unique(factors) num = 1 for f in factors: num*=f primes = [np.asscalar(p) for p in get_primes(num) if p < num] exp_repr = [] for p in primes: count = 0 for f in factors: if p == f: count+=1 exp_repr.append(count) return exp_repr
def exp_representaion(factors): uniq_primes = np.unique(factors) num = 1 for f in factors: num *= f primes = [np.asscalar(p) for p in get_primes(num) if p < num] exp_repr = [] for p in primes: count = 0 for f in factors: if p == f: count += 1 exp_repr.append(count) return exp_repr
def get_prime_factors(num): if num < 2: raise Exception('not decomposable into prime factors') primes = [np.asscalar(p) for p in get_primes(num) if p < num] composite = np.asscalar(np.copy(num)) factors = [] index = 0 while index < len(primes): primf = primes[index] # check if the composite number is factorable # by a primefactor while composite % primf == 0: composite /= primf factors.append(primf) index += 1 if not [int(k) for k in factors]: return [1, num] else: return [int(k) for k in factors]
def get_prime_factors(num): if num < 2: raise Exception('not decomposable into prime factors') primes = [np.asscalar(p) for p in get_primes(num) if p < num] composite = np.asscalar(np.copy(num)) factors = [] index = 0 while index < len(primes): primf = primes[index] # check if the composite number is factorable # by a primefactor while composite%primf == 0: composite /= primf factors.append(primf) index += 1 if not [int(k) for k in factors]: return [1, num] else: return [int(k) for k in factors]
def test_assert_input_is_not_float(self): self.assertRaises(TypeError,get_primes(5))
def test_assert_input_is_not_list(self): self.assertRaises(TypeError,get_primes([1,3]))
def test_assert_input_is_not_string(self): self.assertRaises(TypeError,get_primes('a_string'))
def test_asserts_input_not_negative(self): self.assertIs(get_primes(-10),'Please don\'t input negatives')
def test_returns_correct_primes(self): self.assertTrue(get_primes(20),[1,3,5,7,11,13,17,19])
import prime_numbers primes = prime_numbers.get_primes(2000000-1) res = sum(primes) print "Answer: {}".format(res)
def test_returns_primes_in_array(self): # Result should be an array self.assertIsInstance(get_primes(20), List) def test_assert_input_not decimal(self): self.assertIs(get_primes)(5.2),'dont imput floating points' )