Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Python Program for Legendre's Conjecture
Next article icon

Python Program for Legendre's Conjecture

Last Updated : 27 Jul, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

It says that there is always one prime number between any two consecutive natural number\'s(n = 1, 2, 3, 4, 5, ...) square. This is called Legendre's Conjecture. Conjecture: A conjecture is a proposition or conclusion based upon incomplete information to which no proof has been found i.e it has not been proved or disproved.

Mathematically, there is always one prime p in the range n^2        to (n + 1)^2        where n is any natural number. for examples- 2 and 3 are the primes in the range 1^2        to 2^2        . 5 and 7 are the primes in the range 2^2        to 3^2        . 11 and 13 are the primes in the range 3^2        to 4^2        . 17 and 19 are the primes in the range 4^2        to 5^2        .

Examples:

Input : 4 
output: Primes in the range 16 and 25 are:
17
19
23

Explanation: Here 42 = 16 and 52 = 25 Hence, prime numbers between 16 and 25 are 17, 19 and 23.

Input : 10
Output: Primes in the range 100 and 121 are:
101
103
107
109
113
Python3
# Python program to verify Legendre's Conjecture  # for a given n   import math   def isprime( n ):           i = 2     for i in range (2, int((math.sqrt(n)+1))):          if n%i == 0:              return False     return True      def LegendreConjecture( n ):      print ( "Primes in the range ", n*n              , " and ", (n+1)*(n+1)              , " are:" )                        for i in range (n*n, (((n+1)*(n+1))+1)):          if(isprime(i)):              print (i)               n = 50 LegendreConjecture(n)   # Contributed by _omg  

Output :

Primes in the range 2500 and 2601 are:
2503
2521
2531
2539
2543
2549
2551
2557
2579
2591
2593

Time Complexity: O(n*sqrtn). isPrime() function takes O(n) time and it is embedded in LegendreConjecture() function which also takes O(n) time as it has loop which starts from n2 and ends at 
(n+1)2   so,  (n+1)2 - n2 = 2n+1.

Auxiliary Space: O(1)

METHOD 2: 

Instead of iterating through a range of numbers to check for prime numbers, the new approach uses the sympy library to generate all prime numbers in a given range.

  • The sympy library provides the primesieve function, which takes two arguments: the start and end values for the range to search for primes.
  • The primesieve function returns a generator object that can be iterated over to get all the prime numbers in the specified range.
  • The isprime function is no longer necessary, as the primesieve function has already generated all prime numbers in the range.
  • The LegendreConjecture function remains the same, except for the for loop, which now iterates over the prime numbers.

NOTE: First you will have to install the sympy library using the following command: pip install sympy

Python3
import sympy  def LegendreConjecture(n):       # Find all prime numbers in the given range     primes = list(sympy.primerange(n*n, (n+1)*(n+1)))          # Print the prime numbers     print(f"Primes in the range {n*n} and {(n+1)*(n+1)} are:\n")     for i in range(len(primes)):       print(primes[i])      n = 50 LegendreConjecture(n)  # Contributed by adityasha4x71 

Output :

Primes in the range 2500 and 2601 are:2503
2521
2531
2539
2543
2549
2551
2557
2579
2591
2593

Time complexity: O(n log log n), as the sieve of Eratosthenes algorithm has a time complexity of O(n log log n) for finding all primes up to n, and the algorithm used here is a modified version of the sieve of Eratosthenes.

Auxiliary Space: O(n), because the algorithm uses a boolean list of size n+1 to keep track of whether each number is prime or not.

Please refer complete article on Legendre's Conjecture for more details!


Next Article
Python Program for Legendre's Conjecture

K

kartik
Improve
Article Tags :
  • DSA
  • Python Programs

Similar Reads

    Perfect Number Program in Python
    A Perfect Number is a positive integer that is equal to the sum of its proper divisors, excluding the number itself.Example:6 is a perfect number because its divisors (excluding itself) are 1, 2, and 3, and their sum is 6 (1 + 2 + 3 = 6).28 is a perfect number because its divisors are 1, 2, 4, 7, 14
    3 min read
    Python Program to Find LCM of Two Numbers
    We are given two numbers and our task is to find the LCM of two numbers in Python. In this article, we'll discuss different approaches to finding the LCM of two numbers in Python.Example:Input: a = 12, b = 15Output: 60Explanation: LCM of 12 and 15 is 60Python Program to Find LCM of Two NumbersBelow
    3 min read
    Check Prime Number in Python
    Given a positive integer N, the task is to write a Python program to check if the number is Prime or not in Python. For example, given a number 29, it has no divisors other than 1 and 29 itself. Hence, it is a prime number.Note: Negative numbers (e.g. -13) are not considered prime number.Using sympy
    7 min read
    Python Program to Print all Prime numbers in an Interval
    The task of printing all prime numbers in an interval in Python involves taking two input values representing a range [x, y] and finding all prime numbers within that range. A prime number is a natural number greater than 1 that is divisible only by 1 and itself. For example, in the interval [2, 7],
    4 min read
    Python Conditional Statement and Loops Coding Problems
    Welcome to this article on Python conditional statements problems, where we’ll practice solving a variety of coding challenges that focus on conditional statements and loops. You’ll work on problems like If Conditional Statement, Mark Even and Odd, The FizzBuzz Program, Leap Year, Factorial, GCD, LC
    1 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences