Expressing a fraction as a natural number under modulo 'm' - GeeksforGeeks

13 Sep, 2021
Given two integers A and B where A is not divisible by B, the task is to express A / B as a natural number modulo m where m = 1000000007. Note: This representation is useful where we need to express Probability of an event, Area of Curves and polygons etc.Examples:  
Input: A = 2, B = 6 Output: 333333336Input: A = 4, B = 5 Output: 600000005  
 
Approach: We know that, A / B can be written as A * (1 / B) i.e. A * (B ^ -1).It is known that the modulo(%) operator satisfies the relation:  
(a * b) % m = ( (a % m) * (b % m) ) % m
So, we can write:  
(b ^ -1) % m = (b ^ m-2) % m (Fermat's little theorem)
Therefore the result will be:  
( (A mod m) * ( power(B, m-2) % m) ) % m
Below is the implementation of the above approach:  
C++
Java
Python3
C#
PHP
Javascript
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std;#define ll long long int#define m 1000000007 // Function to return the GCD of given numbersint gcd(int a, int b){    if (a == 0)        return b;    return gcd(b % a, a);} // Recursive function to return (x ^ n) % mll modexp(ll x, ll n){    if (n == 0) {        return 1;    }    else if (n % 2 == 0) {        return modexp((x * x) % m, n / 2);    }    else {        return (x * modexp((x * x) % m, (n - 1) / 2) % m);    }} // Function to return the fraction modulo modll getFractionModulo(ll a, ll b){    ll c = gcd(a, b);     a = a / c;    b = b / c;     // (b ^ m-2) % m    ll d = modexp(b, m - 2);     // Final answer    ll ans = ((a % m) * (d % m)) % m;     return ans;} // Driver codeint main(){    ll a = 2, b = 6;     cout << getFractionModulo(a, b) << endl;     return 0;}
// Java implementation of the approach import java.io.*; class GFG {       static long m  = 1000000007; // Function to return the GCD of given numbers static long gcd(long a, long b){    if (a == 0)        return b;    return gcd(b % a, a);} // Recursive function to return (x ^ n) % mstatic long modexp(long x, long n){    if (n == 0) {        return 1;    }    else if (n % 2 == 0) {        return modexp((x * x) % m, n / 2);    }    else {        return (x * modexp((x * x) % m, (n - 1) / 2) % m);    }} // Function to return the fraction modulo mod static long getFractionModulo(long a, long b){    long c = gcd(a, b);     a = a / c;    b = b / c;     // (b ^ m-2) % m    long  d = modexp(b, m - 2);     // Final answer    long ans = ((a % m) * (d % m)) % m;     return ans;} // Driver code     public static void main (String[] args) {        long a = 2, b = 6;     System.out.println(getFractionModulo(a, b));    }}// This code is contributed by inder_verma
# Python3 implementation of the approachm = 1000000007 # Function to return the GCD# of given numbersdef gcd(a, b):     if (a == 0):        return b    return gcd(b % a, a) # Recursive function to return (x ^ n) % mdef modexp(x, n):     if (n == 0) :        return 1         elif (n % 2 == 0) :        return modexp((x * x) % m, n // 2)         else :        return (x * modexp((x * x) % m,                           (n - 1) / 2) % m)  # Function to return the fraction modulo moddef getFractionModulo(a, b):     c = gcd(a, b)     a = a // c    b = b // c     # (b ^ m-2) % m    d = modexp(b, m - 2)     # Final answer    ans = ((a % m) * (d % m)) % m     return ans # Driver codeif __name__ == "__main__":     a = 2    b = 6     print ( getFractionModulo(a, b)) # This code is contributed by ita_c
//C#  implementation of the approach using System; public class GFG{      static long m = 1000000007; // Function to return the GCD of given numbersstatic long gcd(long a, long b){    if (a == 0)        return b;    return gcd(b % a, a);} // Recursive function to return (x ^ n) % mstatic long modexp(long x, long n){    if (n == 0) {        return 1;    }    else if (n % 2 == 0) {        return modexp((x * x) % m, n / 2);    }    else {        return (x * modexp((x * x) % m, (n - 1) / 2) % m);    }} // Function to return the fraction modulo modstatic long getFractionModulo(long a, long b){    long c = gcd(a, b);     a = a / c;    b = b / c;     // (b ^ m-2) % m    long d = modexp(b, m - 2);     // Final answer    long ans = ((a % m) * (d % m)) % m;     return ans;} // Driver code         static public void Main (){                 long a = 2, b = 6;        Console.WriteLine(getFractionModulo(a, b));    }}
<?php// PHP implementation of the approach // Function to return the GCD of// given numbersfunction abc($a, $b){    if ($a == 0)        return $b;    return abc($b % $a, $a);} // Recursive function to return (x ^ n) % mfunction modexp($x, $n){    $m = 1000000007;    if ($n == 0)    {        return 1;    }    else if ($n % 2 == 0)    {        return modexp(($x * $x) % $m, $n / 2);    }    else    {        return ($x * modexp(($x * $x) % $m,                        ($n - 1) / 2) % $m);    }} // Function to return the fraction// modulo modfunction getFractionModulo($a, $b){    $m = 1000000007;    $c = abc($a, $b);         $a = $a / $c;    $b = $b / $c;     // (b ^ m-2) % m    $d = modexp($b, $m - 2);     // Final answer    $ans = (($a % $m) * ($d % $m)) % $m;     return $ans;} // Driver code$a = 2;$b = 6; echo(getFractionModulo($a, $b)) ; // This code is contributed// by Shivi_Aggarwal?>
<script>// javascript implementation of the approach   var m = 100000007;     // Function to return the GCD of given numbers    function gcd(a , b) {        if (a == 0)            return b;        return gcd(b % a, a);    }     // Recursive function to return (x ^ n) % m    function modexp(x , n) {        if (n == 0) {            return 1;        } else if (n % 2 == 0) {            return modexp((x * x) % m, n / 2);        } else {            return (x * modexp((x * x) % m, (n - 1) / 2) % m);        }    }     // Function to return the fraction modulo mod    function getFractionModulo(a , b) {        var c = gcd(a, b);         a = a / c;        b = b / c;         // (b ^ m-2) % m        var d = modexp(b, m - 2);         // Final answer        var ans = ((a % m) * (d % m)) % m;         return ans;    }     // Driver code              var a = 2, b = 6;         document.write(getFractionModulo(a, b)); // This code is contributed by umadevi9616</script>
333333336
 
Time Complexity: O(log(min(a, b) + logm) Auxiliary Space: O(log(min(a, b) + logm) 
inderDuMCA
Sach_Code
Shivi_Aggarwal
ukasp
pankajsharmagfg
umadevi9616
C Basics
inversion
Modular Arithmetic
Algorithms
C++ Programs
Competitive Programming
Mathematical
Mathematical
Modular Arithmetic
Algorithms
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
DSA Sheet by Love Babbar
How to Start Learning DSA?
Difference between Algorithm, Pseudocode and Program
K means Clustering - Introduction
Types of Complexity Classes | P, NP, CoNP, NP hard and NP complete
Header files in C/C++ and its uses
Program to print ASCII Value of a character
How to return multiple values from a function in C or C++?
C++ Program for QuickSort
Sorting a Map by value in C++ STL