Your variable c
(counting the number of divisors) must be reset to each number being tested. The way you did, she ends up counting the total amount of divisors of all the numbers. Also, you should only print the amount of primes at the end, after you have made the loops:
qntPrimo = 0
for elemento in vetor:
c = 0 # zerar aqui
for divisor in range(1, elemento +1):
if elemento % divisor == 0:
c = c + 1
if c == 2:
qntPrimo += 1
print(qntPrimo) # só imprime a contagem no final
Although the exercise asks to create separate functions: one that only checks if a number is prime, and another that counts the number of primes, using the first function. That is to say:
# verifica se o número é primo
def IsPrime(n):
if n <= 1: # 0 e 1 não são primos, e estou considerando que os negativos também não são
return False
qtd_divisores = 0
# todo número é divisível por 1 e por ele mesmo, então pra que testá-los? Comece pelo 2 mesmo
for i in range(2, n):
if n % i == 0:
qtd_divisores += 1
# é primo se a quantidade de divisores é zero
return qtd_divisores == 0
# retorna a quantidade de números primos
def NumberOfPrimes(numeros):
qtd_primos = 0
for n in numeros:
if IsPrime(n):
qtd_primos += 1
return qtd_primos
I changed a little the algorithm that checks if it is prime, since 1 and 0 are not primes, besides not having to check if the number is divisible by 1 and by itself (all are, it is redundant to check this). Of course you can still improve a lot, has better algorithms, but it is not the focus of the question.
Once having the above functions, just use them:
vetor = []
qtd = int(input('Digite o tamanho do vetor: '))
for _ in range(qtd): # outra forma de ler os números
vetor.append(int(input('Digite um numero: ')))
qtd_primos = NumberOfPrimes(vetor)
print(f'No vetor {vetor} existem {qtd_primos} números primos')
Also note that separating into functions makes it easier to understand what each part of the program does. When trying to do two things with two loops nested, the variables used by each end up getting confused and the code became more confused and difficult to be debugged.
Finally, I also gave better names for variables. It may seem like a silly detail, but give names better help when programming.
"came out wrong", what "comes out"? And what should come out?
– Woss
in the case of item b, it should show a only the amount of a primes within the vector. if I have it rotated the way I put it here, it executes only the part of the vector reading and the entire print.
– Jenifer De Marcena
In the calculating part of the cousin it shows nothing on the console.
– Jenifer De Marcena
It will only show if there is some prime value. In the vector you tested there was at least one prime number?
– Woss
yes, I’m always putting the values [1, 2, 3, 4, 5] to test
– Jenifer De Marcena
The value of
c
should not restart each element?– Woss
How so? I use c as a counter of qnts times that element is divided.
– Jenifer De Marcena
Not only that... but all of them, in the same variable.
– Woss
I don’t know if I understand or how.
– Jenifer De Marcena