Skip to content

Commit

Permalink
rockskip: ruler function is just bits.TrailingZeros (#62567)
Browse files Browse the repository at this point in the history
I couldn't help but make this change after seeing the algorithm. Feel
free to just close this if this makes it less clear, but this kinda
feels clearer in my head but I am probably weird.

Test Plan: TestRuler passes
  • Loading branch information
keegancsmith committed May 13, 2024
1 parent 68fe349 commit 1515512
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions internal/rockskip/server.go
Expand Up @@ -3,6 +3,7 @@ package rockskip
import (
"context"
"database/sql"
"math/bits"
"sync"
"time"

Expand Down Expand Up @@ -168,10 +169,11 @@ func getHops(ctx context.Context, tx dbutil.DB, commit int, tasklog *TaskLog) ([
//
// https://oeis.org/A007814
func ruler(n int) int {
height := 0
for n > 0 && n%2 == 0 {
height++
n = n / 2
if n <= 0 {
return 0
}
return height
// ruler(n) is equivalent to asking how many times can you divide n by 2
// before you get an odd number. That is the number of 0's at the end of n
// when n is written in base 2.
return bits.TrailingZeros(uint(n))
}

0 comments on commit 1515512

Please sign in to comment.