Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
GCF Function Help
#1
Alright, here's the rundown. This year, I took an introduction to computer science course that taught Python, so keep in mind I'm practically a beginner. I enjoy coding a lot and recently we have been working with factoring in algebra. I decided it would be cool if I made a code that would find the greatest common factor (the greatest number that factors into all the numbers given). First, I made one that found the GCF of 2 or 3 numbers, since I didn't know how to do any amount of numbers. Here's the code for that:
def gcf2 (num1, num2):
    num1ar = []
    num2ar = []
    comfac = []
    for x in range (1, 10000):
        if (num1 % x == 0):
            num1ar.append(x)
    for y in range (1, 10000):
        if (num2 % y == 0):
            num2ar.append(y)
    for a in range(len(num1ar)):
        for b in range(len(num2ar)):
            if (num1ar[a] == num2ar[b]):
                comfac.append(num1ar[a])
    return (comfac[len(comfac) - 1])
def gcf3 (num1, num2, num3):
    num1ar = []
    num2ar = []
    num3ar = []
    comfac = []
    for x in range (1, 10000):
        if (num1 % x == 0):
            num1ar.append(x)
    for y in range (1, 10000):
        if (num2 % y == 0):
            num2ar.append(y)
    for z in range (1, 10000):
        if (num3 % z == 0):
            num3ar.append(z)
    for a in range(len(num1ar)):
        for b in range(len(num2ar)):
            if (num1ar[a] == num2ar[b]):
                for c in range(len(num3ar)):
                    if (num2ar[b] == num3ar[c]):
                        comfac.append(num3ar[c])
    return (comfac[len(comfac) - 1])
amount = int(input("Are you doing 2 or 3 numbers?"))
if (amount == 2):
    num1 = int(input("Enter a non-negative whole number:"))
    num2 = int(input("Enter a non-negative whole number:"))
    print ("Number 1: " + str(num1) + "\tNumber 2: " + str(num2) + "\nGCF: " + str(gcf2(num1, num2)))
if (amount == 3):
    num1 = int(input("Enter a non-negative whole number:"))
    num2 = int(input("Enter a non-negative whole number:"))
    num3 = int(input("Enter a non-negative whole number:"))
    print ("Number 1: " + str(num1) + "\tNumber 2: " + str(num2) + "\tNumber 3: " + str(num3) + "\nGCF: " + str(gcf3(num1, num2, num3)))
Then, when I got to this part in my course, I realized 2D arrays are most likely the way to do any amount of numbers. But then I ran into another problem. I'm not really sure how to exactly explain my problem, but basically I can't compare all of the numbers at once, and I don't know how many numbers they will enter so I can't just make a million for loops. Here's my code:
def factor (array, count, num):
    array.append([])
    for x in range(1, 10001):
        if (num % x == 0):
            array[count].append(x)
        else:
            next
def GCF (array, common):
    for x in range(len(array)):
        for a in range(len(array[x])):
            for b in range(len(array[0])):
                if (array[x][a] == array[0][b]):
                    common.append(array[0][b])
count = 0
array = []
common = []
amount = int(input("How many numbers are you finding the greatest common factor of?"))
for x in range(amount):
    num = int(input("Enter a number:"))
    factor (array, count, num)
    count = count + 1
    print ("Number " + str(count) + ": " + str(num))
GCF(array, common)
print ("Greatest Common Factor: " + str(common[len(common) - 1]))
For example, if you enter (3 first) 68, 70, and 92, then it returns 4, while it should return 2. I'm not sure if there is a simple solution I don't know about, or if the solution takes 100 lines of code. Can anybody help?

I maybe should've mentioned that I coded this in some version of python 2, on a website called codeskupltor. http://www.codeskulptor.org
Reply
#2
The following should help you simplify your code.

import math

def gcd(x, y):
    while y != 0:
        (x, y) = (y, x % y)
    return x
        
print(gcd(48,72))        
print(math.gcd(48,72))   # Python 3.5+     
Please let us know if you need additional help.

Lewis
To paraphrase: 'Throw out your dead' code. https://www.youtube.com/watch?v=grbSQ6O6kbs Forward to 1:00
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020