Ana,
To receive two values, one must either consider N as a list of integers, or receive two arguments in the function signature, such as:
def divisores(num1, num2):
Although your procedure works, notice the following:
- You test the division of all numbers between 0 - received number, even if the smallest possible division (2), happens in num/2;
- You test, again, every numpy array if the rest of the split is 0;
Although it’s not a very charming algorithm, one way you perform fewer iterations is as follows::
div_1 = set() #Cria um set vazio
for i in range(1, int(num1/2+1)): #itera de 1 até metade do valor numérico
if num1%i == 0: #testa se mod é zero.
div_1.add(i) #anexa o valor de i ao set de divisores do primeiro numero
It is worth remembering that this function, like its code, does not return the value itself as a possible divisor (in this case, when you enter 30, the values obtained will be:{1, 2, 3, 5, 6, 10, 15} ) (30 Excluso). In addition, it is possible to consider using the "set" object instead of lists, since the numbers should not repeat.
In this case, if the final function were just to find the intersection of the common division values, you could include everything in the same loop, as it is not, a simplistic approach is to run another iteration on to identify the same divisors of the other number (num2).
For the intersection, you could accomplish the following:
lista_divisores_comuns = div_1.intersection(div_2)
The final code would look like:
def divisores(num1, num2):
div_1 = set()
div_2 = set()
for i in range(1, int(num1/2+1)):
if num1%i == 0:
div_1.add(i)
for i in range(1, int(num2/2+1)):
if num2%i == 0:
div_2.add(i)
lista_divisores_comuns = div_1.intersection(div_2)
return div_1, div_2, lista_divisores_comuns
Well, I would use that. The approach is more objective and more polished.
– Nicolas Vieira