Skip to content

Commit

Permalink
Merge pull request #158 from vaskoz/day74
Browse files Browse the repository at this point in the history
Day74
  • Loading branch information
vaskoz committed Nov 4, 2018
2 parents 0b640b5 + 2926e2a commit 3dc4b7e
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,4 @@ problems from
* [Day 71](https://github.com/vaskoz/dailycodingproblem-go/issues/151)
* [Day 72](https://github.com/vaskoz/dailycodingproblem-go/issues/153)
* [Day 73](https://github.com/vaskoz/dailycodingproblem-go/issues/155)
* [Day 74](https://github.com/vaskoz/dailycodingproblem-go/issues/157)
31 changes: 31 additions & 0 deletions day74/problem.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package day74

// MultiplicationTableBrute counts the number of X occurrences
// in a N by N multiplication table.
// Runs in O(N^2) time.
func MultiplicationTableBrute(N, X int) int {
count := 0
for i := 1; i <= N; i++ {
for j := 1; j <= N; j++ {
if i*j == X {
count++
}
}
}
return count
}

// MultiplicationTableLinear counts the number of X occurrences
// in a N by N multiplication table.
// Runs in O(N) time.
func MultiplicationTableLinear(N, X int) int {
count := 0
for i := 1; i <= N; i++ {
if i > X {
break
} else if X%i == 0 && X/i <= N {
count++
}
}
return count
}
48 changes: 48 additions & 0 deletions day74/problem_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package day74

import "testing"

var testcases = []struct {
N, X int
expected int
}{
{6, 12, 4},
{6, 36, 1},
{6, 37, 0},
{6, 29, 0},
{1000, 29, 2},
}

func TestMultiplicationTableBrute(t *testing.T) {
t.Parallel()
for _, tc := range testcases {
if result := MultiplicationTableBrute(tc.N, tc.X); result != tc.expected {
t.Errorf("Given N=%d X=%d got %d expected %d", tc.N, tc.X, result, tc.expected)
}
}
}

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

func TestMultiplicationTableLinear(t *testing.T) {
t.Parallel()
for _, tc := range testcases {
if result := MultiplicationTableLinear(tc.N, tc.X); result != tc.expected {
t.Errorf("Given N=%d X=%d got %d expected %d", tc.N, tc.X, result, tc.expected)
}
}
}

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

0 comments on commit 3dc4b7e

Please sign in to comment.