sci4me

Pure Collatz

Feb 21st, 2016
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function collatz(n) {
  2.     if(n % 2 == 0) {
  3.         return n / 2;
  4.     } else {
  5.         return 3 * n + 1;
  6.     }
  7. }
  8.  
  9. function steps(n) {
  10.     if(n <= 1) {
  11.         return 0;
  12.     }
  13.    
  14.     function s1(a, b) {
  15.         if(a == 1) {
  16.             return b;
  17.         }
  18.        
  19.         return s1(collatz(a), b + 1);
  20.     }
  21.    
  22.     return s1(n, 0);
  23. }
  24.  
  25. console.log(steps(17));
Add Comment
Please, Sign In to add comment