python finding the greatest common divisor of two numbers program
Explanation:
Yes, HSK is right. In the 2nd loop:
while b:
a = b
b = a % b
print(a)
First a
is changed to b
and then what you do is b = b% b
. But here:
a, b = b, a % b
it is executed as one-line so a
is still a
.
Solution:
So just add a third variable:
a = 1071
b = 462
while b:
c = a
a = b
b = c % b
print(c)
One thing that distinguishes Python from other programming languages is that it is interpreted rather than compiled. This means that it is executed line by line.
greatest common divisor (python)
def ggt2(a, b):
while b != 0:
t = b
b = a % b
a = t
return a if a != 0 else None
a = int(input("Enter a: "))
b = int(input("Enter b: "))
print("Their GCD is: "+str(ggt2(a,b)))
This code was converted to Python from the pseudocode on Wikipedia, with minor changes made.
There are some things wrong with your code. Here is your give pseudocode:
GCD :var X,Y,R: int;
input X,Y;
R:=1;
while R ≠ 0 do
R:=X mod Y; X:=Y; Y:=R;
od;
output X.
What this says is:
- Start
GCD
- create variables
X
,Y
, andR
of typeint
each - Input a value to
X
andY
- Set
R
to 1 - While
R
is not equal to0
do: - (in loop of 5) set
R
toX
%Y
(the remainder ofX
andY
) - (in loop of 5) set
X
toY
- (in loop of 5) set
Y
toR
- End of loop started on statement 5 iteration
- Print out
X
This pseudocode converted to Python would be:
def GCD():
X = int(input("Enter X: "))
Y = int(input("Enter Y: "))
R = 1
while R != 0:
R = X % Y
X = Y
Y = R
print(X)
GCD()
Try to figure out how this works, and tell me in the comments if it worked for you!
How to code for a Greatest Common Denominator function that only takes positive integers? (Python)
You can't combine multiple variables like that in a conditional expression. You need to compare each variable separately.
Also, you need to do that check before the other if
statement. Otherwise, the function will return before you check for negative numbers.
def divisor(x,y):
if x < 0 or y < 0:
raise ValueError("This function only takes positive integers!")
if (y==0):
return x
else:
return divisor(y,x%y)
When you write (x or y) < 0
, it calculates the value of x or y
, then compares that to 0
. x or y
is the first truthy value of the two variables, and any non-zero value is truthy. So if x
is not zero, then this is equivalent to x < 0
, and if x
is zero it's equivalent to y < 0
. There's no automatic distribution of <
over or
.
Turn pseudocode into functioning code to find greatest common divisor
To find if a value evenly divides another, you need to have the smaller (divisor) on the right side of the modulus operator. So change your code to:
if (m %d != 0) or (n % d != 0):
This evaluates if d
evenly divides m
or n
, instead of if m
or n
evenly divide d
.
python: Greatest common divisor (gcd) for floats, preferably in numpy
Seems like you could just modify the code of fractions.gcd
to include the tolerances:
def float_gcd(a, b, rtol = 1e-05, atol = 1e-08):
t = min(abs(a), abs(b))
while abs(b) > rtol * t + atol:
a, b = b, a % b
return a
Related Topics
Link Several Popen Commands with Pipes
Removing Elements from a List Containing Specific Characters
Calculate Time Difference Between Pandas Dataframe Indices
How to Write String Literals in Python Without Having to Escape Them
Prevent Plot from Showing in Jupyter Notebook
Passing Csrftoken with Python Requests
How to Rotate Xticklabels in Matplotlib So That the Spacing Between Each Xticklabel Is Equal
Image Segmentation Based on Edge Pixel Map
Difference Between the Built-In Pow() and Math.Pow() for Floats, in Python
Reading/Writing Ms Word Files in Python
How to Strip the Whitespace from Pandas Dataframe Headers
Matplotlib Yaxis Range Display Using Absolute Values Rather Than Offset Values