Hexadecimal is a base 16 counting system that goes from 0 to f. Your job is to make a counter that will display these numbers.

Example:

$python counter.py 1 2 3 4 5 6 7 8 9 a b c d e f 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f 20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f 30  Rules: • The numbers may be separated by spaces, tabs, or new lines. • The minimum number you must go to is 30 (48 in decimal). • You may also make the program print numbers forever until it is stopped. • Letters may be in uppercase or lowercase (A or a). • No built in functions allowed (that directly affect hexadecimal conversions/counting). • Leading zeros are allowed • It may start from 1 or 0 • Shortest code wins! • @Sp3000 How built in are they? Converting decimal to hex? – phase Jul 12 '15 at 16:05 • @Sp3000 No built in functions allowed! – phase Jul 12 '15 at 16:09 • How about general base conversion functions then? – Sp3000 Jul 12 '15 at 16:11 • @Sp3000 Sure (ignore this, 15 character limit) – phase Jul 12 '15 at 16:13 • @Mauris Yes! That sure is going to be interesting... – phase Jul 13 '15 at 17:29 ## 26 Answers # Pyth - 12 bytes Uses cartesian product, and sorts at the end to get in correct order, then joins by spaces. Prints 00-ff inclusive. jdS^s+<G6UT2  jd Join by spaces S Sort lexiographically ^ 2 Cartesian product repeat twice s+ Append then concatenate entire list <G6 First six of alphabet UT Range 0-9  # CJam, 21 14 bytes A,_6,'Af++m*S*  Prints the numbers 00 to 9F. Try it online in the CJam interpreter. ### How it works A, e# Push [0 ... 9]. _ e# Push a copy. 6, e# Push [0 ... 5]. 'Af+ e# Add 'A' to each. This pushes "ABCDEF". + e# Concatenate. This pushes [0 ... 9 'A' ... 'F']. m* e# Cartesian product. This pushes [[0 0] ... [9 'F']. S* e# Join, separating by spaces.  ## JavaScript (ES6), 57 bytes Same approach as the Python ones I suppose. for(i of c='0123456789ABCDEF')for(j of c)console.log(i+j)  # Bash + coreutils, 31 echo {A..P}{A..P}|tr A-P 0-9A-F  # Python 2, 52 a=0 for b in'0123456789ABCDEF'*4:printa+b;a+=b>'E'  Prints 00 to 3F. Takes advantage of the fact that the first digit a is always a number in this range. Loops through four cycles of the second digit b, incrementing a whenever the second digit is F. This is one char shorter than the more direct for a in'0123': for b in'0123456789ABCDEF':print a+b  • n ='0123' should save some chars – Caridorc Jul 13 '15 at 13:45 • @Caridorc How exactly? – xnor Jul 13 '15 at 20:10 • by writing thing in n + restofstring – Caridorc Jul 13 '15 at 20:11 • @Caricord Not sure what you mean, it's longer to do n='0123' for a in n: for b in n+'456789ABCDEF':print a+b – xnor Jul 13 '15 at 20:12 • @Caridorc A metal shortcut I use is that saving to a variable costs 4 chars, so it needs >4 chars of saving to compensate, so saving 4 chars for 0123 for something else isn't enough. – xnor Jul 13 '15 at 20:15 ## Befunge-93, 57 bytes <_v#-*44:+1,*84,g2:\,g2:\ ^ >$1+:9-!#@_0
0123456789ABCDEF


Prints numbers from 00to 8F. If you prefer your programs to run forever, the version below is non-terminating and will continually output all numbers from 00 to FF.

<_v#-*44:+1,*84,g2:\,g2:\
^ >$1+:35*!*0 0123456789ABCDEF  • You can save a couple bytes in -98 with <_v#-f:+1, ',g2:\,g2:\. Can't see many improvements beyond that. – Jacob Jul 13 '15 at 17:05 • 0123456789ABCDEF01g::88+/2-0g,88+%0g,9,1+01p – Lynn Jul 13 '15 at 17:29 • That's 44 bytes. It loops forever, like your second solution, and prints wrong results past the second 1F. It requires an implementation (such as the reference implementation bef.c) that silently ignores unknown commands (ABCDEF). – Lynn Jul 13 '15 at 17:31 • (The OP mentions it's okay for a solution to "break" somewhere past hitting 30 -- this one will slowly overflow the stack, so I suppose there's some point of termination. Also, output is tab-separated; the OP said this was fine.) Oh, the Befunge implementation you use should also initialize the whole 80x25 torus with spaces (ASCII 0x20). – Lynn Jul 13 '15 at 17:37 • @Mauris Regarding your comment about the implementation needing to instantiate the entire torus with spaces, would this affect the byte count for my code presented? I only counted the necessary characters rather than filling in the corners with spaces. – Sok Jul 14 '15 at 8:16 # TI-Basic, 63 bytes :For(I,0,4,16⁻¹ :Disp sub(" 0123456789ABCDEF",1+16fPart(I),2 :Output(7,1,int(I :End  This is 63 bytes, according to the memory management screen on my calculator, a TI-84+. Make sure to start the program with a partially filled home screen! • Did you remember to subtract the length of the 9-byte header and the program name from the code length? – lirtosiast Jul 13 '15 at 2:58 # C, 78 75 bytes x(y){return y+48+y/10*7;}f(j){for(j=0;printf("%c%c ",x(j/16),x(15&j++)););}  We define a function f() to be called with no arguments for printing, and a helper function x(int). This breaks at FF. Amazingly, this is one byte shorter than the more obvious: char*s="0123456789ABCDEF";h(j){for(j=0;printf("%c%c ",s[j/16],s[15&j++]););}  Warning: it is not recommended to run this code outside of a debug environment... Testing: int main(int argc, char** argv) { f(); return 0; }  Output: 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F 20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F 30 31 32 33 34 35 36 (...)  Of course, the more robust (and cheat-y) approach is this 34-byte function: g(i){for(i=0;printf("%x ",i++););}  • Started trying this but my answer was too similar. You can save several bytes by making the first %c into %d and omitting the function. It's only valid up to 9F then though. – Alchymist Jul 14 '15 at 15:25 • return y+ could possibly be y+=. – Jonathan Frech Aug 19 '18 at 4:25 # Pyth, 17 bytes VJs++kUT<G6FYJ+NY  Try it here ## How it works:  <G6 # "abcdef" UT # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] k # an empty string (so + means concatenation, not addition) s++ # join them all ("0123456789abcdef") J # call this J V # for each N in J... FYJ # for each Y in J... +NY # print N and Y  • jb^+jkUT<G6 2 uses the cartesian product to do the same thing, still seems golfable... – FryAmTheEggman Jul 12 '15 at 19:42 # Javascript ES6, 67 62 bytes (x=''.replace.bind('0123456789ABCDEF',/./g))(n=>x(o=>' '+n+o))  # J, 22 bytes >{;~'0123456789abcdef'  Counts to ff. Prints an extra newline between each block of 0x10 numbers, like so: ... 0d 0e 0f 10 11 ...  # CJam, 22 bytes 1{_GbA,6,'af++f=oNo)}h  This runs forever, and thus is probably one of the rare times where it's a good idea not to include a permalink. • oNo is the same as n in TIO. – Esolanging Fruit Apr 27 '17 at 19:01 # Python 2, 66 55 Bytes This should really have been the most obvious approach to me.. a='0123456789ABCDEF' for x in a: for y in a:print x+y  Old (66 Bytes): Technically this causes an error after FF, but it does reach 30. n=1;a='0123456789ABCDEF' while 1:print a[n/16]*(n>15)+a[n%16];n+=1  I assumed string formatting wasn't allowed since I'm pretty sure it would go through base conversion, but if it was allowed, this would be 29 bytes: n=1 while 1:print"%x"%n;n+=1  # Java, 104 bytes char t[]="0123456789abcdef".toCharArray(),i;void f(){for(;i<99;)System.out.println(""+t[i/16]+t[i++%16]);}  If the i<99 is removed, it still reaches 30, but eventually crashes. I'm not sure if that's acceptable. ## J, 47 bytes '0123456789abcdef'{~([:|:2 256$(]#i.),256$i.)16  prints 00 to ff • A much shorter way: >{;~'0123456789abcdef' – Lynn Jul 13 '15 at 16:43 • Wow, that's very good! But why didn't you post it as an answer, it's only 22 bytes! – gar Jul 14 '15 at 14:17 # Mumps - 65 bytes S Q="0123456789ABCDEF" F I=1:1:16 F J=1:1:16 W$E(Q,I),$E(Q,J),!  Nope... Mumps ain't dead yet! :-) # JavaScript 747265 60 //for(i=0,a="0123456789ABCDEF";i++<49;)console.log(a[i>>4]+a[i%16]) for(i=0;i++<48;)console.log((i>>4)+"0123456789ABCDEF"[i%16]) # Dyalog APL, 12 bytes  ∘.,⍨16↑⎕D,⎕A 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F 20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F 30 31 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F 40 41 42 43 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F 50 51 52 53 54 55 56 57 58 59 5A 5B 5C 5D 5E 5F 60 61 62 63 64 65 66 67 68 69 6A 6B 6C 6D 6E 6F 70 71 72 73 74 75 76 77 78 79 7A 7B 7C 7D 7E 7F 80 81 82 83 84 85 86 87 88 89 8A 8B 8C 8D 8E 8F 90 91 92 93 94 95 96 97 98 99 9A 9B 9C 9D 9E 9F A0 A1 A2 A3 A4 A5 A6 A7 A8 A9 AA AB AC AD AE AF B0 B1 B2 B3 B4 B5 B6 B7 B8 B9 BA BB BC BD BE BF C0 C1 C2 C3 C4 C5 C6 C7 C8 C9 CA CB CC CD CE CF D0 D1 D2 D3 D4 D5 D6 D7 D8 D9 DA DB DC DD DE DF E0 E1 E2 E3 E4 E5 E6 E7 E8 E9 EA EB EC ED EE EF F0 F1 F2 F3 F4 F5 F6 F7 F8 F9 FA FB FC FD FE FF  • For once, APL matches Pyth. – Adám Sep 3 '15 at 13:34 # Haskell, 52 bytes a="0123456789abcdef";main=mapM putStrLn$mapM id[a,a]


# Python 2 - 57 bytes

h='0123456789ABCDEF'
' '.join([i+j for i in h for j in h])


This outputs 00 to FF, with spaces between.

## Python 3, 157 bytes

As far as I can tell, this is the only submission so far that can keep counting upwards forever. (Of course, I can't read Haskell or Mumps, but they all seem to be doing a two-character loop...) EDIT: Okay, not the only one, but perhaps the only one in a non-golfy language?

n=bytearray(b'0')
while 1:
print(repr(n)[12:-2]);p,o=len(n),1
while o:
p-=1;m=n[p];n[p]={57:65,70:48}.get(m,m+1);o=m==70
if o and p==0:p=1;n[:0]+=b'0'


Proof of correctness (wraps the above in a function, changing only print to yield, and then loops as high as you like):

def golfed_code():
n=bytearray(b'0')
while 1:
yield(repr(n)[12:-2]);p,o=len(n),1
while o:
p-=1;m=n[p];n[p]={57:65,70:48}.get(m,m+1);o=m==70
if o and p==0:p=1;n[:0]+=b'0'

TEST_LIMIT = 100000

for n, h in enumerate(golfed_code()):
assert hex(n).upper()=='0X'+h,'{} != {}'.format(hex(n).upper(),'0X'+h)
if n == TEST_LIMIT:
break


(Thanks to Sp3000 for saving me 3 bytes.)

• @Sp3000: Not that I could find (though that doesn't mean one doesn't exist). Everything I tried took more characters, usually because I was constructing a new bytearray (and that's a big word). Also: fine, it's the only endless counter in a non-golfy language. ;-) – Tim Pederick Jul 16 '15 at 9:55
• @Sp3000: I missed that first one (everything else I tried with b'' literals left me with a bytes, not a bytearray). But the second one means a is modified right along with n. – Tim Pederick Jul 16 '15 at 10:10
• Right, thought they were immutable for some reason. I'm not sure why you're using bytearrays though, surely division/modulo + string building is shorter? – Sp3000 Jul 16 '15 at 10:15
• @Sp3000: Because their string representation automatically ASCIIfies them. I think if I found an approach other than bytearray, I'd make it a new answer anyway; it'd be too much like "chuck it out and start over" to edit into this one. – Tim Pederick Jul 16 '15 at 10:18
• It's now not the only example of an endless counter from a non-golf language put [R~] (|(0..9),|('A'..'F'))[.polymod: 16 xx*]for 0..* in Perl 6 – Brad Gilbert b2gills Jan 17 '16 at 15:57

# Perl 6, 34 bytes

The shortest that I can come up with that doesn't use any sort of conversion is:

put [X~] (|(0..9),|('A'..'F'))xx 2 # 34 bytes


prints 00 ... FF space separated in order.
If you want more you can swap 2 for a larger number.
(don't use a number bigger than 4 as it concatenates the values together before outputting anything, so it would use a significant amount of RAM )

Shortest that will never stop writing hex values

put [R~] (|(0..9),|('A'..'F'))[.polymod: 16 xx*]for 0..* # 56 bytes


If printf were allowed

printf "%X ",$_ for 0..* # 24 bytes  If a base conversion function were allowed put .base(16)for 0..* # 21 bytes  # Matlab: 47 bytes q=[48:57,97:102,''];fliplr(char(combvec(q,q)'))  I don't know if it would be considered as counter. The code generates vector of hex numbers from 00 to ff. q is a string '0123456789abcdef' in double format. Combvec generates all possible pairs between q and q. Char converts it to string format and fliplr flips the columns to get: 00 01 ... 08 09 0a 0b 0c 0d 0e 0f 10 11 ... fd fe ff  ## C++14 - 135 #include<string> #include<iostream> void f(){std::string a="0123",b="0123456789ABCDEF";for(char c:a)for(char d:b)std::cout<<c<<d<<" ";}  • No, it's fine like it is. What compiler are you using? I get 'string' is not a member of 'std' with mine. – Dennis Jan 17 '16 at 19:11 • @Dennis That's a good point. I always forget that it requires including string as it's own. Fixed. – Yytsi Jan 17 '16 at 19:28 • 1. I'm getting the same error for cout as well. I guess you need iostream too. 2. It prints the numbers without separation. The challenge requires spaces, tabs or newlines. 3. You should mention the required version of C++. – Dennis Jan 17 '16 at 22:11 # jq 1.5: 65 59 characters (56 characters code + 3 characters command line option.) [range(10)]+"a b c d e f"/" "|{a:.[],b:.}|"\(.a)\(.b[])"  Sample run: bash-4.3$ jq -n -r '[range(10)]+"a b c d e f"/" "|{a:.[],b:.}|"\(.a)\(.b[])"' | head
00
01
02
03
04
05
06
07
08
09


On-line test (Passing -r through URL is not supported – check Raw Output yourself.)

# jq 1.5: 56 characters

(53 characters code + 3 characters command line option.)

[[range(10)]+"a b c d e f"/" "|"\(.[])\(.[])"]|sort[]


This produces correct output, however is not exactly a counter: it not generates the values in order, just sorts them after.

On-line test (Passing -r through URL is not supported – check Raw Output yourself.)

• your link for jq doesn't work, and when I fixed it it says there isn't an index file on github :P – phase Jul 13 '15 at 17:24
• Oops. Thank you @Phase. I was too concentrated on the character count. – manatwork Jul 13 '15 at 17:29

## Ruby, 64 bytes

h='0123456789abcdef'.split //;h.each{|i|h.each{|j|print i,j,?\s}}


Explanation:

h = '0123456789abcdef'.split // # Split string of hex chars at
# matches of //, returns array
# splits array by characters
h.each { |i|                    # loop 1
h.each { |j|                  # loop 2
print i,j,?\s               # print() can take multiple params,
# and it is the same as printing
# each one using a separate print().
# ?x is the same as 'x', so ?\s is
# the same as "\s", which is " ".
}
}
`