- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 Articles
- Program to count number of fraction pairs whose sum is 1 in python
- Find Four points such that they form a square whose sides are parallel to x and y axes in Python
- Program to find number of pairs from N natural numbers whose sum values are divisible by k in Python
- Find number of pairs (x, y) in an array such that x^y > y^x in C++
- Program to count maximum number of distinct pairs whose differences are larger than target in Python
- Program to find number of good pairs in Python
- Program to find two pairs of numbers where difference between sum of these pairs are minimized in python
- Find Nth positive number whose digital root is X in C++
- What are the two pairs of coprime and the two pairs of Twin prime numbers?
- Find Four points such that they form a square whose sides are parallel to x and y axes in C++
- Program to find max number of K-sum pairs in Python
- Program to find number of quadruples for which product of first and last pairs are same in Python
- What are the differences in between python 2.x and python 3.x versions?
- Program to find number of subsequences with i, j and k number of x, y, z letters in Python
- What are the key differences between Python 2.7.x and Python 3.x?

Advertisements