How to print nth index of prime number in python ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How to print nth index of prime number in python ?

I want to print prime number on index if user input 3 then I want to print 5 because if we see prime number 2,3,5,7,... 5 comes at index 3 so I wanna print just 5. if user input 2 then I wanna print 3 like this.... My attempt : https://code.sololearn.com/cJA79Tvl8Tpn/?ref=app Finally I did Thank you for helping guys! New code : https://code.sololearn.com/c5JFCpr9bqAl/?ref=app

4th Nov 2020, 8:10 PM
Ratnapal Shende
Ratnapal Shende - avatar
4 Answers
+ 4
Thank you. Your code helped me, my code hope help you. https://code.sololearn.com/cDsUtyqXN9g3/?ref=app
4th Nov 2020, 9:04 PM
Shadoff
Shadoff - avatar
+ 9
Right now, your code prints the prime numbers up to n. In order to solve the given task you should: 1- Break that ugly oneliner into several logical pieces. Use functions and variables with descriptive names(e.g: isPrime, etc). Please decouple things out. Shorter code is not necessarily better. 2- You can follow this logic (pseudocode) : WHILE (PrimesFound < n) DO: if isPrime( i ) DO: PrimesFound += 1 i += 1 WHEN (PrimesFound == n) DO: print LastPrimeFound 3- If you can't succeed after following those tips then let us see your new code. We can guide you from there. Edit: Or use Shadoff's code..
4th Nov 2020, 9:05 PM
Kevin ★
+ 3
You most likely have to iterate through known primes. We have no known prime generating function.
4th Nov 2020, 11:43 PM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 2
Kevin ★ i did it as u said Thank for your help..... Really grateful!
4th Nov 2020, 9:44 PM
Ratnapal Shende
Ratnapal Shende - avatar