Given two numbers a and b find all x such that a % x = b
Given two numbers a and b find all x such that a % x = b.
Examples:
Input : a = 21, b = 5
Output : 2
The answers of the Modular Equation are
8 and 16 since 21 % 8 = 21 % 16 = 5.
Here 3 cases arises :
- If ( a < b ) then there will be no answer .
- If ( a = b ) then all the numbers greater than a are the answer so there will be infinite solutions possible.
- If ( a > b ) Suppose x is an answer to our equation. Then x divides (a – b). Also since a % x = b then b < x. These conditions are necessary and sufficient as well. So the answer is number of divisors of a – b which are strictly greater than b which can be solved in O(sqrt( a-b )). Here only one case arises which we have to deal separately when (a-b) is perfect square then we will add its square root two times so we have to subtract one times, if this case arises.
C++
#include <bits/stdc++.h>
using namespace std;
void modularEquation( int a, int b)
{
if (a < b) {
cout << "No solution possible " << endl;
return ;
}
if (a == b) {
cout << "Infinite Solution possible " << endl;
return ;
}
int count = 0;
int n = a - b;
int y = sqrt (a - b);
for ( int i = 1; i <= y; ++i) {
if (n % i == 0) {
if (n / i > b)
count++;
if (i > b)
count++;
}
}
if (y * y == n && y > b)
count--;
cout << count << endl;
}
int main()
{
int a = 21, b = 5;
modularEquation(a, b);
return 0;
}
|
Java
import java.io.*;
class GFG {
static void modularEquation( int a, int b)
{
if (a < b) {
System.out.println( "No solution possible " );
return ;
}
if (a == b) {
System.out.println( "Infinite Solution possible " );
return ;
}
int count = 0 ;
int n = a - b;
int y = ( int )Math.sqrt(a - b);
for ( int i = 1 ; i <= y; ++i) {
if (n % i == 0 ) {
if (n / i > b)
count++;
if (i > b)
count++;
}
}
if (y * y == n && y > b)
count--;
System.out.println(count);
}
public static void main(String[] args)
{
int a = 21 , b = 5 ;
modularEquation(a, b);
}
}
|
Python3
import math
def modularEquation(a, b) :
if (a < b) :
print ( "No solution possible " )
return
if (a = = b) :
print ( "Infinite Solution possible " )
return
count = 0
n = a - b
y = ( int )(math.sqrt(a - b))
for i in range ( 1 , y + 1 ) :
if (n % i = = 0 ) :
if (n / i > b) :
count = count + 1
if (i > b) :
count = count + 1
if (y * y = = n and y > b) :
count = count - 1
print (count)
a = 21
b = 5
modularEquation(a, b)
|
C#
using System;
class GFG {
static void modularEquation( int a, int b)
{
if (a < b) {
Console.WriteLine( "No solution possible " );
return ;
}
if (a == b) {
Console.WriteLine( "Infinite Solution possible " );
return ;
}
int count = 0;
int n = a - b;
int y = ( int )Math.Sqrt(a - b);
for ( int i = 1; i <= y; ++i) {
if (n % i == 0) {
if (n / i > b)
count++;
if (i > b)
count++;
}
}
if (y * y == n && y > b)
count--;
Console.WriteLine(count);
}
public static void Main()
{
int a = 21, b = 5;
modularEquation(a, b);
}
}
|
Javascript
<script>
function modularEquation(a, b)
{
if (a < b)
{
document.write( "No solution possible " );
return ;
}
if (a == b)
{
document.write( "Infinite Solution possible " );
return ;
}
let count = 0;
let n = a - b;
let y = Math.sqrt(a - b);
for (let i = 1; i <= y; ++i)
{
if (n % i == 0)
{
if (n / i > b)
count++;
if (i > b)
count++;
}
}
if (y * y == n && y > b)
count--;
document.write(count);
}
let a = 21;
let b = 5;
modularEquation(a, b);
</script>
|
PHP
<?php
function modularEquation( $a , $b )
{
if ( $a < $b )
{
echo "No solution possible " ;
return ;
}
if ( $a == $b )
{
echo "Infinite Solution possible " ;
return ;
}
$count = 0;
$n = $a - $b ;
$y = sqrt( $a - $b );
for ( $i = 1; $i <= $y ; ++ $i )
{
if ( $n % $i == 0) {
if ( $n / $i > $b )
$count ++;
if ( $i > $b )
$count ++;
}
}
if ( $y * $y == $n && $y > $b )
$count --;
echo $count ;
}
$a = 21;
$b = 5;
modularEquation( $a , $b );
?>
|
Output:
2
Time Complexity: O(?(a-b))
Auxiliary Space: O(1)
Approach:
Steps for the approach:
- First, defined a function countModularEquation.
- which takes two integers a and b as input.
- returns the count of values of x that satisfy the equation a % x = b.
- Finally, the count of solutions is printed on the console.
Below is code implementation for the above approach:
C++
#include <iostream>
using namespace std;
int countModularEquation( int a, int b)
{
int count = 0;
for ( int x = 1; x <= a; x++) {
if (a % x == b) {
count++;
}
}
return count;
}
int main()
{
int a = 21, b = 5;
int count = countModularEquation(a, b);
cout << count << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
static int countModularEquation( int a, int b)
{
int count = 0 ;
for ( int x = 1 ; x <= a; x++) {
if (a % x == b) {
count++;
}
}
return count;
}
public static void main(String[] args)
{
int a = 21 , b = 5 ;
int count = countModularEquation(a, b);
System.out.println(count);
}
}
|
Python3
def countModularEquation(a, b):
count = 0
for x in range ( 1 , a + 1 ):
if a % x = = b:
count + = 1
return count
a = 21
b = 5
count = countModularEquation(a, b)
print (count)
|
C#
using System;
class GFG
{
static int CountModularEquation( int a, int b)
{
int count = 0;
for ( int x = 1; x <= a; x++)
{
if (a % x == b)
{
count++;
}
}
return count;
}
public static void Main( string [] args)
{
int a = 21, b = 5;
int count = CountModularEquation(a, b);
Console.WriteLine(count);
}
}
|
Javascript
function countModularEquation(a, b) {
let count = 0;
for (let x = 1; x <= a; x++) {
if (a % x === b) {
count++;
}
}
return count;
}
function main() {
const a = 21;
const b = 5;
const count = countModularEquation(a, b);
console.log(count);
}
main();
|
Output:
2
Time Complexity: O(a), where a is the value of the input parameter
Auxiliary Space: O(1)
Last Updated :
09 Nov, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...