I need help on getting solution for the below python code. The code gives the required output which is that it dispaly number of prime number that user wants.
if user inputs 4
Actual Output: 2, 3 , 5 , 7

The main question is that I do not have idea on how to give rank to those prime numbers.

For Example:

The 1st prime number is 2
The 2nd prime number is 3
The 3rd prime number is 5
The 4th prime number is 7

and so on.

Can anyone please help to write the code to get above result. It would be of great help. Thank You.

   def isprime():
        n = int(input("How many prime numbers do you want?"))
        counter = 0
             i = 1
             while True:
                   c = 0
                   for j in range(1, (i + 1), 1):
                      a = i % j
                      if (a == 0):
                         c = c + 1
                  if (c == 2):
                    counter = counter + 1
                   if counter >= n:
                   i = i + 1

Recommended Answers

All 2 Replies

Let's break it down to prime number generation and then what I'm guessing is how to convert from 1, 2, 3 to 1st, 2nd and 3rd and so on.

Prime numbers is a rather done deal. I see 14 categories of Prime number at https://rosettacode.org/wiki/Category:Prime_Numbers
To get prime numbers has over 140 solutions at https://rosettacode.org/wiki/Sieve_of_Eratosthenes so you can adapt that to stop when you reach so many.

Next examples of 1,2,3 to 1st, 2nd, 3rd. These are ordinal numbers. I didn't find a Rosetta code but a coding challenge at https://codegolf.stackexchange.com/questions/4707/outputting-ordinal-numbers-1st-2nd-3rd#answer-4712
and some of the codes are very short.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.