Show python position of prime only

Asked

Viewed 613 times

0

Read 10 integers and store them in a vector. Then type the elements that are prime and their respective positions in the vector

vetor=[]
for c in range(3):
    n = int(input("Digite um número para armazena-lo:"))
    divisores = 0
    for divisor in range(1, n):
        if n % divisor == 0:
            divisores += 1
    if divisores > 1:
        None
    else:
        vetor+=[n]
        index = vetor.index()
print(f'Os primos nesse vetor são {vetor}:{index}')

I cannot show the position of prime numbers.

  • Lucas, can you make a table test of your code? Why the variable c varies from 0 to 3? What would be the logic of None when there is more than one divisor? What would be the method index() of the list?

  • c is to vary from 0 to 10, I only used 3 to test the code faster. E None is because I don’t want him to do anything when the number isn’t prime.

  • It would no longer agree with what was requested if you first read the whole vector and then traverse the vector to see if each element is prime?

  • I get it, what you proposed, I just don’t know how to do it. .

3 answers

2

The logic to solve this problem is simpler than you think:

  1. Set an empty vector;
  2. Set a repeat loop with 10 iterations;
    1. For each iteration read a number;
    2. Add the number in the vector;
  3. Define a repeat loop by traversing the array;
    1. For each value check if it is a prime number;
    2. If it is prime, display the value together with the index;

The index you can get through your loop repetition; recommend studying the function enumerate.

0


You can use the native function enumerate() which is capable of receiving a list or any other object everlasting as parameter and return a generator of tuples containing the index and its respective value in the list, see only:

vetor = [10,20,30,40,50]
gen = enumerate(vetor)
print(list(gen))

Exit:

[(0, 10), (1, 20), (2, 30), (3, 40), (4, 50)]

See working on repl it.

Follow an example of code able to solve your problem by making use of the function enumerate():

def eh_primo(n):
    if n < 2:
        return False
    for i in range(2, n):
        if n % i == 0:
            return False
    return True

def ler_numeros(n):
    v = []
    for i in range(n):
        n = int(input("Digite um número para armazena-lo: "))
        v.append(n)
    return v

for i, n in enumerate(ler_numeros(10)):
    if eh_primo(n):
        print(f'O numero {n} eh primo e estah na posicao {i} do vetor.')

Testing:

Digite um número para armazena-lo: 101
Digite um número para armazena-lo: 102
Digite um número para armazena-lo: 103
Digite um número para armazena-lo: 104
Digite um número para armazena-lo: 105
Digite um número para armazena-lo: 106
Digite um número para armazena-lo: 107
Digite um número para armazena-lo: 108
Digite um número para armazena-lo: 109
Digite um número para armazena-lo: 110

Exit:

O numero 101 eh primo e estah na posicao 0 do vetor.
O numero 103 eh primo e estah na posicao 2 do vetor.
O numero 107 eh primo e estah na posicao 6 do vetor.
O numero 109 eh primo e estah na posicao 8 do vetor.

See working on repl it.

0

To solve this question we must read and store a total of ten values in a vector. Then check which of them are primes and then display the value and position of said prime.

For this we can use the following algorithm...

def valor_posicao(vet):
    primos = list()
    posicao = list()
    for c in vet:
        if primo(c):
            primos.append(c)
            posicao.append(vet.index(c))
    return primos, posicao


def primo(m):
    i = 1
    cont = 0
    while i <= m:
        if m % i == 0:
            cont += 1
        i += 1
    if cont != 2:
        return False
    else:
        return True


vetor = list(map(int, input('Digite 10 números: ').split()))

pri, posi = valor_posicao(vetor)

for a, b in zip(pri, posi):
    print(f'O número {a} é primo e está na posição {b}')

See here the functioning of the algorithm.

Note that when we execute this code we receive the following message: Digite 10 número: . Right now we must type all the values, in the same line, separated for a single space and press enter.

At this time the values will be stored in the list vetor and then such a list will be sent as a parameter to the function valor_posicao(). Chagando there the block for go through all the values of the respective list and, with the help of if block shall be checked whether each item of vetor is prime. If positive, the prime value will be added to the list primos and the value index will be added in the list posicao. Two lists will be displayed later. One containing the prime values found in the vector and the other containing the indices of each prime number found in the respective vector.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.