Created
February 25, 2015 15:00
-
-
Save allanino/a77b28c6684bf45fac0e to your computer and use it in GitHub Desktop.
Generate sequential 3 bytes sequences with characters in [0-9a-z]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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