-
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.
- Loading branch information
Showing
3 changed files
with
74 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,29 @@ | ||
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 X%i == 0 && X/i <= N { | ||
count++ | ||
} | ||
} | ||
return count | ||
} |
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,44 @@ | ||
package day74 | ||
|
||
import "testing" | ||
|
||
var testcases = []struct { | ||
N, X int | ||
expected int | ||
}{ | ||
{6, 12, 4}, | ||
} | ||
|
||
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) | ||
} | ||
} | ||
} |