Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. input data:
  2. 7 1 199999 4 # são os índices da list imaginária
  3.  
  4. answer:
  5. 19 3 2750131 11 # são os primos dessa list, correspondentes aos índices acima
  6.  
  7. from math import sqrt
  8.  
  9. def primos(n, maxIndex):
  10. maiorCheck = int(sqrt(n))
  11. lista = []
  12. i, x, qtdPrimos = 0, 0, 0
  13. for m in range(2, n+1):
  14. lista.append(m)
  15. while x <= maiorCheck:
  16. x = lista[i]
  17. i += 1
  18. for numero in lista:
  19. if numero != x:
  20. if numero % x == 0:
  21. lista[lista.index(numero)] = 0
  22. while 0 in lista:
  23. del lista[lista.index(0)]
  24. if len(lista) == maxIndex:
  25. x = maiorCheck + 1
  26. return lista
  27.  
  28. qtdPrimos = int(input('Entre com a quantidade de primos a serem impressos: ').strip())
  29.  
  30. indexes = input('Entre com os índices para os quais serão retornados primos: ').strip().split()
  31. result = []
  32.  
  33. for n in range(len(indexes)):
  34. indexes[n] = int(indexes[n])
  35.  
  36. arrayPrimos = primos(2750131, max(indexes))
  37.  
  38. for m in range(len(indexes)):
  39. result.append(str(arrayPrimos[indexes[m]-1]))
  40.  
  41. print('n--- R E S U L T A D O ---n'+' '.join(result))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement