V-Lab @ ANDC

Generate nth Prime Number

Aim

To generate nth Prime number

Theory

A prime number is a natural number greater than 1 and is divisible by 1 or itself only.


The nth prime number will be the nth position in the list of natural numbers after removing all the composites.

For example:-

1st prime number is 2.


2nd prime number is 3.


4th prime number is 7.

Psuedocode

            #Function to generate nth Prime Number
            nthPrime(n):
              initialize counter variable to 0 and  num variable to 1 
               while counter is less than n:
                    increment num by 1
                    for i=2 to num:
                    #Determines the modulo and compare it  to zero.
                        if(num%i==0):  
                            #breaks the loop if the above condition returns true  
                                break
                        if(num==i) :#if denominator is same as numerator 
                            increments the counter variable by 1 if the number is prime 
                    EndFor
                EndWhile 
                print("The nth prime numer is "+ num)   #Print the prime number
                    

The while loop:-


With the while loop, we can execute a set of statements as long as a condition is true


SYNTAX:-


While(condition):


//code block to be executed


The for loop:-


A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string).


SYNTAX:-


for counter in sequence:


//code block to be executed


The break statement :-


With the break statement, we can come out of the loop even if, while condition is true.


Procedure

Steps to follow are as follows:

  1. Click on the simulation tab.
  2. Read the code.
  3. Enter the position of the prime number to be generated as input.
  4. Click submit to get the output.


Practice


Description:

  1. A list of 10 natural numbers (1-10) is displayed.
  2. You can move from one number to another by clicking the next button.
  3. When you will reach to the Prime Number,it's correspodning value will be displayes in the text boxes.

    • If the number is not a prime number, then it will be coloured red and the boxes will be empty.
    • When you will reach to a prime number, then it will be coloured green and the first box will contain the position of the prime number. The box below the next button will have the value of the nth prime number.
    • The visited prime numbers will be coloured yellow.
1
2
3
4
5
6
7
8
9
10
n =
Nth Prime Number =

Python Code


QUIZ

Some Rules of this Quiz
1. You will have only 15 seconds per each question.
2. Once you select your answer, it can't be undone.
3. You can't select any option once time goes off.
4. You can't exit from the Quiz while you're playing.
5. You'll get points on the basis of your correct answers.
Awesome Quiz Application
Time Left
15
You've completed the Quiz!

Result

Hence, we have got an idea about prime numbers and can find the value of nth Prime Number using the above code.

References

  1. Urban,M. & Murach , J.(2018).Python programming.Shroff.
  2. Liang ,Y. D.(2013).Introduction to programming using python.Pearson.
  3. Taneja, S. & Kumar,N.(2018).Python Programming-A modular approach.Pearson

Team

Mr. Harsh Bamotra, B.Sc(H) Computer Science, II year.

Mentors:
Prof. Sharanjit Kaur,
Ms. Gunjan Rani