input: positive integer 'n' >= 1 returns all divisors of n (inclusive 1 and 'n')
(n)
| 489 | # ---------------------------------------------------- |
| 490 | |
| 491 | def getDivisors(n): |
| 492 | """ |
| 493 | input: positive integer 'n' >= 1 |
| 494 | returns all divisors of n (inclusive 1 and 'n') |
| 495 | """ |
| 496 | |
| 497 | # precondition |
| 498 | assert isinstance(n,int) and (n >= 1), "'n' must been int and >= 1" |
| 499 | |
| 500 | from math import sqrt |
| 501 | |
| 502 | ans = [] # will be returned. |
| 503 | |
| 504 | for divisor in range(1,n+1): |
| 505 | |
| 506 | if n % divisor == 0: |
| 507 | ans.append(divisor) |
| 508 | |
| 509 | |
| 510 | #precondition |
| 511 | assert ans[0] == 1 and ans[len(ans)-1] == n, \ |
| 512 | "Error in function getDivisiors(...)" |
| 513 | |
| 514 | |
| 515 | return ans |
| 516 | |
| 517 | |
| 518 | # ---------------------------------------------------- |