Any Ideas On This Simple Co-Prime program

Joined
Dec 12, 2022
Messages
1
Reaction score
0
So the program that I want to make it is about printing all the pair co-prime numbers in a given range, if type 10 it has to print out every co-prime number like {1,2} , {2,3} and so on until it reaches the the typed number. So I have to make it with a range that i give and it has to print out every possible pair of co-prime numbers in it. Maybe it has to have an outside of the main function and then it should be called in the main. I don't know can you guy help me.

Any ideas ?
 
Joined
Dec 28, 2022
Messages
4
Reaction score
1
Copy code
def print_coprime_pairs(n):
for i in range(1, n+1):
for j in range(i+1, n+1):
if is_coprime(i, j):
print(f"({i}, {j})")

def is_coprime(a, b):
if a == 1 or b == 1:
return True

r = a % b
while r != 0:
a = b
b = r
r = a % b

return b == 1

To use this function, you can simply call print_coprime_pairs(n), where n is the upper bound of the range you want to consider. For example, calling print_coprime_pairs(10) would print out all pairs of co-prime numbers between 1 and 10, inclusive.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,060
Latest member
BuyKetozenseACV

Latest Threads

Top