-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #213 from vaskoz/day101
Day101
- Loading branch information
Showing
3 changed files
with
110 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,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 | ||
} |
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,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") | ||
} | ||
|
||
} |