Hey all, in this amazing tutorial, we are going to check if two numbers are Co-Prime or not in Python. So get ready with your code editors as we are going to do this program in the best way possible!
In a hurry? Just read the chronology of the program given at the end of this article!
As always, you can find the final code at the end of this article. But I would advice you to stick with us this whole time as we are going to go through each and every step you need to follow to become the best coder in the world!
Excited? Let's start with some basics.
What are Co-Prime numbers?
Two numbers are called Co-Prime if the only number that evenly divides both of them is 1.
In simple language, when we find that the prime factorization of both the numbers don't contain any common integer, we say that these numbers are Co-Prime in nature.
For example: The numbers 25 and 21:
Prime Factorization of 25: 5 x 5
Prime Factorization of 21: 3 x 7
We find that there is no common integer in the factorizations of 25 and 21. Hence, 25 and 21 are Co-Prime numbers.
IMP: Another definition of Co-Prime numbers is that 'The numbers whose HCF is 1 are called Co-Prime numbers'.
This is the same thing we discussed in the previous example. HCF or the Highest Common Factor of two numbers tells the highest integer that is common in the factorization of the two numbers.
I hope that you are now clear with the basic definition of Co-Prime numbers. Now, let's start with the Overview!
Overview:
To write the code for this program, we will use the basic knowledge of finding HCF/GCD of two numbers.
This is because, finding the HCF of two numbers gives us exactly what we want. If the HCF of those two numbers comes out to be 1, then they are Co-Prime, otherwise they are not!
I have already wrote an article on finding HCF of two numbers. You can check that here:
WAP (Python) to find the HCF/GCD of two numbers
Here's the summary of that article:
It is a known fact that the HCF of two numbers cannot be more than the smaller number, hence we would write a loop that would divide both the main numbers with every number starting from 1 till the smallest number. The last number to divide both the main numbers simultaneously will be our HCF!
Understood? Most probably not! Don't worry, I will cover the steps later in the article 🌝.
*As I have already told you some of the basics of the program, you can try to write it on your own. Check if the solution matches!*
Understanding what we need to do:
Step 1: Taking the input from the user:
Step 2: Finding the HCF of the two numbers:
Step3: Check if the two numbers are Co-Prime or not
- User enters the input i.e., num1 and num2
- Smallest number among them is found using the min() function.
- Value of the smallest number is stored in the mn variable.
- A for loop is run to divide both the numbers by all the numbers from 1 to mn.
- The range of the loop is (1 , mn+1). Variable taken as iterator is i.
- If statement is used to test if any number i divides both num1 and num2 simultaneously.
- If one does, then the value of variable hcf is assigned as i.
- This process is done for all the numbers in the range.
- When loop terminates, the final value of HCF is found.
- If the value of HCF is 1, then the numbers are Co-Prime.
- If not, then the numbers are not Co-Prime.
No comments:
Post a Comment