Given a and b which represent the location of two factors of N whose product is equal to the number N when the factors are arranged in ascending order. The task is to find the total number of factors of N.
Examples:
Input : a = 2, b = 3
Output : 4
N = 6
factors are {1, 2, 3, 6}
No of factors are 4
Input : a = 13, b = 36
Output : 48
Approach: The solution is based on observation.
Assume N = 50. Then N has 6 factors 1, 2, 5, 10, 25 and 50. On multiplying 1 and 50 will always give the value 50( N ). Also, by multiplying 2 and 25 gives us the value N, similarly by multiplying 5 and 10 we get the value N. So, here we can see that by multiplying two factors when arranged in an increasing order gives the value N. The multiplication must be done in a manner: 1st factor and the last factor on multiplication gives N, 2nd factor and 2nd last factor on multiplication gives N and so on.
With this pattern, we can find a way to calculate the number of factors. Say, the 1st and 4th factor on multiplication gives N. This means that there are 4 factors ( 1st, 2nd, 3rd, and 4th ). If the product of 2nd and 3rd factor gives N then we can say there must be a factor in the 1st position and on the 4th position.
Therefore, the number of factors will be equal to a + b – 1.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findFactors( int a, int b)
{
int c;
c = a + b - 1;
cout << c;
}
int main()
{
int a, b;
a = 13;
b = 36;
findFactors(a, b);
return 0;
}
|
Java
class GFG
{
static void findFactors( int a, int b)
{
int c;
c = a + b - 1 ;
System.out.print(c);
}
public static void main(String[] args)
{
int a, b;
a = 13 ;
b = 36 ;
findFactors(a, b);
}
}
|
Python3
def findFactors(a, b):
c = a + b - 1
print (c)
if __name__ = = '__main__' :
a = 13
b = 36
findFactors(a, b)
|
C#
using System;
class GFG
{
static void findFactors( int a, int b)
{
int c;
c = a + b - 1;
Console.Write(c);
}
public static void Main(String[] args)
{
int a, b;
a = 13;
b = 36;
findFactors(a, b);
}
}
|
Javascript
<script>
function findFactors(a, b)
{
let c;
c = a + b - 1;
document.write(c);
}
let a, b;
a = 13;
b = 36;
findFactors(a, b);
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!