- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Program to find number of pairs between x, whose multiplication is x and they are coprime in Python

Suppose there is a function f(x), that counts number of (p, q) pairs, such that

- 1 < p <= q <= x
- p and q are coprime
- p * q = x So if we have n.

We have to find sum f(x[i]) for all i in range 1 to n.

So, if the input is like 12, then the output will be 3, because x values are ranging from 1 to 12.

- When x = 6, the valid pair is (2, 3) so f(6) = 1
- When x = 10, the valid pair is (2, 5) so f(10) = 1
- When x = 12, the valid pair is (3, 4) so f(12) = 1

so there are total 3 pairs.

To solve this, we will follow these steps −

- count := 0
- sqr := integer part of (square root of n) + 1
- for base in range 2 to sqr - 1, do
- for i in range 1 to minimum of base and floor of (n / base - base + 1), do
- if gcd of base and i) is not same as 1, then
- go for next iteration

- count := count + floor of (n - i * base)/(base * base)

- if gcd of base and i) is not same as 1, then

- for i in range 1 to minimum of base and floor of (n / base - base + 1), do
- return count

## Example

Let us see the following implementation to get better understanding −

from math import sqrt, gcd def solve(n): count = 0 sqr = int(sqrt(n)) + 1 for base in range(2, sqr): for i in range(1, min(base, n // base - base + 1)): if gcd(base, i) != 1: continue count += (n - i * base) // (base * base) return count n = 12 print(solve(n))

## Input

12

## Output

3

- Related Questions & Answers
- Find Four points such that they form a square whose sides are parallel to x and y axes in Python
- Find number of pairs (x, y) in an array such that x^y > y^x in C++
- Find Nth positive number whose digital root is X in C++
- Differences between Python 2.x and Python 3.x?
- What are the key differences between Python 2.7.x and Python 3.x?
- Find Four points such that they form a square whose sides are parallel to x and y axes in C++
- What are the differences in between python 2.x and python 3.x versions?
- Difference between x++ and x = x+1 in Java
- Program to count number of fraction pairs whose sum is 1 in python
- Find the first natural number whose factorial is divisible by x in C++
- Difference between x++ and x= x+1 in Java programming
- Python Program to Generate a Dictionary that Contains Numbers (between 1 and n) in the Form (x,x*x).
- Find a Number X whose sum with its digits is equal to N in C++
- Count pairs in a sorted array whose sum is less than x in C++
- Find larger of x^y and y^x in C++

Advertisements