Skip to content

Commit

Permalink
Merge 6fd9662 into 7107dc6
Browse files Browse the repository at this point in the history
  • Loading branch information
vaskoz committed Mar 20, 2019
2 parents 7107dc6 + 6fd9662 commit 6e6805e
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,4 @@ problems from
* [Day 206](https://github.com/vaskoz/dailycodingproblem-go/issues/424)
* [Day 207](https://github.com/vaskoz/dailycodingproblem-go/issues/426)
* [Day 208](https://github.com/vaskoz/dailycodingproblem-go/issues/427)
* [Day 210](https://github.com/vaskoz/dailycodingproblem-go/issues/432)
25 changes: 25 additions & 0 deletions day210/problem.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package day210

// LongestCollatzSequence returns the number with the longest
// sequence.
// First result is the number, second number is the length of the sequence.
func LongestCollatzSequence(max int) (int, int) {
var n, longest int
for i := max; i > 0; i-- {
count := 0
v := i
for v != 1 {
if v%2 == 0 {
v /= 2
} else {
v = 3*v + 1
}
count++
}
if count > longest {
longest = count
n = i
}
}
return n, longest
}
28 changes: 28 additions & 0 deletions day210/problem_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package day210

import (
"testing"
)

func TestLongestCollatzSequence(t *testing.T) {
t.Parallel()
t.Skip("unskip test to verify result. takes too long to run")
n, length := LongestCollatzSequence(1000000)
if n != 837799 || length != 524 {
t.Errorf("Expected (837799,524), got (%v,%v)", n, length)
}
}

func TestLongestCollatzSequenceSmaller(t *testing.T) {
t.Parallel()
n, length := LongestCollatzSequence(10)
if n != 9 || length != 19 {
t.Errorf("Expected (9, 19), got (%v,%v)", n, length)
}
}

func BenchmarkLongestCollatzSequence(b *testing.B) {
for i := 0; i < b.N; i++ {
LongestCollatzSequence(10)
}
}

0 comments on commit 6e6805e

Please sign in to comment.