Legendre's Conjecture: Concept, Algorithm, Implementation in C++11 Feb 2025 | 6 min read Legendre's Conjecture is a statement that one prime number always exists between two natural numbers' squares, which are consecutive to each other. In this article, we will discuss Legendre's Conjecture with its algorithm and its implementation. Mathematical statement:There is a prime number p existing between any two numbers, i.e., n2^2 and (n+1)2^2. In this case, n is a whole number. A conjecture is a finding of a conclusion that doesn't have any mathematical proof. For this reason, Legendre's Conjecture requires no mathematical evidence. Problem Statement:For a given number n, where we will print each prime between the range of numbers from n^2 to (n+1)^2 from 1 to n. Approach:
Example 1:Let us take an example to illustrate the Legendre's Conjecture in C++. Filename: Legendre's.cpp Output: The Value of the number m: 5 For i: 1 Total prime numbers in the following range 1 and 4 = 2 For i: 2 Total prime numbers in the following range 4 and 9 = 2 For i: 3 Total prime numbers in the following range 9 and 16 = 2 For i: 4 Total prime numbers in the following range 16 and 25 = 3 For i: 5 Total prime numbers in the following range 25 and 36 = 2 For i: 6 Total prime numbers in the following range 36 and 49 = 4 For i: 7 Total prime numbers in the following range 49 and 64 = 3 For i: 8 Total prime numbers in the following range 64 and 81 = 4 Explanation:
Example 2: Efficient ApproachLet us take another example to illustrate the Legendre's Conjecture in C++ Filename: Legendre's2.cpp Output: The Value of the number is: 10 For i: 1 Total primes in the range 1 and 4 = 2 For i: 2 Total primes in the range 4 and 9 = 2 For i: 3 Total primes in the range 9 and 16 = 2 For i: 4 Total primes in the range 16 and 25 = 3 For i: 5 Total primes in the range 25 and 36 = 2 For i: 6 Total primes in the range 36 and 49 = 4 For i: 7 Total primes in the range 49 and 64 = 3 For i: 8 Total primes in the range 64 and 81 = 4 For i: 9 Total primes in the range 81 and 100 = 3 For i: 10 Total primes in the range 100 and 121 = 5 Explanation:
Conclusion:In conclusion, Legendre's conjecture is an exciting mathematical conjecture that states that at least one prime number exists between consecutive squares of the natural numbers. Each experiment serves as practical proof to the hypothetical by simply determining the prime number of integers within these numbers' squares ranges. The implementation, which the Sieve of Eratosthenes optimizes, substantially speeds up the naive strategy for values of num having a larger magnitude. Although Legendre's Conjecture has yet to be proven, the development of implementations provides practical tools to verify it within the available computational constraints. |
We request you to subscribe our newsletter for upcoming updates.

We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India