def test_rand(self): def divisors_ref(num): l = [a for a in range(2, num) if num % a == 0] if len(l) == 0: return str(num) + " is prime" return l a = randint(2, 100) b = randint(2, 100) c = randint(2, 100) self.assertEqual(divisors(a), divisors_ref(a)) self.assertEqual(divisors(b), divisors_ref(b)) self.assertEqual(divisors(c), divisors_ref(c))
def order(n, elt): if elt in coPrime(n): for i in sorted(divisors(totient(n))): if elt ** i % n == 1: return i else: return "Element not in set"
def main(): n = input("Enter n: ") n = int(n) sum_divisors = sum_ints(divisors(n)) print(sum_divisors)
def is_practical(p_candidate: int) -> bool: if p_candidate < 1: return False if p_candidate == 1: return True divisors = list(divisors(p_candidate)) sums = set(divisors) arity = 1 def get_sums(arity: int): return [sum(c) for c in combinations(divisors, arity)] def is_divisor_sum(ds_candidate): nonlocal arity nonlocal sums if ds_candidate in sums: return True while arity <= len(divisors): arity += 1 more_sums = get_sums(arity) sums = sums.union(more_sums) if ds_candidate in sums: return True return False for i in range(1, p_candidate): if not is_divisor_sum(i): return False return True
def is_perfect(n): perfect = False if n == sum_ints(divisors(n)): perfect = True return perfect
def test_three_to_nine(): assert divisors(3) == [1, 3] assert divisors(4) == [1, 2, 4] assert divisors(5) == [1, 5] assert divisors(6) == [1, 2, 3, 6] assert divisors(7) == [1, 7] assert divisors(8) == [1, 2, 4, 8] assert divisors(9) == [1, 3, 9]
def is_prime(number): if number % 2 == 0: log("{0} is not prime".format(number)) else: divisors_list = divisors(number) if len(divisors_list) > 2: log("{0} is not prime, here the divisors:{1}".format( number, divisors_list)) else: log("{0} is prime, here the divisors:{1}".format( number, divisors_list))
def find_prime(n): if n == 1: return [] prime = [] multi = 1 factor = list(divisors(n)) factor.remove(1) for i in factor: if is_prime(i): prime.append(i) multi *= i return prime + find_prime(n // multi)
def is_perfect(number): log("Perfect number") divisors_list = divisors(number) divisors_list.pop() if sum(divisors_list) == number: return {'is': True, 'divisors': divisors_list} return { 'is': False, }
def test(self): self.assertEqual(divisors(15), [3, 5]) self.assertEqual(divisors(253), [11, 23]) self.assertEqual(divisors(24), [2, 3, 4, 6, 8, 12]) self.assertEqual(divisors(13), "13 is prime") self.assertEqual(divisors(3), "3 is prime") self.assertEqual(divisors(29), "29 is prime")
def factors(n): ''' A generator that generates the prime factors of n. For example >>> list(factors(12)) [2,2,3] Params: n (int): The operand Yields: (int): All the prime factors of n in ascending order. Raises: ValueError: When n is <= 1. ''' keep_going = True prime_factors = divisors(n) while keep_going: keep_going = False for val in prime_factors: if divisors(val) > 2: keep_going = True prime_factors.append(divisors(val)) return sorted(factors)
def test_suite(image_name='images/orig.png',slice_widths=None,n_repeats=10,max_factors=None,no_small=False): image = SlicedImage(fn=image_name) if slice_widths is None: width = image.pix.shape[1] factors = divisors(width) if no_small: factors = filter(lambda x:.5*image.pix.shape[1] > x > .02*image.pix.shape[1], factors) if max_factors is not None and max_factors < len(factors): factors = sample(factors, max_factors) slice_widths = factors tests = [] for n_slices in slice_widths: for n_test in range(n_repeats): print n_slices, n_test tests.append(slice_test(n_slices,image=image)) for s_in,s_out in tests: print s_in == s_out, s_in, s_out print 'success rate: %.2f %%' % (float(len(filter(lambda x: x[0] == x[1],tests)))/len(tests)*100)
def main(): fail = False i = 1 n = 0 while True: n += i limit = math.ceil(math.sqrt(n)) d = divisors(n) if len(d) > 500: break; i += 1 return n
def main(): abundant_numbers = [] for i in range(28123): if i < sum(divisors(i)): abundant_numbers.append(i) a_sums = set() for a in abundant_numbers: for b in abundant_numbers: a_sums.add(a + b) s = 0 for i in range(28123): if i not in a_sums: s += i return s
def main(): numbers = [] amicable_numbers = set() for i in range(0, 10000): numbers.append(divisors(i)) for a in range(len(numbers)): d_a = sum(numbers[a]) for b in range(len(numbers)): if a == b: continue d_b = sum(numbers[b]) if a == d_b and b == d_a: amicable_numbers.add(a) amicable_numbers.add(b) return sum(amicable_numbers)
def test_five(): assert divisors(5) == [1, 5]
def test_four(): assert divisors(4) == [1, 2, 4]
def test_three(): assert divisors(3) == [1, 3]
def is_amicable_pair(x, y): if x == y: return False else: return sum(divisors(x)) == y and sum(divisors(y)) == x
def test_ten(): assert divisors(10) == [1, 2, 5, 10]
def test_eight(): assert divisors(8) == [1, 2, 4, 8]
def test_one(): assert divisors(1) == [1]
def test_negatives(): assert 3 not in divisors(7) assert 5 not in divisors(11)
def test_prime_two_hundred_eight_one(): assert divisors(281) == [1, 281]
def test_two_hundred(): assert divisors(200) == [1, 2, 4, 5, 8, 10, 20, 25, 40, 50, 100, 200]
def isamicablepair(x): if sum(divisors(x)) != x and sum(divisors(sum(divisors(x)))) == x: return True return False
def tau(n): return divisors(n).__len__()
def test_six(): assert divisors(6) == [1, 2, 3, 6]
def test_seven(): assert divisors(7) == [1, 7]
from math import sqrt from divisors import divisors def is_amicable_pair(x, y): if x == y: return False else: return sum(divisors(x)) == y and sum(divisors(y)) == x if __name__ == '__main__': divisor_sums = {} for i in range(1, 10000): divisor_sums[i] = sum(divisors(i)) amicable_numbers = set() for k, v in divisor_sums.items(): if k != v: try: possible_pair = divisor_sums[v] except KeyError: pass else: if possible_pair == k: amicable_numbers.add(k) amicable_numbers.add(v) print(sum(amicable_numbers))
def test_nine(): assert divisors(9) == [1, 3, 9]
def is_abundant(n): return sum(divisors(n)) > n
def test_one_hundred(): assert divisors(100) == [1, 2, 4, 5, 10, 20, 25, 50, 100]
def test_divisors_6(self): self.assertNotEqual(divisors(6), [1, 2, 3, 7])
def test_prime_seventeen(): assert divisors(17) == [1, 17]
def test_3600(): assert list(divisors(3600)) == [ 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, 16, 18, 20, 24, 25, 30, 36, 40, 45, 48, 50, 60, 72, 75, 80, 90, 100, 120, 144, 150, 180, 200, 225, 240, 300, 360, 400, 450, 600, 720, 900, 1200, 1800, 3600 ]
def test_100(): assert 50 in divisors(100) assert 25 in divisors(100) assert 10 in divisors(100)
def return_divisors_list(num): # make list into dictionary return jsonify({'divisors': divisors(num)})
def test_product_of_primes(): assert divisors(901) == [1, 17, 53, 901]
def sigma(n): sum = 0 for i in divisors(n): sum = sum + i return sum
def test_two(): assert divisors(2) == [1, 2]
def test_divisors_88(self): self.assertEqual(divisors(88), [1, 2, 4, 8, 11, 22, 44, 88])
def is_prime(n): return n != 1 and divisors(n) == {1, n}
def test_divisors_127(self): self.assertEqual(divisors(127), [1, 127])