Last active
October 1, 2023 07:59
-
-
Save PatrickVienne/d6c6c1bd47a0967711a4164c15a484ac to your computer and use it in GitHub Desktop.
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
package towerofseven | |
import ( | |
"testing" | |
) | |
// greatest power of seven within the range of int32 | |
const sevenPowEleven = 1977326743 | |
// returns whether an integer number 'n' == 7**int(x) (a positive integer power of 7) | |
func TowerOfSevens(n int32) bool { | |
/* | |
cpu: Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz | |
BenchmarkTowersOfSeven-8 1000000000 0.4956 ns/op 0 B/op 0 allocs/op | |
*/ | |
return n > 0 && sevenPowEleven%n == 0 | |
} | |
func TowerOfSevensLoop(n int32) bool { | |
/* | |
cpu: Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz | |
BenchmarkTowersOfSevenLoop-8 283085700 4.334 ns/op 0 B/op 0 allocs/op | |
*/ | |
for n%7 == 0 { | |
n /= 7 | |
} | |
return n == 1 | |
} | |
const val0 = 1 | |
const val1 = 7 | |
const val2 = 49 | |
const val3 = 343 | |
const val4 = 2401 | |
const val5 = 16807 | |
const val6 = 117649 | |
const val7 = 823543 | |
const val8 = 5764801 | |
const val9 = 40353607 | |
const val10 = 282475249 | |
const val11 = 1977326743 | |
func TowerOfSevensTable(n int32) bool { | |
/* | |
cpu: Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz | |
BenchmarkTowersOfSevenTable-8 1000000000 0.8043 ns/op 0 B/op 0 allocs/op | |
*/ | |
switch n { | |
case val0, val1, val2, val3, val4, val5, val6, val7, val8, val9, val10, val11: | |
return true | |
} | |
return false | |
} | |
/* | |
BENCHMARK AND UNIT TESTS | |
*/ | |
const val = 7*7*7 + 100 | |
func BenchmarkTowersOfSeven(b *testing.B) { | |
/* | |
goos: windows | |
goarch: amd64 | |
pkg: gitlab.com/yqapp/dev/yq-gateway/appmetrics/towersogseven | |
cpu: Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz | |
BenchmarkTowersOfSeven-8 1000000000 0.4956 ns/op 0 B/op 0 allocs/op | |
PASS | |
ok gitlab.com/yqapp/dev/yq-gateway/appmetrics/towersogseven 1.625s | |
*/ | |
for i := 0; i < b.N; i++ { | |
TowerOfSevens(val) | |
} | |
} | |
func BenchmarkTowersOfSevenLoop(b *testing.B) { | |
/* | |
goos: windows | |
goarch: amd64 | |
pkg: gitlab.com/yqapp/dev/yq-gateway/appmetrics/towersogseven | |
cpu: Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz | |
BenchmarkTowersOfSevenLoop-8 283085700 4.334 ns/op 0 B/op 0 allocs/op | |
PASS | |
ok gitlab.com/yqapp/dev/yq-gateway/appmetrics/towersogseven 3.699s | |
*/ | |
for i := 0; i < b.N; i++ { | |
TowerOfSevensLoop(val) | |
} | |
} | |
func BenchmarkTowersOfSevenTable(b *testing.B) { | |
/* | |
goos: windows | |
goarch: amd64 | |
pkg: gitlab.com/yqapp/dev/yq-gateway/appmetrics/towersogseven | |
cpu: Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz | |
BenchmarkTowersOfSevenTable-8 1000000000 0.8043 ns/op 0 B/op 0 allocs/op | |
PASS | |
ok gitlab.com/yqapp/dev/yq-gateway/appmetrics/towersogseven 1.122s | |
*/ | |
for i := 0; i < b.N; i++ { | |
TowerOfSevensTable(val) | |
} | |
} | |
var poscases = [12]int{ | |
1, | |
7, | |
49, | |
343, | |
2401, | |
16807, | |
117649, | |
823543, | |
5764801, | |
40353607, | |
282475249, | |
1977326743, | |
} | |
var negcases = [22]int{ | |
2, 3, 4, 5, 6, 8, 14, 21, 28, 35, 42, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, | |
} | |
func TestTowerOfSevens(t *testing.T) { | |
for _, poscase := range poscases { | |
if !TowerOfSevens(int32(poscase)) { | |
t.Errorf("TowerOfSevens(%d) was 'false' but should be 'true'", poscase) | |
} | |
} | |
for _, negcase := range negcases { | |
if TowerOfSevens(int32(negcase)) { | |
t.Errorf("TowerOfSevens(%d) was 'true' but should be 'false'", negcase) | |
} | |
} | |
} | |
func TestTowerOfSevensLoop(t *testing.T) { | |
for _, poscase := range poscases { | |
if !TowerOfSevensLoop(int32(poscase)) { | |
t.Errorf("TowerOfSevensLoop(%d) was 'false' but should be 'true'", poscase) | |
} | |
} | |
for _, negcase := range negcases { | |
if TowerOfSevensLoop(int32(negcase)) { | |
t.Errorf("TowerOfSevensLoop(%d) was 'true' but should be 'false'", negcase) | |
} | |
} | |
} | |
func TestTowerOfSevensTable(t *testing.T) { | |
for _, poscase := range poscases { | |
if !TowerOfSevensTable(int32(poscase)) { | |
t.Errorf("TowerOfSevensTable(%d) was 'false' but should be 'true'", poscase) | |
} | |
} | |
for _, negcase := range negcases { | |
if TowerOfSevensTable(int32(negcase)) { | |
t.Errorf("TowerOfSevensTable(%d) was 'true' but should be 'false'", negcase) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment