-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add egyptian fractions example.
- Loading branch information
Showing
11 changed files
with
168 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// Package egyptianfractions calculates the "Egyptian Fractions" version of a given fraction | ||
package egyptianfractions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package egyptianfractions | ||
|
||
func Extend(a, b Fraction) (Fraction, Fraction) { | ||
denominator := a.Denominator * b.Denominator | ||
|
||
extendedA := Fraction{ | ||
Numerator: a.Numerator * b.Denominator, | ||
Denominator: denominator, | ||
} | ||
extendedB := Fraction{ | ||
Numerator: b.Numerator * a.Denominator, | ||
Denominator: denominator, | ||
} | ||
|
||
return extendedA, extendedB | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package egyptianfractions_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/thenativeweb/codingcircle/egyptianfractions" | ||
) | ||
|
||
func TestExtend(t *testing.T) { | ||
fractionA := egyptianfractions.Fraction{1, 3} | ||
fractionB := egyptianfractions.Fraction{4, 7} | ||
|
||
extendedFractionA, extendedFractionB := | ||
egyptianfractions.Extend(fractionA, fractionB) | ||
|
||
assert.Equal(t, 7, extendedFractionA.Numerator) | ||
assert.Equal(t, 21, extendedFractionA.Denominator) | ||
|
||
assert.Equal(t, 12, extendedFractionB.Numerator) | ||
assert.Equal(t, 21, extendedFractionB.Denominator) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package egyptianfractions | ||
|
||
import "math" | ||
|
||
func FindEgyptianFractions(fraction Fraction) []Fraction { | ||
fractions := []Fraction{} | ||
|
||
for fraction.Numerator > 0 { | ||
n := int(math.Ceil( | ||
float64(fraction.Denominator) / | ||
float64(fraction.Numerator))) | ||
|
||
partialFraction := Fraction{1, n} | ||
|
||
fractions = append(fractions, partialFraction) | ||
|
||
extendedFraction, extendedPartialFraction := | ||
Extend(fraction, partialFraction) | ||
|
||
fraction = Reduce(Fraction{ | ||
Numerator: extendedFraction.Numerator - extendedPartialFraction.Numerator, | ||
Denominator: extendedFraction.Denominator, | ||
}) | ||
} | ||
|
||
return fractions | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package egyptianfractions_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/thenativeweb/codingcircle/egyptianfractions" | ||
) | ||
|
||
func TestFindEgyptianFractions(t *testing.T) { | ||
t.Run("returns 1/2 and 1/6 for 2/3", func(t *testing.T) { | ||
fraction := egyptianfractions.Fraction{2, 3} | ||
|
||
result := egyptianfractions.FindEgyptianFractions(fraction) | ||
|
||
assert.Equal( | ||
t, | ||
[]egyptianfractions.Fraction{{1, 2}, {1, 6}}, | ||
result, | ||
) | ||
}) | ||
|
||
t.Run("returns 1/3, 1/11 and 1/231 for 3/7", func(t *testing.T) { | ||
fraction := egyptianfractions.Fraction{3, 7} | ||
|
||
result := egyptianfractions.FindEgyptianFractions(fraction) | ||
|
||
assert.Equal( | ||
t, | ||
[]egyptianfractions.Fraction{{1, 3}, {1, 11}, {1, 231}}, | ||
result, | ||
) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package egyptianfractions | ||
|
||
type Fraction struct { | ||
Numerator int | ||
Denominator int | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package egyptianfractions | ||
|
||
func GCD(a, b int) int { | ||
for b != 0 { | ||
remainder := a % b | ||
a = b | ||
b = remainder | ||
} | ||
return a | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package egyptianfractions_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/thenativeweb/codingcircle/egyptianfractions" | ||
) | ||
|
||
func TestGCD(t *testing.T) { | ||
t.Run("returns the greatest common divisor if one exists", func(t *testing.T) { | ||
result := egyptianfractions.GCD(8, 12) | ||
assert.Equal(t, 4, result) | ||
}) | ||
|
||
t.Run("returns 1 if no greatest common divisor exists", func(t *testing.T) { | ||
result := egyptianfractions.GCD(2, 3) | ||
assert.Equal(t, 1, result) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package egyptianfractions | ||
|
||
func Reduce(fraction Fraction) Fraction { | ||
divisor := GCD(fraction.Numerator, fraction.Denominator) | ||
|
||
return Fraction{ | ||
Numerator: fraction.Numerator / divisor, | ||
Denominator: fraction.Denominator / divisor, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package egyptianfractions_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/thenativeweb/codingcircle/egyptianfractions" | ||
) | ||
|
||
func TestReduce(t *testing.T) { | ||
fraction := egyptianfractions.Fraction{ | ||
Numerator: 4, | ||
Denominator: 8, | ||
} | ||
|
||
reducedFraction := egyptianfractions.Reduce(fraction) | ||
|
||
assert.Equal(t, 1, reducedFraction.Numerator) | ||
assert.Equal(t, 2, reducedFraction.Denominator) | ||
} |