+ 2
Cual es el error?, Necesito que cuente cuántos primos hay en los tres número ingresados
#Inicializar c = 0 i=1 numEntradas = 3 #proceso print("Ingrese", numEntradas, "Números:") while i<= numEntradas: n = int( input("Ingrese número: ")) for r in range(2,n+1): if n%r== 0 : c = c + 1 i+=1 if c<2: print ("En", numEntradas, "números ingresados hay", c,"numeros primos")
1 Réponse
0
some of the problems I see:
• i += 1 is in the 'for' loop, should be in the 'while' loop
• c counts the number of divisors, not the number of primes, and you don't reset it for each n
• you display c in the loop, if you want to display the number of primes (not c, c isn't the number of primes), it should be at the end, outside of any loop.
Btw, you don't need to go to n+1 to determine if n is prime, even for a non-prime you won't find any divisors after n/2 (but if you haven't found a divisor until sqrt(n), sqrt(n) included, then n is prime, you won't find any after). And once you found a divisor, you can break out of the 'for' loop, as it clearly isn't a prime.
Here's how I would have done it:
https://code.sololearn.com/c880u48WSst2
(note that the 'else' I used is for the loop, not for the 'if'; in python, loops can have an 'else' branch that is executed if the loop completes without reaching a break statement)