Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

how to simplify fraction in python

def gcd(a,b):
	if a == 0:
		return b
	if b == 0:
		return a
	if a == b:
		return a
	if a > b:
		return gcd(a-b, b)
	return gcd(a, b-a)

def simplifyFraction(a, b):
  divisor = gcd(a, b)
  return (a / divisor, b / divisor)
Comment

python simplify fraction

#From scratch

#Euclid's algorithm https://en.wikipedia.org/wiki/Greatest_common_divisor#Euclid's_algorithm
def gcd(a: int, b: int):
    fraction = (a, b)
    while fraction[0] != fraction[1]:
        maximum = max(fraction)
        minimum = max(fraction)
        fraction = (maximum - minimum, minimum)
    return fraction[0]

def simplify(a: int, b: int):
  divisor = gcd(a, b)
  return (a / divisor, b / divisor)
Comment

PREVIOUS NEXT
Code Example
Python :: how to change button background color while clicked tkinter python 
Python :: python get list of files in path 
Python :: update python 3.10 ubuntu 
Python :: argument sequence in python function 
Python :: numpy normal distribution 
Python :: how to clear the console python 
Python :: pandas not is na 
Python :: python WhatsApp messaging spammer 
Python :: how to concat csv files python 
Python :: how to get a list of followers on instagram python 
Python :: iterate over rows dataframe 
Python :: Import "decouple" could not be resolved Pylance 
Python :: how to add time with time delta in python 
Python :: stop a function from continuing when a condition is met python 
Python :: how to make a url shortener in python 
Python :: adjust tick label size matplotlib 
Python :: dataframe show to semicolon python 
Python :: python xor two bytes 
Python :: function as parameter tpye hinting python 
Python :: heatmap(df_train.corr()) 
Python :: value count a list python 
Python :: scikit learn ridge classifier 
Python :: jupyter no output cell 
Python :: regex email python 
Python :: code hand tracking 
Python :: $ sudo pip install pdml2flow-frame-inter-arrival-time 
Python :: how to run pytest and enter console on failure 
Python :: concat tensors pytorch 
Python :: Import "django.core.urlresolvers" could not be resolved 
Python :: numpy count the number of 1s in array 
ADD CONTENT
Topic
Content
Source link
Name
6+5 =