# Number of distinct integers obtained by lcm(X, N)/X

Given a number , find the number of distinct integers obtained by * lcm(X, N)/X* where X can be any positive number.

**Examples**:

Input: N = 2Output: 2 if X is 1, then lcm(1, 2)/1 is 2/1=2. if X is 2, then lcm(2, 2)/2 is 2/2=1. For any X greater than 2 we cannot obtain a distinct integer.Input: N = 3Output: 2

It is known that **lcm(x, y) = x*y/gcd(x, y)**.

Therefore,

lcm(X, N) = X*N/gcd(X, N) or,lcm(X, N)/X= N/gcd(X, N)

So only the distinct factors of can be the distinct integers possible. Hence count the number of distinct factors of N including 1 and N itself, which is the required answer.

Below is the implementation of the above approach:

## C++

`// C++ program to find distinct integers ` `// ontained by lcm(x, num)/x ` `#include <cmath> ` `#include <iostream> ` ` ` `using` `namespace` `std; ` ` ` `// Function to count the number of distinct ` `// integers ontained by lcm(x, num)/x ` `int` `numberOfDistinct(` `int` `n) ` `{ ` ` ` `int` `ans = 0; ` ` ` ` ` `// iterate to count the number of factors ` ` ` `for` `(` `int` `i = 1; i <= ` `sqrt` `(n); i++) { ` ` ` `if` `(n % i == 0) { ` ` ` `ans++; ` ` ` `if` `((n / i) != i) ` ` ` `ans++; ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `ans; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `n = 3; ` ` ` ` ` `cout << numberOfDistinct(n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find distinct integers ` `// ontained by lcm(x, num)/x ` ` ` `import` `java.io.*; ` ` ` `class` `GFG { ` ` ` `// Function to count the number of distinct ` `// integers ontained by lcm(x, num)/x ` `static` `int` `numberOfDistinct(` `int` `n) ` `{ ` ` ` `int` `ans = ` `0` `; ` ` ` ` ` `// iterate to count the number of factors ` ` ` `for` `(` `int` `i = ` `1` `; i <= Math.sqrt(n); i++) { ` ` ` `if` `(n % i == ` `0` `) { ` ` ` `ans++; ` ` ` `if` `((n / i) != i) ` ` ` `ans++; ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `ans; ` `} ` ` ` `// Driver Code ` ` ` `public` `static` `void` `main (String[] args) { ` ` ` `int` `n = ` `3` `; ` ` ` ` ` `System.out.println (numberOfDistinct(n)); ` ` ` ` ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python 3

`# Python 3 program to find distinct integers ` `# ontained by lcm(x, num)/x ` ` ` `import` `math ` ` ` `# Function to count the number of distinct ` `# integers ontained by lcm(x, num)/x ` `def` `numberOfDistinct(n): ` ` ` `ans ` `=` `0` ` ` ` ` `# iterate to count the number of factors ` ` ` `for` `i ` `in` `range` `( ` `1` `, ` `int` `(math.sqrt(n))` `+` `1` `): ` ` ` `if` `(n ` `%` `i ` `=` `=` `0` `) : ` ` ` `ans ` `+` `=` `1` ` ` `if` `((n ` `/` `/` `i) !` `=` `i): ` ` ` `ans ` `+` `=` `1` ` ` `return` `ans ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` `n ` `=` `3` ` ` ` ` `print` `(numberOfDistinct(n)) ` ` ` `# This code is contributed by ` `# ChitraNayal ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find distinct integers ` `// ontained by lcm(x, num)/x ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to count the number ` `// of distinct integers ontained ` `// by lcm(x, num)/x ` `static` `int` `numberOfDistinct(` `int` `n) ` `{ ` ` ` `int` `ans = 0; ` ` ` ` ` `// iterate to count the number ` ` ` `// of factors ` ` ` `for` `(` `int` `i = 1; i <= Math.Sqrt(n); i++) ` ` ` `{ ` ` ` `if` `(n % i == 0) ` ` ` `{ ` ` ` `ans++; ` ` ` `if` `((n / i) != i) ` ` ` `ans++; ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `ans; ` `} ` ` ` `// Driver Code ` `static` `public` `void` `Main () ` `{ ` ` ` `int` `n = 3; ` ` ` `Console.WriteLine(numberOfDistinct(n)); ` `} ` `} ` ` ` `// This code is contributed by ajit ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find distinct ` `// integers ontained by lcm(x, num)/x ` ` ` `// Function to count the number ` `// of distinct integers ontained ` `// by lcm(x, num)/x ` `function` `numberOfDistinct(` `$n` `) ` `{ ` ` ` `$ans` `= 0; ` ` ` ` ` `// iterate to count the ` ` ` `// number of factors ` ` ` `for` `(` `$i` `= 1; ` `$i` `<= sqrt(` `$n` `); ` `$i` `++) ` ` ` `{ ` ` ` `if` `(` `$n` `% ` `$i` `== 0) ` ` ` `{ ` ` ` `$ans` `++; ` ` ` `if` `((` `$n` `/ ` `$i` `) != ` `$i` `) ` ` ` `$ans` `++; ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `$ans` `; ` `} ` ` ` `// Driver Code ` `$n` `= 3; ` `echo` `numberOfDistinct(` `$n` `); ` ` ` `// This code is contributed by ajit ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

2

**Time Complexity**: O(sqrt(n))

## Recommended Posts:

- Total distinct pairs from two arrays such that second number can be obtained by inverting bits of first
- Check whether a number can be represented as sum of K distinct positive integers
- Check if the sum of distinct digits of two integers are equal
- Integers from the range that are composed of a single distinct digit
- Generate permutation of 1 to N such that absolute difference of consecutive numbers give K distinct integers
- Number of Co-prime pairs obtained from the sum of digits of elements in the given range
- Find the number of integers x in range (1,N) for which x and x+1 have same number of divisors
- Print a number strictly less than a given number such that all its digits are distinct.
- Number of solutions for x < y, where a <= x <= b and c <= y <= d and x, y are integers
- Number of distinct subsets of a set
- Number of N digit integers with weight W
- Number expressed as sum of five consecutive integers
- Find the number of integers from 1 to n which contains digits 0's and 1's only
- Check whether a number has exactly three distinct factors or not
- Number of substrings divisible by 4 in a string of integers

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.