-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexercises_4.py
49 lines (34 loc) · 952 Bytes
/
exercises_4.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Create a program that asks the user for a number and then prints out a list of all the divisors of that number.
(If you don’t know what a divisor is, it is a number that divides evenly into another number.
For example, 13 is a divisor of 26 because 26 / 13 has no remainder.)
'''
n = input('Please input a number: ')
n = int(n)
# The simplest solution
res = []
for i in range(2, n):
if n % i == 0:
res.append(i)
res.append(1)
res.append(n)
res.sort()
print('The following are divisors of your input number: ')
print(res)
# A more efficient solution
from math import sqrt
x = int(sqrt(n))
res2 = []
for i in range(2, x):
if n % i == 0:
res2.append(i)
res2.append(n//i)
if n == x * x:
res.append(x)
res2.append(1)
res2.append(n)
res2.sort()
print('The following are divisors of your input number calculated by another way: ')
print(res2)