The Wayback Machine - https://web.archive.org/web/20241106213649/https://www.geeksforgeeks.org/euclid-euler-theorem/
Open In App

Euclid Euler Theorem

Last Updated : 16 Dec, 2022
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

According to Euclid Euler Theorem, a perfect number which is even, can be represented in the form (2^n - 1)*(2^n / 2) ))              where n is a prime number and 2^n - 1              is a Mersenne prime number. It is a product of a power of 2 with a Mersenne prime number. This theorem establishes a connection between a Mersenne prime and an even perfect number.
 

Some Examples (Perfect Numbers) which satisfy Euclid Euler Theorem are:

6, 28, 496, 8128, 33550336, 8589869056, 137438691328

Explanations:
1) 6 is an even perfect number.
So, it can be written in the form 
(22 - 1) * (2(2 - 1)) = 6
where n = 2 is a prime number and 2^n - 1 = 3 is a Mersenne prime number.

2) 28 is an even perfect number.
So, it can be written in the form 
(23 - 1) * (2(3 - 1)) = 28
where n = 3 is a prime number and 2^n - 1 = 7 is a Mersenne prime number.

3) 496 is an even perfect number.
So, it can be written in the form 
(25 - 1) * (2(5 - 1)) = 496
where n = 5 is a prime number and 2^n - 1 = 31 is a Mersenne prime number.


Approach(Brute Force):
Take each prime number and form a Mersenne prime with it. Mersenne prime =              where n is prime. Now form the number (2^n – 1)*(2^(n – 1)) and check if it is even and perfect. If the condition satisfies then it follows Euclid Euler Theorem. 
 

C++

// CPP code to verify Euclid Euler Theorem
#include <bits/stdc++.h>
using namespace std;
 
#define show(x) cout << #x << " = " << x << "\n";
 
bool isprime(long long n)
{
    // check whether a number is prime or not
    for (int i = 2; i * i <= n; i++)
        if (n % i == 0)
            return false;
    return true;
}
 
bool isperfect(long long n) // perfect numbers
{
    // check is n is perfect sum of divisors
    // except the number itself = number
    long long s = -n;
    for (long long i = 1; i * i <= n; i++) {
 
        // is i is a divisor of n
        if (n % i == 0) {
            long long factor1 = i, factor2 = n / i;
            s += factor1 + factor2;
 
            // here i*i == n
            if (factor1 == factor2)
                s -= i;
        }
    }
    return (n == s);
}
 
int main()
{
    // storing powers of 2 to access in O(1) time
    vector<long long> power2(61);
    for (int i = 0; i <= 60; i++)
        power2[i] = 1LL << i;
 
    // generation of first few numbers
    // satisfying Euclid Euler's theorem
 
    cout << "Generating first few numbers "
            "satisfying Euclid Euler's theorem\n";
    for (long long i = 2; i <= 25; i++) {
        long long no = (power2[i] - 1) * (power2[i - 1]);
        if (isperfect(no) and (no % 2 == 0))
            cout << "(2^" << i << " - 1) * (2^(" << i
                 << " - 1)) = " << no << "\n";
    }
    return 0;
}

                    

Java

// Java code to verify Euclid Euler Theorem
class GFG
{
    static boolean isprime(long n)
    {
        // check whether a number is prime or not
        for (int i = 2; i * i <= n; i++)
        {
            if (n % i == 0)
            {
                return false;
            }
        }
        return false;
    }
 
    static boolean isperfect(long n) // perfect numbers
    {
        // check is n is perfect sum of divisors
        // except the number itself = number
        long s = -n;
        for (long i = 1; i * i <= n; i++)
        {
 
            // is i is a divisor of n
            if (n % i == 0)
            {
                long factor1 = i, factor2 = n / i;
                s += factor1 + factor2;
 
                // here i*i == n
                if (factor1 == factor2)
                {
                    s -= i;
                }
            }
        }
        return (n == s);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        // storing powers of 2 to access in O(1) time
        long power2[] = new long[61];
        for (int i = 0; i <= 60; i++)
        {
            power2[i] = 1L << i;
        }
 
        // generation of first few numbers
        // satisfying Euclid Euler's theorem
        System.out.print("Generating first few numbers " +
                         "satisfying Euclid Euler's theorem\n");
        for (int i = 2; i <= 25; i++)
        {
            long no = (power2[i] - 1) * (power2[i - 1]);
            if (isperfect(no) && (no % 2 == 0))
            {
                System.out.print("(2^" + i + " - 1) * (2^(" +
                                 i + " - 1)) = " + no + "\n");
            }
        }
    }
}
 
// This code is contributed by PrinciRaj1992

                    

Python3

# Python3 code to verify Euclid Euler Theorem
#define show(x) cout << #x << " = " << x << "\n";
def isprime(n):
    i = 2
 
    # check whether a number is prime or not
    while(i * i <= n):
        if (n % i == 0):
            return False;
        i += 1
    return False;
 
def isperfect(n): # perfect numbers
 
    # check is n is perfect sum of divisors
    # except the number itself = number
    s = -n;
    i =1
    while(i * i <= n):
 
        # is i is a divisor of n
        if (n % i == 0):
            factor1 = i
            factor2 = n // i;
            s += factor1 + factor2;
 
            # here i*i == n
            if (factor1 == factor2):
                s -= i;   
        i += 1
    return (n == s);
 
# Driver code
if __name__=='__main__':
 
    # storing powers of 2 to access in O(1) time
    power2 = [1<<i for i in range(61)]
 
    # generation of first few numbers
    # satisfying Euclid Euler's theorem
    print("Generating first few numbers satisfying Euclid Euler's theorem");
    for i in range(2, 26):  
        no = (power2[i] - 1) * (power2[i - 1]);
        if (isperfect(no) and (no % 2 == 0)):
            print("(2^{} - 1) * (2^({} - 1)) = {}".format(i, i, no))
     
    # This code is contributed by rutvik_56.

                    

C#

// C# code to verify Euclid Euler Theorem
using System;
using System.Collections.Generic;
     
class GFG
{
    static Boolean isprime(long n)
    {
        // check whether a number is prime or not
        for (int i = 2; i * i <= n; i++)
        {
            if (n % i == 0)
            {
                return false;
            }
        }
        return false;
    }
 
    static Boolean isperfect(long n) // perfect numbers
    {
        // check is n is perfect sum of divisors
        // except the number itself = number
        long s = -n;
        for (long i = 1; i * i <= n; i++)
        {
 
            // is i is a divisor of n
            if (n % i == 0)
            {
                long factor1 = i, factor2 = n / i;
                s += factor1 + factor2;
 
                // here i*i == n
                if (factor1 == factor2)
                {
                    s -= i;
                }
            }
        }
        return (n == s);
    }
 
    // Driver Code
    public static void Main(String[] args)
    {
        // storing powers of 2 to access in O(1) time
        long []power2 = new long[61];
        for (int i = 0; i <= 60; i++)
        {
            power2[i] = 1L << i;
        }
 
        // generation of first few numbers
        // satisfying Euclid Euler's theorem
        Console.Write("Generating first few numbers " +
                      "satisfying Euclid Euler's theorem\n");
        for (int i = 2; i <= 25; i++)
        {
            long no = (power2[i] - 1) * (power2[i - 1]);
            if (isperfect(no) && (no % 2 == 0))
            {
                Console.Write("(2^" + i + " - 1) * (2^(" +
                                i + " - 1)) = " + no + "\n");
            }
        }
    }
}
 
// This code is contributed by Rajput-Ji

                    

PHP

<?php
// PHP code to verify
// Euclid Euler Theorem
 
// define show(x)
// cout << #x << " = " << x << "\n";
 
function isprime($n)
{
    // check whether a number
    // is prime or not
    for ($i = 2; $i * $i <= $n; $i++)
        if ($n % $i == 0)
            return false;
    return false;
}
 
function isperfect($n) // perfect numbers
{
    // check is n is perfect sum
    // of divisors except the
    // number itself = number
    $s = -$n;
    for ($i = 1;
         $i * $i <= $n; $i++)
    {
 
        // is i is a divisor of n
        if ($n % $i == 0)
        {
            $factor1 = $i;
            $factor2 = $n / $i;
            $s += $factor1 + $factor2;
 
            // here i*i == n
            if ($factor1 == $factor2)
                $s -= $i;
        }
    }
    return ($n == $s);
}
 
// Driver code
 
// storing powers of 2 to
// access in O(1) time
$power2 = array();
for ($i = 0; $i <= 60; $i++)
    $power2[$i] = 1<< $i;
 
// generation of first few
// numbers satisfying Euclid
// Euler's theorem
echo "Generating first few numbers " .
     "satisfying Euclid Euler's theorem\n";
      
for ($i = 2; $i <= 25; $i++)
{
    $no = ($power2[$i] - 1) *
          ($power2[$i - 1]);
    if (isperfect($no) &&
                 ($no % 2 == 0))
        echo "(2^" . $i . " - 1) * (2^(" .
                     $i . " - 1)) = " .
                     $no . "\n";
}
 
// This code is contributed by mits
?>

                    

Javascript

<script>
 
// JavaScript program to verify Euclid Euler Theorem
 
    function isprime(n)
    {
        // check whether a number is prime or not
        for (let i = 2; i * i <= n; i++)
        {
            if (n % i == 0)
            {
                return false;
            }
        }
        return false;
    }
  
    function isperfect(n) // perfect numbers
    {
        // check is n is perfect sum of divisors
        // except the number itself = number
        let s = -n;
        for (let i = 1; i * i <= n; i++)
        {
  
            // is i is a divisor of n
            if (n % i == 0)
            {
                let factor1 = i, factor2 = n / i;
                s += factor1 + factor2;
  
                // here i*i == n
                if (factor1 == factor2)
                {
                    s -= i;
                }
            }
        }
        return (n == s);
    }
  
// Driver code
     
         // storing powers of 2 to access in O(1) time
        let power2 = [];
        for (let i = 0; i <= 60; i++)
        {
            power2[i] = 1 << i;
        }
  
        // generation of first few numbers
        // satisfying Euclid Euler's theorem
        document.write("Generating first few numbers " +
                         "satisfying Euclid Euler's theorem" + "<br/>");
        for (let i = 2; i <= 25; i++)
        {
            let no = (power2[i] - 1) * (power2[i - 1]);
            if (isperfect(no) && (no % 2 == 0))
            {
                document.write("(2^" + i + " - 1) * (2^(" +
                                 i + " - 1)) = " + no + "<br/>");
            }
        }
     
    // This code is contributed by code_hunt.
</script>

                    

Output: 
Generating first few numbers satisfying Euclid Euler's theorem
(2^2 - 1) * (2^(2 - 1)) = 6
(2^3 - 1) * (2^(3 - 1)) = 28
(2^5 - 1) * (2^(5 - 1)) = 496
(2^7 - 1) * (2^(7 - 1)) = 8128
(2^13 - 1) * (2^(13 - 1)) = 33550336
(2^17 - 1) * (2^(17 - 1)) = 8589869056
(2^19 - 1) * (2^(19 - 1)) = 137438691328

 

Time Complexity: O(sqrt(n))
Auxiliary Space: O(1) 



Previous Article
Next Article

Similar Reads

Check whether the given number is Euclid Number or not
Given a positive integer n, the task is to check if it is Euclid Number or not. Print ‘YES’ if the given number is Euclid Number otherwise print ‘NO'. Euclid number : In Mathematics, Euclid numbers are integers of the form - [Tex]E_{n} = p_{n}\# + 1 [/Tex]where [Tex]p_{n}\# [/Tex]is product of first n prime numbers.The first few Euclid numbers are-
15 min read
Euclid–Mullin Sequence
Given an integer N, the task is to print the first N elements of the Euclid-Mullin Sequence. The Euclid-Mullin sequence is a sequence of prime numbers where each element is the least prime factor of one plus the product of all earlier elements. The sequence is named after the ancient Greek mathematician Euclid. Examples: Input: N = 14 Output: 2 3 7
6 min read
Euclid's Algorithm when % and / operations are costly
Euclid's algorithm is used to find GCD of two numbers. There are mainly two versions of algorithm. Version 1 (Using subtraction) C/C++ Code // Recursive function to return gcd of a and b int gcd(int a, int b) { if (a == b) return a; return (a > b) ? gcd(a - b, b) : gcd(a, b - a); } C/C++ Code // Recursive function to return gcd of a and b int gc
9 min read
Euclid's lemma
We are given two numbers x and y. We know that a number p divides their product. Can we say for sure that p also divides one of them? The answer is no. For example, consider x = 15, y = 6 and p = 9. p divides the product 15*6, but doesn't divide any of them. What if p is prime? Euclid's lemma states that if a prime p divides the product of two numb
1 min read
Cook–Levin theorem or Cook's theorem
In computational complexity theory, the Cook–Levin theorem, also known as Cook's theorem, states that the Boolean satisfiability problem is NP-complete. That is, it is in NP, and any problem in NP can be reduced in polynomial time by a deterministic Turing machine to the Boolean satisfiability problem. Stephen Arthur Cook and L.A. Levin in 1973 ind
4 min read
Compute nCr % p | Set 4 (Chinese Remainder theorem with Lucas Theorem)
Given three numbers n, r and p, the task is to compute the value of nCr % p. Note: p is a square-free number and the largest prime factor of p â‰Ī 50. Examples: Input: n = 10, r = 2, p = 13Output: 6Explanation: 10C2 is 45 and 45 % 13= 6 Input: n = 6, r = 2, p = 13Output: 2 Approach: We have already discussed several other approaches of this problem.
10 min read
Euler Method for solving differential equation
Given a differential equation dy/dx = f(x, y) with initial condition y(x0) = y0. Find its approximate solution using Euler method. Euler Method : In mathematics and computational science, the Euler method (also called forward Euler method) is a first-order numerical procedure for solving ordinary differential equations (ODEs) with a given initial v
6 min read
Euler's Four Square Identity
According to Euler's four square identity, the product of any two numbers a and b can be expressed as a sum of four squares if a and b both can individually be expressed as the sum of four squares.Mathematically, if a = [Tex]c1^2 + c2^2 + c3^2 + c4^2 [/Tex]and b = [Tex]d1^2 + d2^2 + d3^2 + d4^2 [/Tex]Then, a * b = [Tex]e1^2 + e2^2 + e3^2 + e4^2 [/T
15+ min read
Euler Tour of Tree
A Tree is a generalization of connected graph where it has N nodes that will have exactly N-1 edges, i.e one edge between every pair of vertices. Find the Euler tour of tree represented by adjacency list. Examples: Input : Output : 1 2 3 2 4 2 1 Input : Output : 1 5 4 2 4 3 4 5 1 Euler tour is defined as a way of traversing tree such that each vert
6 min read
Euler tour of Binary Tree
Given a binary tree where each node can have at most two child nodes, the task is to find the Euler tour of the binary tree. Euler tour is represented by a pointer to the topmost node in the tree. If the tree is empty, then value of root is NULL. Examples: Input : Output: 1 5 4 2 4 3 4 5 1 Approach: First, start with root node 1, Euler[0]=1 Go to l
7 min read
Total nodes traversed in Euler Tour Tree
Euler tour of tree has been already discussed which flattens the hierarchical structure of tree into array which contains exactly 2*N-1 values. In this post, the task is to prove that the degree of Euler Tour Tree is 2 times the number of nodes minus one. Here degree means the total number of nodes get traversed in Euler Tour. Examples: Input: Outp
7 min read
Predictor-Corrector or Modified-Euler method for solving Differential equation
For a given differential equation [Tex]\frac{dy}{dx}=f(x, y) [/Tex]with initial condition [Tex]y(x_0)=y_0 [/Tex]find the approximate solution using Predictor-Corrector method.Predictor-Corrector Method : The predictor-corrector method is also known as Modified-Euler method. In the Euler method, the tangent is drawn at a point and slope is calculate
13 min read
Count integers in a range which are divisible by their euler totient value
Given 2 integers L and R, the task is to find out the number of integers in the range [L, R] such that they are completely divisible by their Euler totient value.Examples: Input: L = 2, R = 3 Output: 1 [Tex] [/Tex](2) = 2 => 2 % [Tex] [/Tex](2) = 0 [Tex] [/Tex](3) = 2 => 3 % [Tex] [/Tex](3) = 1 Hence 2 satisfies the condition.Input: L = 12, R
6 min read
Euler zigzag numbers ( Alternating Permutation )
Euler Zigzag numbers is a sequence of integers which is a number of arrangements of those numbers so that each entry is alternately greater or less than the preceding entry.c1, c2, c3, c4 is Alternating permutation where c1 < c2 c3 < c2 c3 < c4...zigzag numbers are as follows 1, 1, 1, 2, 5, 16, 61, 272, 1385, 7936, 50521 ......For a given
7 min read
Check if a number is Euler Pseudoprime
Given an integer N and a base number A, the task is to check whether N is a Euler Pseudoprime to the given base A. An integer N is called Euler Pseudoprime to the base A, if A > 0 and N is an odd composite number.A and N are co-prime i.e. GCD(A, N) = 1.A(N - 1) / 2 % N is either 1 or N - 1. Examples: Input: N = 121, A = 3 Output: YesInput: N = 3
9 min read
Euler's Factorization method
Given a number N, the task is to find the factors of N. Examples: Input: N = 1000009 Output: 293 3413 Explanation: 293 * 3413 = 1000009Input: N = 100000 Output: 800 125 Explanation: 800 * 125 = 100000 Euler's Factorization method: Euler's factorization method works on the principle that all the numbers N which can be written as the sum of two power
15+ min read
Conversion of an Undirected Graph to a Directed Euler Circuit
Given an undirected graph with V nodes (say numbered from 1 to V) and E edges, the task is to check whether the graph is an Euler Graph or not and if so then convert it into a Directed Euler Circuit. A Directed Euler Circuit is a directed graph such that if you start traversing the graph from any node and travel through each edge exactly once you w
10 min read
Count of elements having Euler's Totient value one less than itself
Given an array arr[] of N integers, and a range L to R, the task is to find the total number of elements in the array from index L to R that satisfies the following condition: [Tex]F(arr[i]) = arr[i] - 1 [/Tex] where F(x) is Euler's Totient Function. Examples: Input: arr[] = {2, 4, 5, 8}, L = 1, R = 3 Output: 2 Explanation: Here in the given range,
9 min read
Probability of Euler's Totient Function in a range [L, R] to be divisible by M
Given three integers L, R, and M, the task is to find the probability of Euler's Totient Function of a number in the range [L, R] is divisible by M. Euler’s Totient function is the count of numbers in {1, 2, 3, â€Ķ, N} that are relatively prime to N, i.e., the numbers whose GCD (Greatest Common Divisor) with N is 1. Examples: Input: L = 1, R = 5, M =
12 min read
Check if Euler Totient Function is same for a given number and twice of that number
Given an integer N, the task is to check whether the Euler’s Totient Function of N and 2 * N are the same or not. If they are found to be the same, then print “Yes”. Otherwise, print “No”. Examples: Input: N = 9 Output: Yes Explanation: Let phi() be the Euler Totient function Since 1, 2, 4, 5, 7, 8 have GCD 1 with 9. Therefore, phi(9) = 6. Since 1,
7 min read
Generate an array having sum of Euler Totient Function of all elements equal to N
Given a positive integer N, the task is to generate an array such that the sum of the Euler Totient Function of each element is equal to N. Examples: Input: N = 6Output: 1 6 2 3 Input: N = 12Output: 1 12 2 6 3 4 Approach: The given problem can be solved based on the divisor sum property of the Euler Totient Function, i.e., The Euler Totient Functio
5 min read
Sum of Euler Totient Functions obtained for each divisor of N
Given a positive integer N, the task is to find the sum of the Euler Totient Function for all the divisor of the given number N. Examples: Input: N = 3Output: 3Explanation:Divisors of 3 are {1, 3}. The Euler totient function for the values 1 and 3 are 1 and 2 respectively.Therefore, the required sum is 1 + 2 = 3. Input: N = 6Output: 6 Naive Approac
3 min read
Euler Circuit in a Directed Graph
Eulerian Path is a path in graph that visits every edge exactly once. Eulerian Circuit is an Eulerian Path which starts and ends on the same vertex. A graph is said to be eulerian if it has a eulerian cycle. We have discussed eulerian circuit for an undirected graph. In this post, the same is discussed for a directed graph. For example, the followi
13 min read
Euler's Totient function for all numbers smaller than or equal to n
Euler's Totient function ?(n) for an input n is the count of numbers in {1, 2, 3, ..., n} that are relatively prime to n, i.e., the numbers whose GCD (Greatest Common Divisor) with n is 1. For example, ?(4) = 2, ?(3) = 2 and ?(5) = 4. There are 2 numbers smaller or equal to 4 that are relatively prime to 4, 2 numbers smaller or equal to 3 that are
13 min read
Euler's criterion (Check if square root under modulo p exists)
Given a number 'n' and a prime p, find if square root of n under modulo p exists or not. A number x is square root of n under modulo p if (x*x)%p = n%p. Examples : Input: n = 2, p = 5 Output: false There doesn't exist a number x such that (x*x)%5 is 2 Input: n = 2, p = 7 Output: true There exists a number x such that (x*x)%7 is 2. The number is 3.
11 min read
Project Euler
What is Project Euler? Project Euler is a series of challenging problems that require mathematical and programming skills. Somebody who enjoys learning new area of mathematics, project Euler is going to be a fun journey. Where are the problems ? The problems are right here in their official archive. Let's solve a problem from the archive and unders
3 min read
Minimum edges required to add to make Euler Circuit
Given a undirected graph of n nodes and m edges. The task is to find minimum edges required to make Euler Circuit in the given graph. Examples: Input : n = 3, m = 2 Edges[] = {{1, 2}, {2, 3}} Output : 1By connecting 1 to 3, we can create a Euler Circuit. For a Euler Circuit to exist in the graph we require that every node should have even degree be
11 min read
Optimized Euler Totient Function for Multiple Evaluations
Euler Totient Function (ETF) ?(n) for an input n is count of numbers in {1, 2, 3, â€Ķ, n} that are relatively prime to n, i.e., the numbers whose GCD (Greatest Common Divisor) with n is 1. Examples: ?(5) = 4 gcd(1, 5) is 1, gcd(2, 5) is 1, gcd(3, 5) is 1 and gcd(4, 5) is 1 ?(6) = 2 gcd(1, 6) is 1 and gcd(5, 6) is 1, Recommended PracticeEuler Totient
9 min read
Euler Totient Function in Python
Euler’s Totient function, also known as Euler's Phi Function ÎĶ (n) is a mathematical function that counts the number of positive integers up to a given integer n that are relatively prime to n. i.e., the numbers whose GCD (Greatest Common Divisor) with n is 1. Examples : Input: ÎĶ(1) = 1 Output: gcd(1, 1) is 1 Input: ÎĶ(2) = 1Output: gcd(1, 2) is 1,
4 min read
Euler's Totient Function
Euler's Totient function ÎĶ(n) for an input n is the count of numbers in {1, 2, 3, ..., n-1} that are relatively prime to n, i.e., the numbers whose GCD (Greatest Common Divisor) with n is 1. Examples : ÎĶ(1) = 1 gcd(1, 1) is 1ÎĶ(2) = 1gcd(1, 2) is 1, but gcd(2, 2) is 2.ÎĶ(3) = 2gcd(1, 3) is 1 and gcd(2, 3) is 1ÎĶ(4) = 2gcd(1, 4) is 1 and gcd(3, 4) is 1
15+ min read
three90RightbarBannerImg