cousin problem in python

0

Friends I need help with this code where I can be wrong? always tells me that the older cousin is in positions 0:

'' 'Read 10 integers, store them in a list and determine in what positions of the list is the greatest number read first' ''

try:

    lista=[]
    lista2=[]
    lista3=[]
    aumento=0

    for i in range(10):
        numero=int(input("Digite un numero entero: "))
        lista.append(numero)

    for l in range(len(lista)):
        primo=lista[l]
        aumento=0

        for k in range (1,primo+1):
            if (primo%k)==0:
                aumento+=1

        if aumento==2:
            lista2.append(primo)        

    print(lista2)

    mayor=lista2[0]

    for m in range(len(lista2)):
        if lista2[m]>mayor:
            mayor=lista2[m]

    for s in range(len(lista2)):
        if lista2[s]==mayor:
            lista3.append(s)

    print(lista3)

except ValueError:
    print("El valor digitado debe ser numerico")        
    
asked by Andress115 06.10.2018 в 19:26
source

1 answer

0

The problem is here:

 for s in range(len(lista2)):
        if lista2[s]==mayor:
            lista3.append(s)

Because you do not have to check the list of prime numbers but the list of entered numbers, then, it would look something like this:

 for s in range(len(lista)):
        if lista[s]==mayor:
            lista3.append(s)

Also! I recommend an easier method to check if a number is prime, you just have to try the prime numbers up to the square root of that number inclusive, for example:

If you have 67 and you want to know if it's a cousin, do not try from 1 to 67 all the numbers and you're dividing, you simply calculate the square root of 67, which is 8.18, then, tests from 2 to the square root inclusive and ready . If it is divisible by some prime number between 2 and 8, then it is no longer a prime.

    
answered by 06.10.2018 в 21:43