Skip to content

Commit

Permalink
Merge 9cd982c into c735903
Browse files Browse the repository at this point in the history
  • Loading branch information
vaskoz committed Jun 2, 2019
2 parents c735903 + 9cd982c commit 47576dd
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -289,3 +289,4 @@ problems from
* [Day 280](https://github.com/vaskoz/dailycodingproblem-go/issues/568)
* [Day 281](https://github.com/vaskoz/dailycodingproblem-go/issues/572)
* [Day 282](https://github.com/vaskoz/dailycodingproblem-go/issues/575)
* [Day 283](https://github.com/vaskoz/dailycodingproblem-go/issues/577)
68 changes: 68 additions & 0 deletions day283/problem.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package day283

// RegularNumbersFaster returns n regular numbers
// using brute force.
func RegularNumbersFaster(n int) []int {
if n < 0 {
panic("negative values are not possible")
} else if n == 0 {
return []int{}
}
result := make([]int, n)
result[0] = 1
var i2, i3, i5 int
next2, next3, next5 := 2, 3, 5
for i := 1; i < n; i++ {
result[i] = min(next2, next3, next5)
if result[i] == next2 {
i2++
next2 = result[i2] * 2
}
if result[i] == next3 {
i3++
next3 = result[i3] * 3
}
if result[i] == next5 {
i5++
next5 = result[i5] * 5
}
}
return result
}

func min(vals ...int) int {
smallest := vals[0]
for _, v := range vals {
if v < smallest {
smallest = v
}
}
return smallest
}

// RegularNumbersBrute returns n regular numbers
// using brute force.
func RegularNumbersBrute(n int) []int {
if n < 0 {
panic("negative values are not possible")
}
result := make([]int, 0, n)
count := 0
for x := 1; count < n; x++ {
remain := repeatDivide(x, 2)
remain = repeatDivide(remain, 3)
remain = repeatDivide(remain, 5)
if remain == 1 {
result = append(result, x)
count++
}
}
return result
}

func repeatDivide(val, factor int) int {
for val%factor == 0 {
val /= factor
}
return val
}
70 changes: 70 additions & 0 deletions day283/problem_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package day283

import (
"reflect"
"testing"
)

// nolint
var testcases = []struct {
n int
expected []int
}{
{0, []int{}},
{1, []int{1}},
{2, []int{1, 2}},
{11, []int{1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15}},
{26, []int{1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, 16, 18, 20, 24, 25, 27, 30, 32, 36, 40, 45, 48, 50, 54, 60}},
}

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

func TestRegularNumbersFasterBadInput(t *testing.T) {
defer func() {
if err := recover(); err == nil {
t.Errorf("Expected a panic for a negative input")
}
}()
RegularNumbersFaster(-2)
}

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

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

func TestRegularNumbersBruteBadInput(t *testing.T) {
defer func() {
if err := recover(); err == nil {
t.Errorf("Expected a panic for a negative input")
}
}()
RegularNumbersBrute(-2)
}

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

0 comments on commit 47576dd

Please sign in to comment.