Egyptian fraction solution in golang

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

package main
import "fmt"
/*
  Go program for
  Egyptian fraction solution
*/

func egyptianFraction(a, b int) {
	if a == 0 || b == 0 {
		return
	}
	if b >= a {
		// Calculate remainder
		var remainder int = b % a
		// Calculate divisor
		var divisor int = b / a
		if remainder != 0 {
			divisor++
			fmt.Print("1/", divisor, " + ")
			a = a * divisor - b
			b = b * divisor
			egyptianFraction(a, b)
		} else {
			// When remainder is zero
			fmt.Print("1/", divisor)
		}
	} else {
		// When b < a
		fmt.Print((a / b), " + ")
		egyptianFraction(a % b, b)
	}
}
func main() {
	// Two numbers
	var a int = 7
	var b int = 53
	egyptianFraction(a, b)
}

Output

1/8 + 1/142 + 1/30104


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







© 2021, kalkicode.com, All rights reserved