Skip to content

Commit

Permalink
Merge pull request #213 from vaskoz/day101
Browse files Browse the repository at this point in the history
Day101
  • Loading branch information
vaskoz committed Dec 2, 2018
2 parents c0864ff + 123f468 commit c96deb1
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,4 @@ problems from
* [Day 98](https://github.com/vaskoz/dailycodingproblem-go/issues/204)
* [Day 99](https://github.com/vaskoz/dailycodingproblem-go/issues/205)
* [Day 100](https://github.com/vaskoz/dailycodingproblem-go/issues/209)
* [Day 101](https://github.com/vaskoz/dailycodingproblem-go/issues/212)
47 changes: 47 additions & 0 deletions day101/problem.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package day101

import (
"math"
)

// Sieve of prime numbers up to n.
// Runs in O(N log log N)
func Sieve(n int) []int {
data := make([]bool, n+1)
for i := 2; i < int(math.Sqrt(float64(n)))+1; i++ {
if !data[i] {
for j := i * i; j <= n; j += i {
data[j] = true
}
}
}
var results []int
for i := 2; i <= n; i++ {
if !data[i] {
results = append(results, i)
}
}
return results
}

// GoldbachsConjecture returns two prime numbers whose sum
// is equal to the given number.
// If even param is less than 4 or an odd number, nil is returned.
func GoldbachsConjecture(even int) []int {
if even < 4 || even%2 != 0 {
return nil
}
primes := Sieve(even)
set := make(map[int]struct{}, len(primes))
for _, prime := range primes {
set[prime] = struct{}{}
}
result := make([]int, 2)
for _, prime := range primes {
if _, found := set[even-prime]; found {
result[0], result[1] = prime, even-prime
break
}
}
return result
}
62 changes: 62 additions & 0 deletions day101/problem_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package day101

import (
"reflect"
"testing"
)

var testcases = []struct {
even int
expected []int
}{
{4, []int{2, 2}},
{6, []int{3, 3}},
{8, []int{3, 5}},
{10, []int{3, 7}},
{10, []int{3, 7}},
{18, []int{5, 13}},
}

func TestSieve(t *testing.T) {
t.Parallel()
expected := []int{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37,
41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97}

if result := Sieve(100); !reflect.DeepEqual(expected, result) {
t.Errorf("Expected %v got %v", expected, result)
}
}

func BenchmarkSieve(b *testing.B) {
for i := 0; i < b.N; i++ {
Sieve(100)
}
}

func TestGoldbachsConjecture(t *testing.T) {
t.Parallel()
for _, tc := range testcases {
if result := GoldbachsConjecture(tc.even); !reflect.DeepEqual(tc.expected, result) {
t.Errorf("Expected %v got %v", tc.expected, result)
}
}
}

func BenchmarkGoldbachsConjecture(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, tc := range testcases {
GoldbachsConjecture(tc.even)
}
}
}

func TestGoldbachsConjectureBadInput(t *testing.T) {
t.Parallel()
if result := GoldbachsConjecture(3); result != nil {
t.Errorf("Parameters less than 4 should result in nil return")
}
if result := GoldbachsConjecture(9); result != nil {
t.Errorf("Odd parameters should result in nil return")
}

}

0 comments on commit c96deb1

Please sign in to comment.