Advertisement
sci4me

Gollatz

Dec 30th, 2017
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.27 KB | None | 0 0
  1. func steps(n uint64) uint64 {
  2.     if n <= 0 { return 0; }
  3.     if n == 1 { return 0; }
  4.  
  5.     var steps uint64
  6.  
  7.     i := n
  8.     for i != 1 {
  9.         for (i & 1) == 1 {
  10.             i = ((3 * i) + 1) / 2
  11.             steps += 2
  12.         }
  13.  
  14.         for (i & 1) == 0 {
  15.             i /= 2
  16.             steps++
  17.         }
  18.     }
  19.  
  20.     return steps
  21. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement