write an algorithm to find the difference of two numbers A and B where a is greater than b
Answers
Answer:
Explanation:
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 :
1.If ( a < b ) then there will be no answer .
2.f ( a = b ) then all the numbers greater than a are the answer so there will be infinite solutions possible.
3.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 ++ programme
// CPP program to find x such that a % x is equal
// to b.
#include <bits/stdc++.h>
using namespace std;
void modularEquation(int a, int b)
{
// if a is less than b then no solution
if (a < b) {
cout << "No solution possible " << endl;
return;
}
// if a is equal to b then every number
// greater than a will be the solution
// so its infinity
if (a == b) {
cout << "Infinite Solution possible " << endl;
return;
}
// all resultant number should be greater than
// b and (a-b) should be divisible by resultant
// number
// count variable store the number of values
// possible
int count = 0;
int n = a - b;
int y = sqrt(a - b);
for (int i = 1; i <= y; ++i) {
if (n % i == 0) {
// checking for both divisor and quotient
// whether they divide ( a-b ) completely
// and greater than b .
if (n / i > b)
count++;
if (i > b)
count++;
}
}
// Here y is added twice in the last iteration
// so 1 y should be decremented to get correct
// solution
if (y * y == n && y > b)
count--;
cout << count << endl;
}
// Driver code
int main()
{
int a = 21, b = 5;
modularEquation(a, b);
return 0;
}
Java Programme
// Java program to find x such that
// a % x is equal to b.
import java.io.*;
class GFG {
static void modularEquation(int a, int b)
{
// if a is less than b then no solution
if (a < b) {
System.out.println("No solution possible ");
return;
}
// if a is equal to b then every number
// greater than a will be the solution
// so its infinity
if (a == b) {
System.out.println("Infinite Solution possible ");
return;
}
// all resultant number should be greater
// than b and (a-b) should be divisible
// by resultant number
// count variable store the number of
// values possible
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) {
// checking for both divisor and
// quotient whether they divide
// ( a-b ) completely and
// greater than b .
if (n / i > b)
count++;
if (i > b)
count++;
}
}
// Here y is added twice in the last
// iteration so 1 y should be decremented
// to get correct solution
if (y * y == n && y > b)
count--;
System.out.println(count);
}
// Driver code
public static void main(String[] args)
{
int a = 21, b = 5;
modularEquation(a, b);
}
}
Steps :-
● Start
● Write the first number.
● Write the second number
● Find the difference
● Display the sum
● Stop