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 Fractions

import fractions

print(fractions.Fraction(1.5))

print(fractions.Fraction(5))

print(fractions.Fraction(1,3))
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 :: a int and float python 
Python :: circular linked list in python 
Python :: python 4 release date 
Python :: Convert .tif images files to .jpeg in python 
Python :: swap case python 
Python :: dataframe coulmn to list 
Python :: how to append data in excel using python pandas 
Python :: how to make a do while in python 
Python :: what does manage.py do 
Python :: python sort dictionary case insensitive 
Python :: fluffy ancake recipe 
Python :: print statements 
Python :: how to get runtime of a function in python 
Python :: use decorator in class python 
Python :: print something python 
Python :: to text pandas 
Python :: and logic python 
Python :: combine for and if python 
Python :: python is instance numpy arrya 
Python :: ValueError: tuple.index(x): x not in tuple 
Python :: how to create file organizer using python 
Python :: function TBone() if 2=2 then print("Sup") end 
Python :: how to set pywal permenent 
Python :: convert code c++ to python online 
Python :: rapids - convert nerworkx to cugraph 
Shell :: add-apt-repository command not found 
Shell :: uninstall k3s 
Shell :: nginx restart 
Shell :: choco list installed 
Shell :: mysqlclient install ubuntu 
ADD CONTENT
Topic
Content
Source link
Name
2+2 =