Ruler function

From Esolang
Jump to navigation Jump to search

The ruler function of an integer is how many times the integer can be evenly divided by two. For each positive integer, this results in the sequence:

0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, ...

The function can also be defined the same way, plus one:

1, 2, 1, 3, 1, 2, 1, 4, 1, 2, 1, 3, 1, 2, 1, 5, ...

The second can also be constructed from the first by omitting all zeroes, and vice versa by inserting zeroes before every number. The same sequence can also be used to solve the Tower of Hanoi.

Alternate constructions

  • Starting with zero, repeatedly duplicating the current sequence and incrementing the last number once will produce the sequence.
  • Counting the number of trailing zeroes in the integer's binary representation will produce the sequence.
    • Taking the least significant bit set to 1 and calculating produces the same result.
  • Each number in the sequence first appears at , and repeats every places.

Implementations

Lua

l=math.log for i=1,1/0 do print(l(i&-i)/l(2)) end

Python

import math
a=1
while 1:print(math.log2(a&-a));a+=1