Tip:
Do not use list comprehensions to perform functions like print()
, append()
etc ... will create you a temporary list full of None
values, and has performance implications if this is very large, you can test this by doing print(print('hey'))
whose print
outer will print the return of print
inner (which is None
), REF .
As for performance there is not much to do and your implementation is largely well done:
Way 1:
def divisores(num):
for i in range(1, num//2+1):
if num % i == 0:
yield i
yield num
num = 47587950
print(list(divisores(num)))
Way 2:
num = 47587950
print([i for i in range(1, num//2+1) if num%i==0])
As for performance, the first way can be a bit faster, and if you do not make it into the list it's almost instantaneous:
STATEMENT
In the above demo:
using way 1 we have a time of 1.7259
secs
using way 2 we take 1.9021
secs
Remember that your computer should run faster than in Ideone.
Using the Way 1 function, the most appropriate way to print one at a time would be:
...
num = 47587950
for d in divisores(num):
print(d)