Skip to content

Instantly share code, notes, and snippets.

@allanino
Created February 25, 2015 15:00
Show Gist options
  • Select an option

  • Save allanino/a77b28c6684bf45fac0e to your computer and use it in GitHub Desktop.

Select an option

Save allanino/a77b28c6684bf45fac0e to your computer and use it in GitHub Desktop.
Generate sequential 3 bytes sequences with characters in [0-9a-z]
def character(i)
return i < 10 ? (i + 48).chr : (i + 87).chr
end
def generate(n)
i1 = n % 36
i3 = n/(36*36)
i2 = n/36 - i3*36
return "#{character(i3)}#{character(i2)}#{character(i1)}"
end
# Some examples
for i in 0..50
puts "#{i} => #{generate(i)}\n"
end
for i in 10000..10050
puts "#{i} => #{generate(i)}\n"
end
for i in 40000..45000
puts "#{i} => #{generate(i)}\n"
end
# Last allowed number
i = 46655 # 36*36*36 - 1
puts "#{i} => #{generate(i)}\n"
# Breaks for the next
i = 46656
puts "#{i} => #{generate(i)}\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment