# Count ordered pairs with product less than N

Given an integer N. The task is to count the number of ordered pairs (a, b) such that .

**Examples:**

Input:N = 5Output:8 Ordered Pairs are = (1, 1), (1, 2), (1, 3), (1, 4), (2, 1), (2, 2), (3, 1), (4, 1).Input:N = 1000Output:7053

**Naive Approach:** Run two for loops upto N – 1 and count ordered pairs whose product are less than N.

**Efficient Approach:** Let’s considered an ordered pair **(a, b)**. Then, if the product of two numbers is less than n i:e **a * b < n**, then at least one of them must be less then square root of n. We can proof by contradiction that if both numbers are greater then square root of n the their product is not less than n.

So, you can take all integers a up to** sqrt(n – 1)** instead of all integers up to n. For each a, count the number of **b >= a** such that **a * b < n**. Then multiply the result by two to count the pair (b, a) for each pair (a, b) you saw. After that, subtract the integer part of **sqrt(n – 1)** to ensure the pairs **(a, a) **were counted exactly once.

Below is the implementation of the above approach:

## C++

`// C++ implementation of above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return count of Ordered pairs ` `// whose product are less than N ` `int` `countOrderedPairs(` `int` `N) ` `{ ` ` ` `// Initialize count to 0 ` ` ` `int` `count_pairs = 0; ` ` ` ` ` `// count total pairs ` ` ` `for` `(` `int` `i = 1; i <= ` `sqrt` `(N - 1); ++i) { ` ` ` `for` `(` `int` `j = i; j * i < N; ++j) ` ` ` `++count_pairs; ` ` ` `} ` ` ` ` ` `// multiply by 2 to get ordered_pairs ` ` ` `count_pairs *= 2; ` ` ` ` ` `// subtract redundant pairs (a, b) where a==b. ` ` ` `count_pairs -= ` `int` `(` `sqrt` `(N - 1)); ` ` ` ` ` `// return answer ` ` ` `return` `count_pairs; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `N = 5; ` ` ` ` ` `// function call to print required answer ` ` ` `cout << countOrderedPairs(N); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of above approach ` ` ` `class` `GFG{ ` `// Function to return count of Ordered pairs ` `// whose product are less than N ` `static` `int` `countOrderedPairs(` `int` `N) ` `{ ` ` ` `// Initialize count to 0 ` ` ` `int` `count_pairs = ` `0` `; ` ` ` ` ` `// count total pairs ` ` ` `for` `(` `int` `i = ` `1` `; i <= (` `int` `)Math.sqrt(N - ` `1` `); ++i) { ` ` ` `for` `(` `int` `j = i; j * i < N; ++j) ` ` ` `++count_pairs; ` ` ` `} ` ` ` ` ` `// multiply by 2 to get ordered_pairs ` ` ` `count_pairs *= ` `2` `; ` ` ` ` ` `// subtract redundant pairs (a, b) where a==b. ` ` ` `count_pairs -= (` `int` `)(Math.sqrt(N - ` `1` `)); ` ` ` ` ` `// return answer ` ` ` `return` `count_pairs; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `N = ` `5` `; ` ` ` ` ` `// function call to print required answer ` ` ` `System.out.println(countOrderedPairs(N)); ` `} ` `} ` `// This code is contributed by mits ` |

*chevron_right*

*filter_none*

## Python3

`# Pyhton 3 implementation of above approach ` ` ` `from` `math ` `import` `sqrt ` `# Function to return count of Ordered pairs ` `# whose product are less than N ` `def` `countOrderedPairs(N): ` ` ` `# Initialize count to 0 ` ` ` `count_pairs ` `=` `0` ` ` ` ` `# count total pairs ` ` ` `p ` `=` `int` `(sqrt(N` `-` `1` `)) ` `+` `1` ` ` `q ` `=` `int` `(sqrt(N))` `+` `2` ` ` `for` `i ` `in` `range` `(` `1` `,p,` `1` `): ` ` ` `for` `j ` `in` `range` `(i,q,` `1` `): ` ` ` `count_pairs ` `+` `=` `1` ` ` ` ` `# multiply by 2 to get ordered_pairs ` ` ` `count_pairs ` `*` `=` `2` ` ` ` ` `# subtract redundant pairs (a, b) where a==b. ` ` ` `count_pairs ` `-` `=` `int` `(sqrt(N ` `-` `1` `)) ` ` ` ` ` `# return answer ` ` ` `return` `count_pairs ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `N ` `=` `5` ` ` ` ` `# function call to print required answer ` ` ` `print` `(countOrderedPairs(N)) ` ` ` `# This code is contributed by ` `# Surendra_Gangwar ` |

*chevron_right*

*filter_none*

## C#

`//C# implementation of above approach ` ` ` `using` `System; ` ` ` `public` `class` `GFG{ ` ` ` `// Function to return count of Ordered pairs ` `// whose product are less than N ` `static` `int` `countOrderedPairs(` `int` `N) ` `{ ` ` ` `// Initialize count to 0 ` ` ` `int` `count_pairs = 0; ` ` ` ` ` `// count total pairs ` ` ` `for` `(` `int` `i = 1; i <= (` `int` `)Math.Sqrt(N - 1); ++i) { ` ` ` `for` `(` `int` `j = i; j * i < N; ++j) ` ` ` `++count_pairs; ` ` ` `} ` ` ` ` ` `// multiply by 2 to get ordered_pairs ` ` ` `count_pairs *= 2; ` ` ` ` ` `// subtract redundant pairs (a, b) where a==b. ` ` ` `count_pairs -= (` `int` `)(Math.Sqrt(N - 1)); ` ` ` ` ` `// return answer ` ` ` `return` `count_pairs; ` `} ` ` ` `// Driver code ` ` ` `static` `public` `void` `Main (){ ` ` ` ` ` `int` `N = 5; ` ` ` `// function call to print required answer ` ` ` `Console.WriteLine(countOrderedPairs(N)); ` `} ` `} ` `// This code is contributed by Sachin. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of above approach ` ` ` `// Function to return count of Ordered ` `// pairs whose products are less than N ` `function` `countOrderedPairs(` `$N` `) ` `{ ` ` ` `// Initialize count to 0 ` ` ` `$count_pairs` `= 0; ` ` ` ` ` `// count total pairs ` ` ` `for` `(` `$i` `= 1; ` `$i` `<= sqrt(` `$N` `- 1); ++` `$i` `) ` ` ` `{ ` ` ` `for` `( ` `$j` `= ` `$i` `; ` `$j` `* ` `$i` `< ` `$N` `; ++` `$j` `) ` ` ` `++` `$count_pairs` `; ` ` ` `} ` ` ` ` ` `// multiply by 2 to get ordered_pairs ` ` ` `$count_pairs` `*= 2; ` ` ` ` ` `// subtract redundant pairs ` ` ` `// (a, b) where a==b. ` ` ` `$count_pairs` `-= (sqrt(` `$N` `- 1)); ` ` ` ` ` `// return answer ` ` ` `return` `$count_pairs` `; ` `} ` ` ` `// Driver code ` `$N` `= 5; ` ` ` `// function call to print ` `// required answer ` `echo` `countOrderedPairs(` `$N` `); ` ` ` `// This code is contributed ` `// by Sach_Code ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

8

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

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

## Recommended Posts:

- Count number of ordered pairs with Even and Odd Product
- Count number of ordered pairs with Even and Odd Sums
- Count ordered pairs of positive numbers such that their sum is S and XOR is K
- Count pairs of numbers from 1 to N with Product divisible by their Sum
- Count all distinct pairs with product equal to K
- Count unordered pairs (i,j) such that product of a[i] and a[j] is power of two
- Count of pairs upto N such whose LCM is not equal to their product for Q queries
- Count of pairs in an array whose product is a perfect square
- Count pairs in Array whose product is a Kth power of any positive integer
- Count of pairs in a given range with sum of their product and sum equal to their concatenated number
- Count of pairs in an array such that the highest power of 2 that divides their product is 1
- Count of index pairs in array whose range product is a positive integer
- Find the number of ordered pairs such that a * p + b * q = N, where p and q are primes
- Find the Kth pair in ordered list of all possible sorted pairs of the Array
- Count of ordered triplets (R, G, B) in a given original string
- Product of all the pairs from the given array
- Number of Co-prime pairs from 1 to N with product equals to N
- Number of pairs in an array having sum equal to product
- Maximize product of digit sum of consecutive pairs in a subsequence of length K
- Find pairs of elements from two different arrays whose product is a perfect square

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.