Check whether the given number is Euclid Number or not in Python

A Euclid number is an integer that can be represented as the product of the first n prime numbers plus 1. In mathematical terms, a Euclid number follows the formula:

En = Pn + 1

where Pn is the product of the first n prime numbers. For example, 211 is a Euclid number because it can be expressed as (2×3×5×7) + 1 = 210 + 1 = 211.

Algorithm

To check if a number is a Euclid number, we need to ?

  • Generate all prime numbers up to a reasonable limit using the Sieve of Eratosthenes
  • Calculate products of consecutive primes starting from 2
  • Check if adding 1 to any product equals our target number

Implementation

Step 1: Prime Number Generation

First, we generate all prime numbers using the Sieve of Eratosthenes algorithm ?

MAX = 10000
primes = []

def generate_all_primes():
    prime = [True] * MAX
    
    x = 2
    while x * x < MAX:
        if prime[x] == True:
            for i in range(x * 2, MAX, x):
                prime[i] = False
        x += 1
    
    for x in range(2, MAX):
        if prime[x]:
            primes.append(x)

# Generate primes for testing
generate_all_primes()
print(f"First 10 primes: {primes[:10]}")
First 10 primes: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]

Step 2: Checking Euclid Numbers

Now we check if a given number is a Euclid number by calculating products of consecutive primes ?

MAX = 10000
primes = []

def generate_all_primes():
    prime = [True] * MAX
    
    x = 2
    while x * x < MAX:
        if prime[x] == True:
            for i in range(x * 2, MAX, x):
                prime[i] = False
        x += 1
    
    for x in range(2, MAX):
        if prime[x]:
            primes.append(x)

def is_euclid_number(n):
    generate_all_primes()
    product = 1
    i = 0
    
    while product < n:
        product = product * primes[i]
        if product + 1 == n:
            return True
        i += 1
    return False

# Test with example numbers
test_numbers = [3, 7, 31, 211, 2311]

for num in test_numbers:
    result = is_euclid_number(num)
    print(f"{num} is {'a' if result else 'not a'} Euclid number")
3 is a Euclid number
7 is a Euclid number
31 is a Euclid number
211 is a Euclid number
2311 is a Euclid number

How It Works

The algorithm works by systematically building products of consecutive primes ?

  • E1 = 2 + 1 = 3 (first prime)
  • E2 = (2×3) + 1 = 7 (first two primes)
  • E3 = (2×3×5) + 1 = 31 (first three primes)
  • E4 = (2×3×5×7) + 1 = 211 (first four primes)

Complete Example

Here's the complete solution with detailed output ?

MAX = 10000
primes = []

def generate_all_primes():
    prime = [True] * MAX
    
    x = 2
    while x * x < MAX:
        if prime[x] == True:
            for i in range(x * 2, MAX, x):
                prime[i] = False
        x += 1
    
    for x in range(2, MAX):
        if prime[x]:
            primes.append(x)

def check_euclid_number(n):
    generate_all_primes()
    product = 1
    i = 0
    
    print(f"Checking if {n} is a Euclid number:")
    
    while product < n:
        product = product * primes[i]
        current_euclid = product + 1
        print(f"Product of first {i+1} prime(s): {product}, Euclid number: {current_euclid}")
        
        if current_euclid == n:
            return True
        i += 1
    return False

# Test with 211
n = 211
result = check_euclid_number(n)
print(f"\nResult: {n} is {'a' if result else 'not a'} Euclid number")
Checking if 211 is a Euclid number:
Product of first 1 prime(s): 2, Euclid number: 3
Product of first 2 prime(s): 6, Euclid number: 7
Product of first 3 prime(s): 30, Euclid number: 31
Product of first 4 prime(s): 210, Euclid number: 211

Result: 211 is a Euclid number

Conclusion

Euclid numbers are formed by adding 1 to the product of consecutive primes. The algorithm efficiently checks this by generating primes using the Sieve of Eratosthenes and testing products until we find a match or exceed the target number.

---
Updated on: 2026-03-25T14:40:00+05:30

325 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements