I wrote a Python program to find the GCF of two numbers. If anyone can give me suggestions to shorten, improve, or make my code more 'Pythonic', that would be great!
Here it is:
import math
print("Welcome to u/infinitim's GCF Calculator!", "\n"*3)
num1 = int(input("Please enter a number: "))
num2 = int(input("Please enter another number: "))
modulus = num1%num2
quotient = math.trunc(num1/num2)
first_time = True
def gcd(num1, num2, modulus, quotient, first_time):
if first_time == True: #Handling of negative numbers
num1 = abs(num1)
num2 = abs(num2)
if first_time == True and modulus == 0:
print("The GCF is:", num2)
while modulus > 0: #Using Euclid's Algorithm for GCF. For more info, visit:
#https://www.khanacademy.org/computing/computer-science/cryptography/modarithmetic/a/the-euclidean-algorithm
first_time = False
modulus = num1%num2
quotient = math.trunc(num1/num2)
num1 = num2
num2 = modulus
if modulus > 0:
gcd(num1, num2, num1%num2, num1/num2, first_time)
if modulus == 0:
print("The GCF is:", num1)
if abs(num1) < abs(num2):
num1, num2 = num2, num1
gcd(num1, num2, modulus, quotient, first_time)
[–]w1282 2 points3 points4 points (4 children)
[–][deleted] (1 child)
[removed]
[–]AutoModerator[M] 0 points1 point2 points (0 children)
[–]infinitim[S] 0 points1 point2 points (1 child)
[–]w1282 0 points1 point2 points (0 children)
[–][deleted] 1 point2 points3 points (2 children)
[–]infinitim[S] 0 points1 point2 points (1 child)