Check if two numbers are Co-Prime or not - Python - BoiCoder

BoiCoder

Coding Made Easy for All

Breaking

Sunday, 14 February 2021

Check if two numbers are Co-Prime or not - Python


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:


Here I will tell you step-by-step what you need to do.

Step 1: Taking the input from the user:


This is the easiest part of the program. We have to just take the input from the user. Also, don't forget to typecast the input into int as we have to perform calculations on them.

We have to ask the user to input the first number (num1) and the second number (num2).

Here's the code for this step:

Step 2: Finding the HCF of the two numbers:


As said in the Overview, the HCF of two numbers can't be greater than the smallest of the two numbers. Hence, we need to find the smallest number among the two.

This can be done using the min() function. I have stored this value in the variable 'mn'. 

Then, we need to divide both the numbers continuously by all the numbers in the range 1 to mn. This can be done using the for loop with the range (1, mn+1). 

'mn+1', as range function doesn't take in account the upper limit. We have taken the variable to be iterated as i.

To check if any number, i, divides both the numbers simultaneously, we have used an if statement. If i divides both of them, then the value of i will be stored in another variable called 'hcf'.

And this value will be constantly updated when i matches the conditions again in the for loop.

When the loop will terminate, we will get our final value of HCF.

Here's the code for this step:

Step3: Check if the two numbers are Co-Prime or not


For the two numbers to be Co-Prime, the HCF of the two numbers should be equal to 1. 

Hence, for this last step, we could use a simple if-else statement. If the HCF is 1, then the numbers are Co-Prime otherwise they aren't.

Here's the code for this step:

So now, we have found the best way to check if two numbers are Co-Prime or not.

Here's the chronology of the program:

  1. User enters the input i.e., num1 and num2
  2. Smallest number among them is found using the min() function.
  3. Value of the smallest number is stored in the mn variable.
  4. A for loop is run to divide both the numbers by all the numbers from 1 to mn.
  5. The range of the loop is (1 , mn+1). Variable taken as iterator is i.
  6. If statement is used to test if any number i divides both num1 and num2 simultaneously.
  7. If one does, then the value of variable hcf is assigned as i.
  8. This process is done for all the numbers in the range.
  9. When loop terminates, the final value of HCF is found.
  10. If the value of HCF is 1, then the numbers are Co-Prime.
  11. If not, then the numbers are not Co-Prime.

I hope that you have now understood the whole program. If not, then please try playing with the lines of code and try to interpret the importance of each line of code. This will certainly help. Otherwise, the comments section is always open for your queries!

Here's the final code:

Here's the output of the code:

Check if two numbers are Co-Prime or not - Python

Check if two numbers are Co-Prime or not - Python

This is the end of our amazing journey where we found the best way to check if two numbers are Co-Prime or not in Python.

If you liked the tutorial, don't forget to share it to others as it may help someone struggling in coding.

Also, don't forget to subscribe to our newsletter as we do these amazing coding sessions each week.

Again, thanks for reading!!!

No comments:

Post a Comment