usingMaths.com
From Theory to Practice - Math You Can Use.







<< PreviousNext >>

Finding HCF and GCD in Python - Kids Fun Exercise



What is HCF and GCD? | Maths Explanation for Python Kids

Highest Common Factor or Greatest Common Divisor is the highest-valued-factor or greatest-valued-divisor common to a set of numbers.

In this Python math project, we'll explore how to find the Highest Common Factor (HCF) and Greatest Common Divisor (GCD) of numbers. This tutorial is perfect for primary school students learning to code with Python.

A common method for finding H.C.F. - G.C.D. is repeated factorization using only common factors.

If we have the set of numbers 30, 48 and 54 for example, their H.C.F. or G.C.D. is found thus:

Steps for calculating HCF using factorization method

Hence, H.C.F. of 30, 48 and 54 = 2 X 3 = 6

How to find HCF of multiple numbers in Python | Step-by-step Guide.

We shall follow the steps below in our Python algorithm for finding HCF.

Step 1:

Do a numerical sort on the set so its first member is the smallest in the set.

Step 2:

Starting with 2, iteratively check through the set of numbers for a common factor.

Step 3:

For each common factor, divide every member of the number set by the common factor.

Step 4:

Repeat from step 2 recursively until there are no more common factors.


Create a new Python module file; File, New File.
Call it FindHCF.py

Type out the adjoining Python code for finding Highest Common Factor (Greatest Common Divisor).(H.C.F.)


Note: You can comment out the Python code for the main class from the previous lesson if you have been following.


So! Python Fun Practice Exercise - Find HCF

As a fun practice exercise, feel free to try out your own numbers, and see how the Python code finds the HCF of those numbers.









<< PreviousNext >>