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 :: pyspark group by and average in dataframes 
Python :: write json pythonb 
Python :: numpy find columns containing nan 
Python :: selenium firefox webdriver 
Python :: get number of zero is a row pandas 
Python :: Python program to print all odd numbers in a range 
Python :: python initialize empty dictionary 
Python :: delete dataframe from memory python 
Python :: drop list of columns pandas 
Python :: change dictionary value python 
Python :: python url shortener 
Python :: python relative file path doesnt work 
Python :: Find Specific value in Column 
Python :: python gui drag and drop 
Python :: how to give autocomplete in python 
Python :: python list 
Python :: newsapi in python 
Python :: how to get random number python 
Python :: last index in python 
Python :: python run exe 
Python :: round decimal to 2 places python 
Python :: how yo import python lib 
Python :: pandas date range 
Python :: django check user admin 
Python :: python insert sorted list 
Python :: python replace nth occurrence in string 
Python :: set column datatype pandas 
Python :: how to set variable to null in python 
Python :: django render template 
Python :: numpy random in python 
ADD CONTENT
Topic
Content
Source link
Name
9+6 =