Repository: vaskoz/dailycodingproblem-go (go)

Issue:
Day 252
Good morning! Here's your coding interview problem for today.

This problem was asked by Palantir.

The ancient Egyptians used to express fractions as a sum of several terms where each numerator is one. For example, `4 / 13` can be represented as `1 / 4 + 1 / 18 + 1 / 468`.

Create an algorithm to turn an ordinary fraction a / b, where a < b, into an Egyptian fraction.

Pull request description:
Day252
Resolves #519.

Patch:
diff --git a/README.md b/README.md
index dd4989b..ade3bcf 100644
--- a/README.md
+++ b/README.md
@@ -262,3 +262,4 @@ problems from
 * [Day 248](https://github.com/vaskoz/dailycodingproblem-go/issues/512)
 * [Day 249](https://github.com/vaskoz/dailycodingproblem-go/issues/514)
 * [Day 251](https://github.com/vaskoz/dailycodingproblem-go/issues/517)
+* [Day 252](https://github.com/vaskoz/dailycodingproblem-go/issues/519)
diff --git a/day252/problem.go b/day252/problem.go
new file mode 100644
index 0000000..4db1a0e
--- /dev/null
+++ b/day252/problem.go
@@ -0,0 +1,16 @@
+package day252
+
+import "math"
+
+// EgyptianFractions convert a fraction into a list of denominators
+// for an Egyptian fraction.
+func EgyptianFractions(numerator, denominator int) []int {
+	var denominators []int
+	for numerator != 0 {
+		d := int(math.Ceil(float64(denominator) / float64(numerator)))
+		denominators = append(denominators, d)
+		numerator = d*numerator - denominator
+		denominator *= d
+	}
+	return denominators
+}


Test patch:
diff --git a/day252/problem_test.go b/day252/problem_test.go
new file mode 100644
index 0000000..114707d
--- /dev/null
+++ b/day252/problem_test.go
@@ -0,0 +1,31 @@
+package day252
+
+import (
+	"reflect"
+	"testing"
+)
+
+// nolint
+var testcases = []struct {
+	numerator, denominator int
+	denominators           []int
+}{
+	{4, 13, []int{4, 18, 468}},
+}
+
+func TestEgyptianFractions(t *testing.T) {
+	t.Parallel()
+	for _, tc := range testcases {
+		if denoms := EgyptianFractions(tc.numerator, tc.denominator); !reflect.DeepEqual(tc.denominators, denoms) {
+			t.Errorf("Expected %v, got %v", tc.denominators, denoms)
+		}
+	}
+}
+
+func BenchmarkEgyptianFractions(b *testing.B) {
+	for i := 0; i < b.N; i++ {
+		for _, tc := range testcases {
+			EgyptianFractions(tc.numerator, tc.denominator)
+		}
+	}
+}