The Wayback Machine - https://web.archive.org/web/20240627104137/https://www.geeksforgeeks.org/multiplicative-order/
Open In App

Multiplicative order

Last Updated : 14 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In number theory, given an integer A and a positive integer N with gcd( A , N) = 1, the multiplicative order of a modulo N is the smallest positive integer k with A^k( mod N ) = 1. ( 0 < K < N ) 

Examples : 

Input : A = 4 , N = 7 
Output : 3
explanation :  GCD(4, 7) = 1  
               A^k( mod N ) = 1 ( smallest positive integer K )
               4^1 = 4(mod 7)  = 4
               4^2 = 16(mod 7) = 2
               4^3 = 64(mod 7)  = 1
               4^4 = 256(mod 7) = 4
               4^5 = 1024(mod 7)  = 2
               4^6 = 4096(mod 7)  = 1

smallest positive integer K = 3  

Input :  A = 3 , N = 1000 
Output : 100  (3^100 (mod 1000) == 1) 

Input : A = 4 , N = 11 
Output : 5 

If we take a close look then we observe that we do not need to calculate power every time. we can be obtaining next power by multiplying ‘A’ with the previous result of a module. 

Explanation : 
A = 4 , N = 11  
initially result = 1 
with normal                with modular arithmetic (A * result)
4^1 = 4 (mod 11 ) = 4  ||  4 * 1 = 4 (mod 11 ) = 4 [ result = 4]
4^2 = 16(mod 11 ) = 5  ||  4 * 4 = 16(mod 11 ) = 5 [ result = 5]
4^3 = 64(mod 11 ) = 9  ||  4 * 5 = 20(mod 11 ) = 9 [ result = 9]
4^4 = 256(mod 11 )= 3  ||  4 * 9 = 36(mod 11 ) = 3 [ result = 3]
4^5 = 1024(mod 5 ) = 1 ||  4 * 3 = 12(mod 11 ) = 1 [ result = 1]

smallest positive integer  5 

Run a loop from 1 to N-1 and Return the smallest +ve power of A under modulo n which is equal to 1. 

Below is the implementation of above idea.  

C++




// C++ program to implement multiplicative order
#include<bits/stdc++.h>
using namespace std;
 
// function for GCD
int GCD ( int a , int b )
{
    if (b == 0 )
        return a;
    return GCD( b , a%b ) ;
}
 
// Function return smallest +ve integer that
// holds condition A^k(mod N ) = 1
int multiplicativeOrder(int A, int  N)
{
    if (GCD(A, N ) != 1)
        return -1;
 
    // result store power of A that raised to
    // the power N-1
    unsigned int result = 1;
 
    int K = 1 ;
    while (K < N)
    {
        // modular arithmetic
        result = (result * A) % N ;
 
        // return smallest +ve integer
        if (result  == 1)
            return K;
 
        // increment power
        K++;
    }
 
    return -1 ;
}
 
//driver program to test above function
int main()
{
    int A = 4 , N = 7;
    cout << multiplicativeOrder(A, N);
    return 0;
}


Java




// Java program to implement multiplicative order
import java.io.*;
 
class GFG {
 
    // function for GCD
    static int GCD(int a, int b) {
         
        if (b == 0)
            return a;
             
        return GCD(b, a % b);
    }
     
    // Function return smallest +ve integer that
    // holds condition A^k(mod N ) = 1
    static int multiplicativeOrder(int A, int N) {
         
        if (GCD(A, N) != 1)
            return -1;
     
        // result store power of A that raised to
        // the power N-1
        int result = 1;
     
        int K = 1;
         
        while (K < N) {
             
            // modular arithmetic
            result = (result * A) % N;
         
            // return smallest +ve integer
            if (result == 1)
                return K;
         
            // increment power
            K++;
        }
     
        return -1;
    }
     
    // driver program to test above function
    public static void main(String args[]) {
         
        int A = 4, N = 7;
         
        System.out.println(multiplicativeOrder(A, N));
    }
}
 
/* This code is contributed by Nikita Tiwari.*/


Python3




# Python 3 program to implement
# multiplicative order
 
# function for GCD
def GCD (a, b ) :
    if (b == 0 ) :
        return a
    return GCD( b, a % b )
 
# Function return smallest + ve
# integer that holds condition
# A ^ k(mod N ) = 1
def multiplicativeOrder(A, N) :
    if (GCD(A, N ) != 1) :
        return -1
 
    # result store power of A that raised
    # to the power N-1
    result = 1
 
    K = 1
    while (K < N) :
     
        # modular arithmetic
        result = (result * A) % N
 
        # return smallest + ve integer
        if (result == 1) :
            return K
 
        # increment power
        K = K + 1
     
    return -1
     
# Driver program
A = 4
N = 7
print(multiplicativeOrder(A, N))
 
# This code is contributed by Nikita Tiwari.


C#




// C# program to implement multiplicative order
using System;
 
class GFG {
 
    // function for GCD
    static int GCD(int a, int b)
    {
         
        if (b == 0)
            return a;
             
        return GCD(b, a % b);
    }
     
    // Function return smallest +ve integer
    // that holds condition A^k(mod N ) = 1
    static int multiplicativeOrder(int A, int N)
    {
         
        if (GCD(A, N) != 1)
            return -1;
     
        // result store power of A that
        // raised to the power N-1
        int result = 1;
     
        int K = 1;
         
        while (K < N)
        {
             
            // modular arithmetic
            result = (result * A) % N;
         
            // return smallest +ve integer
            if (result == 1)
                return K;
         
            // increment power
            K++;
        }
     
        return -1;
    }
     
    // Driver Code
    public static void Main()
    {
         
        int A = 4, N = 7;
         
        Console.Write(multiplicativeOrder(A, N));
    }
}
 
// This code is contributed by Nitin Mittal.


PHP




<?php
// PHP program to implement
// multiplicative order
 
// function for GCD
function GCD ( $a , $b )
{
    if ($b == 0 )
        return $a;
    return GCD( $b , $a % $b ) ;
}
 
// Function return smallest
// +ve integer that holds
// condition A^k(mod N ) = 1
function multiplicativeOrder($A, $N)
{
    if (GCD($A, $N ) != 1)
        return -1;
 
    // result store power of A
    // that raised to the power N-1
    $result = 1;
 
    $K = 1 ;
    while ($K < $N)
    {
        // modular arithmetic
        $result = ($result * $A) % $N ;
 
        // return smallest +ve integer
        if ($result == 1)
            return $K;
 
        // increment power
        $K++;
    }
 
    return -1 ;
}
 
// Driver Code
$A = 4; $N = 7;
echo(multiplicativeOrder($A, $N));
 
// This code is contributed by Ajit.
?>


Javascript




<script>
 
// JavaScript program to implement
// multiplicative order
 
    // function for GCD
    function GCD(a, b) {
           
        if (b == 0)
            return a;
               
        return GCD(b, a % b);
    }
       
    // Function return smallest +ve integer that
    // holds condition A^k(mod N ) = 1
    function multiplicativeOrder(A, N) {
           
        if (GCD(A, N) != 1)
            return -1;
       
        // result store power of A that raised to
        // the power N-1
        let result = 1;
       
        let K = 1;
           
        while (K < N) {
               
            // modular arithmetic
            result = (result * A) % N;
           
            // return smallest +ve integer
            if (result == 1)
                return K;
           
            // increment power
            K++;
        }
       
        return -1;
    }
 
// Driver Code
    let A = 4, N = 7;
           
    document.write(multiplicativeOrder(A, N));
  
 // This code is contributed by chinmoy1997pal.
</script>


Output : 

3

Time Complexity: O(N) 

Space Complexity: O(1)

Reference: https://en.wikipedia.org/wiki/Multiplicative_order 

 



Previous Article
Next Article

Similar Reads

Sort an array in increasing order of their Multiplicative Persistence
Given an array arr[] consisting of N positive integers, the task is to sort the array in increasing order with respect to the count of steps required to obtain a single-digit number by multiplying its digits recursively for each array element. If any two numbers have the same count of steps, then print the smaller number first. Examples: Input: arr
8 min read
Print all multiplicative primes &lt;= N
Given an integer N, the task is to print all the multiplicative primes ? N. Multiplicative Primes are the primes such that the product of their digits is also a prime. For example; 2, 3, 7, 13, 17, ... Examples: Input: N = 10 Output: 2 3 5 7Input: N = 3 Output: 2 3 Approach: Using Sieve of Eratosthenes check for all the primes ? N whether they are
8 min read
Count of multiplicative partitions of N
Given an integer N, the task is to find the total number of multiplicative partitions for N. Multiplicative Partition: Number of ways of factoring of an integer with all factors greater than 1. Examples: Input: N = 20 Output: 4 Explanation: Multiplicative partitions of 20 are: 2 × 2 × 5 = 2 × 10 = 4 × 5 = 20.Input: N = 30 Output: 5 Explanation: Mul
6 min read
Multiplicative Congruence method for generating Pseudo Random Numbers
Multiplicative Congruential Method (Lehmer Method) is a type of linear congruential generator for generating pseudorandom numbers in a specific range. This method can be defined as: [Tex]\[X_{i + 1} = aX_{i} \hspace{0.2cm} mod \hspace{0.2cm} m\] [/Tex] where, X, the sequence of pseudo-random numbersm ( &gt; 0), the modulusa (0, m), the multiplierX0
6 min read
Nearest smaller number to N having multiplicative inverse under modulo N equal to that number
Given a prime number N, the task is to find the closest smaller number than N such that modulo multiplicative inverse of a number under modulo N is equal to the number itself. Examples: Input: N = 7Output: 6Explanation:Modulo multiplicative inverse of all possible natural numbers from 1 to less than N are:Modulo multiplicative inverse of 1 under mo
3 min read
Modular multiplicative inverse from 1 to n
Give a positive integer n, find modular multiplicative inverse of all integer from 1 to n with respect to a big prime number, say, 'prime'. The modular multiplicative inverse of a is an integer 'x' such that. a x ? 1 (mod prime) Examples: Input : n = 10, prime = 17 Output : 1 9 6 13 7 3 5 15 2 12 Explanation : For 1, modular inverse is 1 as (1 * 1)
9 min read
Modular multiplicative inverse
Given two integers A and M, find the modular multiplicative inverse of A under modulo M.The modular multiplicative inverse is an integer X such that: A X ? 1 (mod M) Note: The value of X should be in the range {1, 2, ... M-1}, i.e., in the range of integer modulo M. ( Note that X cannot be 0 as A*0 mod M will never be 1). The multiplicative inverse
15+ min read
Pre Order, Post Order and In Order traversal of a Binary Tree in one traversal | (Using recursion)
Given a binary tree, the task is to print all the nodes of the binary tree in Pre-order, Post-order, and In-order in one iteration. Examples: Input: Output: Pre Order: 1 2 4 5 3 6 7 Post Order: 4 5 2 6 7 3 1 In Order: 4 2 5 1 6 3 7 Input: Output: Pre Order: 1 2 4 8 12 5 9 3 6 7 10 11 Post Order: 12 8 4 9 5 2 6 10 11 7 3 1 In Order: 8 12 4 2 9 5 1 6
9 min read
Sorting rows of matrix in ascending order followed by columns in descending order
Given a matrix, sort the rows of matrix in ascending order followed by sorting the columns in descending order. Examples : Input : a[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; Output : 7 8 9 4 5 6 1 2 3 Input : a[3][3] = {{3, 2, 1}, {9, 8, 7}, {6, 5, 4}}; Output : 7 8 9 4 5 6 1 2 3 Approach: Traverse all rows one by one and sort rows in ascending o
10 min read
Sort first k values in ascending order and remaining n-k values in descending order
Given an array of size n, arrange the first k elements of the array in ascending order and the remaining n-k elements in descending order. Examples: Input: arr[] = {5, 4, 6, 2, 1, 3, 8, 9, -1}, k = 4 Output: 2 4 5 6 9 8 3 1 -1 Input: arr[] = {5, 4, 6}, k = 2 Output: 4 5 6 Algorithm: Store the first k elements in an array and sort that in ascending
11 min read
three90RightbarBannerImg