How to Find HCF or GCD using Python?

In this article, we will show you how to find the HCF (Highest Common Factor) or GCD (Greatest Common Factor) in Python. Below are the various methods to accomplish this task:

  • Using For Loop

  • Using Euclidean Algorithm

  • Using While Loop

  • Using Recursion(Naive method)

  • Handling Negative Numbers in HCF

What is H.C.F. or G.C.D?

The largest positive integer that perfectly divides the two given numbers is known as the highest common factor (H.C.F.) or greatest common divisor (G.C.D.).

Example - The HCF of 12 and 14, is 2.

Using For Loop

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task -

  • Create a variable to store the input number 1.

  • Create another variable to store the input number 2.

  • Use the if conditional statement to check whether the first number is greater than the second.

  • Considering the second number as the smaller number if the condition is true.

  • Else considering the first number as a smaller number.

  • Use for loop to traverse from 1 to small number using the range() function (The range() function returns a sequence of numbers that starts at 0 and increments by 1 (default) and stops before a given number).

  • Use the if conditional statement to check whether both the first and second numbers divide exactly by index (i) with the modulo operator % (returns the remainder), and operator(returns true if both the conditions are true)

  • Assign the corresponding index(i) value as HCF if the condition is true

  • Print the resultant HCF of input number 1 & input number 2.

Example

The following program returns the HCF of input number 1 & input number 2 using for loop -

<div class="execute"></div><div class="code-mirror  language-python" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token comment"># input number 1</span>
inputNumber_1 <span class="token operator">=</span> <span class="token number">30</span>
<span class="token comment"># input number 2</span>
inputNumber_2 <span class="token operator">=</span> <span class="token number">14</span>
<span class="token comment"># checking whether the first number is greater than</span>
<span class="token comment"># the second one</span>
<span class="token keyword">if</span> inputNumber_1 <span class="token operator">></span> inputNumber_2<span class="token punctuation">:</span>
   <span class="token comment"># assigning the second number as a smaller number</span>
   small_num <span class="token operator">=</span> inputNumber_2
<span class="token keyword">else</span><span class="token punctuation">:</span>
   <span class="token comment"># else assigning the first number as a smaller number</span>
   small_num <span class="token operator">=</span> inputNumber_1
<span class="token comment"># traversing from 1 to the small number range</span>
<span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> small_num<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
   <span class="token comment"># checking whether both the first and second numbers divide exactly by index (i)</span>
   <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token punctuation">(</span>inputNumber_1 <span class="token operator">%</span> i <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token keyword">and</span> <span class="token punctuation">(</span>inputNumber_2 <span class="token operator">%</span> i <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
      <span class="token comment"># assigning the index(i) value as HCF if the condition is true</span>
      resultHcf <span class="token operator">=</span> i
<span class="token comment"># printing the HCF of input number 1 & input number 2</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"The H.C.F of"</span><span class="token punctuation">,</span> inputNumber_1<span class="token punctuation">,</span> <span class="token string">"and"</span><span class="token punctuation">,</span> inputNumber_2<span class="token punctuation">,</span> <span class="token string">"="</span><span class="token punctuation">,</span> resultHcf<span class="token punctuation">)</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>

Output

On executing, the above program will generate the following output ?

The H.C.F of 30 and 14 = 2

Using Euclidean Algorithm

Example

The following program returns the HCF of input number 1 & input number 2 using Euclidean Algorithm -

<div class="execute"></div><div class="code-mirror  language-python" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token comment"># creating a function that returns the HCF of two numbers passed to it</span>
<span class="token keyword">def</span> <span class="token function">calculateHcf</span><span class="token punctuation">(</span>p<span class="token punctuation">,</span> q<span class="token punctuation">)</span><span class="token punctuation">:</span>
   <span class="token comment"># traversing the loop until q times</span>
   <span class="token keyword">while</span><span class="token punctuation">(</span>q<span class="token punctuation">)</span><span class="token punctuation">:</span>
      <span class="token comment"># assigning q value to p, p % q value to q</span>
      p<span class="token punctuation">,</span> q <span class="token operator">=</span> q<span class="token punctuation">,</span> p <span class="token operator">%</span> q
   <span class="token comment"># returning p value(i.e, inputNumber_2)</span>
   <span class="token keyword">return</span> p
<span class="token comment"># input number 1</span>
inputNumber_1 <span class="token operator">=</span> <span class="token number">15</span>
<span class="token comment"># input number 2</span>
inputNumber_2 <span class="token operator">=</span> <span class="token number">4</span>
<span class="token comment"># calling the calculateHcf() function by passing both the input numbers to it</span>
resultHcf <span class="token operator">=</span> calculateHcf<span class="token punctuation">(</span>inputNumber_1<span class="token punctuation">,</span> inputNumber_2<span class="token punctuation">)</span>
<span class="token comment"># printing the HCF of input number 1 & input number 2</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"The H.C.F of"</span><span class="token punctuation">,</span> inputNumber_1<span class="token punctuation">,</span> <span class="token string">"and"</span><span class="token punctuation">,</span> inputNumber_2<span class="token punctuation">,</span> <span class="token string">"="</span><span class="token punctuation">,</span>
calculateHcf<span class="token punctuation">(</span>inputNumber_1<span class="token punctuation">,</span> inputNumber_2<span class="token punctuation">)</span><span class="token punctuation">)</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>

Output

On executing, the above program will generate the following output ?

The H.C.F of 15 and 4 = 1

Using While Loop (Repeated Subtraction)

Example

The following program returns the HCF of input number 1 & input number 2 using a while loop (Repeated Subtraction approach) -

<div class="execute"></div><div class="code-mirror  language-python" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token comment"># input number 1</span>
inputNumber_1 <span class="token operator">=</span> <span class="token number">40</span>
<span class="token comment"># input number 2</span>
inputNumber_2 <span class="token operator">=</span> <span class="token number">5</span>
<span class="token comment"># storing both the numbers in temporary variables</span>
<span class="token comment"># for not getting disturbed</span>
temp_1 <span class="token operator">=</span> inputNumber_1
temp_2 <span class="token operator">=</span> inputNumber_2
<span class="token comment"># traversing the loop until both the numbers are not equal</span>
<span class="token keyword">while</span> inputNumber_1 <span class="token operator">!=</span> inputNumber_2<span class="token punctuation">:</span>
   <span class="token comment"># checking whether inputNumber_1 is greater than inputNumber_2</span>
   <span class="token keyword">if</span> inputNumber_1 <span class="token operator">></span> inputNumber_2<span class="token punctuation">:</span>
      <span class="token comment"># assigning inputNumber_1-inputNumber_2 value to inputNumber_1</span>
      inputNumber_1 <span class="token operator">-=</span> inputNumber_2
   <span class="token keyword">else</span><span class="token punctuation">:</span>
      <span class="token comment"># else assigning inputNumber_2-inputNumber_1 value to inputNumber_2</span>
      inputNumber_2 <span class="token operator">-=</span> inputNumber_1
<span class="token comment"># printing the HCF of input number 1 & input number 2</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"The H.C.F of"</span><span class="token punctuation">,</span> temp_1<span class="token punctuation">,</span> <span class="token string">"and"</span><span class="token punctuation">,</span> temp_2<span class="token punctuation">,</span> <span class="token string">"="</span><span class="token punctuation">,</span> inputNumber_1<span class="token punctuation">)</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>

Output

On executing, the above program will generate the following output ?

The H.C.F of 40 and 5 = 5

Using Recursion(Naive method)

Example

The following program returns the HCF of input number 1 & input number 2 using recursion -

<div class="execute"></div><div class="code-mirror  language-python" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token comment"># creating a function that returns the HCF of two numbers passed to it</span>
<span class="token keyword">def</span> <span class="token function">calculateHcf</span><span class="token punctuation">(</span>num_1<span class="token punctuation">,</span> num_2<span class="token punctuation">)</span><span class="token punctuation">:</span>
   <span class="token comment"># checking whether the second number is equal to 0</span>
      <span class="token keyword">if</span><span class="token punctuation">(</span>num_2 <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
         <span class="token comment"># returning the absolute value of the first number if the condition is true</span>
         <span class="token keyword">return</span> <span class="token builtin">abs</span><span class="token punctuation">(</span>num_1<span class="token punctuation">)</span>
      <span class="token keyword">else</span><span class="token punctuation">:</span>
         <span class="token comment"># else returning the resultant HCF by calling the calculateHcf() function</span>
         <span class="token comment"># by passing num_2, value of num_1 % num_2 as arguments</span>
         <span class="token keyword">return</span> calculateHcf<span class="token punctuation">(</span>num_2<span class="token punctuation">,</span> num_1 <span class="token operator">%</span> num_2<span class="token punctuation">)</span>
<span class="token comment"># input number 1</span>
inputNumber_1 <span class="token operator">=</span> <span class="token number">32</span>
<span class="token comment"># input number 2</span>
inputNumber_2 <span class="token operator">=</span> <span class="token number">6</span>
<span class="token comment"># calling the calculateHcf() function by passing both the input numbers to it</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"The H.C.F of"</span><span class="token punctuation">,</span> inputNumber_1<span class="token punctuation">,</span> <span class="token string">"and"</span><span class="token punctuation">,</span> inputNumber_2<span class="token punctuation">,</span> <span class="token string">"="</span><span class="token punctuation">,</span>
calculateHcf<span class="token punctuation">(</span>inputNumber_1<span class="token punctuation">,</span> inputNumber_2<span class="token punctuation">)</span><span class="token punctuation">)</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>

Output

On executing, the above program will generate the following output ?

The H.C.F of 32 and 6 = 2

Handling Negative Numbers in HCF

The HCF of two numbers can never be negative, so if any of the numbers are negative, simply multiply them by -1 to make them positive.

In this method, the complexity of repeated subtraction is improved by the efficient use of the modulo operator(%) in the euclidean algorithm.

The following program returns the HCF of input number 1 & input number 2 using while by handling the negative numbers in HCF -

Example

<div class="execute"></div><div class="code-mirror  language-python" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token comment"># creating a function to calculate the hcf</span>
<span class="token keyword">def</span> <span class="token function">calculateHcf</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span> y<span class="token punctuation">)</span><span class="token punctuation">:</span>
   <span class="token keyword">return</span> y <span class="token operator">==</span> <span class="token number">0</span> <span class="token keyword">and</span> x <span class="token keyword">or</span> calculateHcf<span class="token punctuation">(</span>y<span class="token punctuation">,</span> x <span class="token operator">%</span> y<span class="token punctuation">)</span>
<span class="token comment"># input number 1</span>
inputNumber_1 <span class="token operator">=</span> <span class="token operator">-</span><span class="token number">4</span>
<span class="token comment"># input number 2</span>
inputNumber_2 <span class="token operator">=</span> <span class="token number">15</span>
<span class="token comment"># If the user types a negative number, we simply change it to a positive number.</span>
<span class="token comment"># HCF is the highest positive number that divides both numbers</span>
<span class="token comment"># -4 & 15 --> HCF = 1 (as the highest number that divides both)</span>
<span class="token comment"># -4 & -15 --> HCF = 1 (as the highest number that divides both)</span>
inputNumber_1 <span class="token operator">>=</span> <span class="token number">0</span> <span class="token keyword">and</span> inputNumber_1 <span class="token keyword">or</span> <span class="token operator">-</span>inputNumber_1
inputNumber_2 <span class="token operator">>=</span> <span class="token number">0</span> <span class="token keyword">and</span> inputNumber_2 <span class="token keyword">or</span> <span class="token operator">-</span>inputNumber_2
<span class="token comment"># calling the calculateHcf() function by passing both the input numbers to it</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"The H.C.F of"</span><span class="token punctuation">,</span> inputNumber_1<span class="token punctuation">,</span> <span class="token string">"and"</span><span class="token punctuation">,</span> inputNumber_2<span class="token punctuation">,</span> <span class="token string">"="</span><span class="token punctuation">,</span>
calculateHcf<span class="token punctuation">(</span>inputNumber_1<span class="token punctuation">,</span> inputNumber_2<span class="token punctuation">)</span><span class="token punctuation">)</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>

Output

On executing, the above program will generate the following output ?

The H.C.F of -4 and 15 = 1

Conclusion

In this article, we learned how to find the gcd/hcf of a number in Python using four different methods. We also learned how to deal with negative numbers when computing HCF.

Updated on: 2022-10-28T11:31:48+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements