Program for Legendre’s Conjecture using Python

Introduction

There is always one prime between n2 and (n+1)2 . The task is to print all the prime number between the range n2 to (n+1)2 .

Program for Legendre’s Conjecture using Python 

 

Program

import math 

def check( ip_num ):

    n = 2

    for n in range (2, int((math.sqrt(ip_num)+1))):

        if ip_num % n == 0:

            return False

    return True     

def find( ip_num ):

    print ( "Prime numbers between {0} and {1} are: ".format(ip_num*ip_num, ((ip_num+1)*(ip_num+1))+1) )              

    for n in range (ip_num*ip_num,(((ip_num+1)*(ip_num+1))+1)):

        if(check(n)):

            print(n)             

ip_num = int(input("Enter the number: "))

find(ip_num)

Output

Program for Legendre’s Conjecture using Python Output

Explanation

In the above python code, we have created a function check() to get the prime numbers between the range 2 to sqrt(n) + 1. If the number is prime the value is printed on the screen.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.