Skip to main content

Egyptian fraction solution in python

Python program for Egyptian fraction solution. Here problem description and other solutions.

#  Python 3 program for
#  Egyptian fraction solution
class Fraction :
	def egyptianFraction(self, a, b) :
		if (a == 0 or b == 0) :
			return
		
		if (b >= a) :
			#  Calculate remainder
			remainder = b % a
			#  Calculate divisor
			divisor = int(b / a)
			if (remainder != 0) :
				divisor += 1
				print("1/", divisor , end = " + ", sep = "")
				a = a * divisor - b
				b = b * divisor
				self.egyptianFraction(a, b)
			else :
				#  When remainder is zero
				print("1/", divisor, end = "", sep = "")
			
		else :
			#  When b < a
			print((int(a / b)), end = " + ", sep = "")
			self.egyptianFraction(a % b, b)
		
	

def main() :
	task = Fraction()
	#  Two numbers
	a = 7
	b = 53
	task.egyptianFraction(a, b)

if __name__ == "__main__": main()

Output

1/8 + 1/142 + 1/30104




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment