MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / get_divisors

Function get_divisors

maths/primelib.py:651–682  ·  view source on GitHub ↗

input: positive integer 'n' >= 1 returns all divisors of n (inclusive 1 and 'n') >>> get_divisors(8) [1, 2, 4, 8] >>> get_divisors(824) [1, 2, 4, 8, 103, 206, 412, 824] >>> get_divisors(-1) Traceback (most recent call last): ... AssertionError: 'n' must

(n)

Source from the content-addressed store, hash-verified

649
650
651def get_divisors(n):
652 """
653 input: positive integer 'n' >= 1
654 returns all divisors of n (inclusive 1 and 'n')
655
656 >>> get_divisors(8)
657 [1, 2, 4, 8]
658 >>> get_divisors(824)
659 [1, 2, 4, 8, 103, 206, 412, 824]
660 >>> get_divisors(-1)
661 Traceback (most recent call last):
662 ...
663 AssertionError: 'n' must been int and >= 1
664 >>> get_divisors("test")
665 Traceback (most recent call last):
666 ...
667 AssertionError: 'n' must been int and >= 1
668 """
669
670 # precondition
671 assert isinstance(n, int) and (n >= 1), "'n' must been int and >= 1"
672
673 ans = [] # will be returned.
674
675 for divisor in range(1, n + 1):
676 if n % divisor == 0:
677 ans.append(divisor)
678
679 # precondition
680 assert ans[0] == 1 and ans[len(ans) - 1] == n, "Error in function getDivisiors(...)"
681
682 return ans
683
684
685# ----------------------------------------------------

Callers 1

is_perfect_numberFunction · 0.85

Calls 1

appendMethod · 0.45

Tested by

no test coverage detected