Your task is to print the hexidecimal times table:
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f
00 02 04 06 08 0a 0c 0e 10 12 14 16 18 1a 1c 1e
00 03 06 09 0c 0f 12 15 18 1b 1e 21 24 27 2a 2d
00 04 08 0c 10 14 18 1c 20 24 28 2c 30 34 38 3c
00 05 0a 0f 14 19 1e 23 28 2d 32 37 3c 41 46 4b
00 06 0c 12 18 1e 24 2a 30 36 3c 42 48 4e 54 5a
00 07 0e 15 1c 23 2a 31 38 3f 46 4d 54 5b 62 69
00 08 10 18 20 28 30 38 40 48 50 58 60 68 70 78
00 09 12 1b 24 2d 36 3f 48 51 5a 63 6c 75 7e 87
00 0a 14 1e 28 32 3c 46 50 5a 64 6e 78 82 8c 96
00 0b 16 21 2c 37 42 4d 58 63 6e 79 84 8f 9a a5
00 0c 18 24 30 3c 48 54 60 6c 78 84 90 9c a8 b4
00 0d 1a 27 34 41 4e 5b 68 75 82 8f 9c a9 b6 c3
00 0e 1c 2a 38 46 54 62 70 7e 8c 9a a8 b6 c4 d2
00 0f 1e 2d 3c 4b 5a 69 78 87 96 a5 b4 c3 d2 e1
Specifications:
- You can print the hex values in uppercase.
- Your lines can end with a trailing space and the program output can end with a trailing newline.
- Every hex value must be padded to 2 digits with
0s as shown.
This is code-golf, so the shortest answer (measured in bytes) wins.
-
\$\begingroup\$ Related \$\endgroup\$Luis Mendo– Luis Mendo2016年12月17日 01:18:02 +00:00Commented Dec 17, 2016 at 1:18
-
5\$\begingroup\$ Multiplication tables don't usually include factor 0... :-) \$\endgroup\$Luis Mendo– Luis Mendo2016年12月17日 01:55:14 +00:00Commented Dec 17, 2016 at 1:55
-
31\$\begingroup\$ @Luis Mendo: How else will school children be able to memorize what 0 times a number is? :P \$\endgroup\$milk– milk2016年12月17日 01:56:11 +00:00Commented Dec 17, 2016 at 1:56
-
1\$\begingroup\$ Darn, I wanted to make a solution using hexdump, but that groups into 4-byte blocks. :( \$\endgroup\$hyperneutrino– hyperneutrino ♦2017年02月26日 01:04:21 +00:00Commented Feb 26, 2017 at 1:04
-
1\$\begingroup\$ Can we have more space between columns if we're consistent? \$\endgroup\$Adám– Adám2019年07月10日 23:46:30 +00:00Commented Jul 10, 2019 at 23:46
70 Answers 70
Python 2, 60 bytes
for n in range(256):r=n%16;print'%02x%s'%(n/16*r,r/15*'\n'),
How it works
For all integers n from 0 to 255, we do the following.
We compute (n / 16) ×ばつ (n % 16).
Over the range of n, both n / 16 and n % 16 independently cover the range 0, ..., 15, so this generates all entries of the multiplication table.
We repeat the linefeed character (
'\n') (n % 16) / 15 times, which results in the same character when n % 16 = 15 and an empty string otherwise.The format string
'%02x%s'turns the two previous results into a single string, first a lowercase hexadecimal integer representation, zero-padded to (at least) two digits, then the generated string.Finally,
print...,prints the formatted results.Since the print statement ends with a comma, Python will not append a linefeed. Also, before printing the next string, Python will prepend a space unless we're at the beginning of a new line. (source) This happens to format the output exactly like we want to.
Jelly, 12 bytes
×ばつþ`d4‘ịØhG
How it works
×ばつþ`d4‘ịØhG Main link. No arguments.
4 Set the return value to 16.
Ḷ Unlength; yield [0, ..., 15].
×ばつþ` Build the multiplication table of [0, ..., 15] and itself.
d4 Divmod 16; yield [p : 16, p % 16] for each product p.
‘ Increment quotients and remainders (1-based indexing).
ịØh Index into the lowercase hexadecimal alphabet.
G Grid; join columns by spaces, rows by newlines.
-
\$\begingroup\$ That's 12 characters, not bytes. According to the question, answer is measured in bytes, and your answer is 25 bytes and 12 characters. At least according to this website mothereff.in/byte-counter \$\endgroup\$jan– jan2016年12月18日 20:45:34 +00:00Commented Dec 18, 2016 at 20:45
-
20
R, 42 bytes
as.hexmode(sapply(0:15,function(x)x*0:15))
Prints the following:
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15] [,16]
[1,] "00" "00" "00" "00" "00" "00" "00" "00" "00" "00" "00" "00" "00" "00" "00" "00"
[2,] "00" "01" "02" "03" "04" "05" "06" "07" "08" "09" "0a" "0b" "0c" "0d" "0e" "0f"
[3,] "00" "02" "04" "06" "08" "0a" "0c" "0e" "10" "12" "14" "16" "18" "1a" "1c" "1e"
[4,] "00" "03" "06" "09" "0c" "0f" "12" "15" "18" "1b" "1e" "21" "24" "27" "2a" "2d"
[5,] "00" "04" "08" "0c" "10" "14" "18" "1c" "20" "24" "28" "2c" "30" "34" "38" "3c"
[6,] "00" "05" "0a" "0f" "14" "19" "1e" "23" "28" "2d" "32" "37" "3c" "41" "46" "4b"
[7,] "00" "06" "0c" "12" "18" "1e" "24" "2a" "30" "36" "3c" "42" "48" "4e" "54" "5a"
[8,] "00" "07" "0e" "15" "1c" "23" "2a" "31" "38" "3f" "46" "4d" "54" "5b" "62" "69"
[9,] "00" "08" "10" "18" "20" "28" "30" "38" "40" "48" "50" "58" "60" "68" "70" "78"
[10,] "00" "09" "12" "1b" "24" "2d" "36" "3f" "48" "51" "5a" "63" "6c" "75" "7e" "87"
[11,] "00" "0a" "14" "1e" "28" "32" "3c" "46" "50" "5a" "64" "6e" "78" "82" "8c" "96"
[12,] "00" "0b" "16" "21" "2c" "37" "42" "4d" "58" "63" "6e" "79" "84" "8f" "9a" "a5"
[13,] "00" "0c" "18" "24" "30" "3c" "48" "54" "60" "6c" "78" "84" "90" "9c" "a8" "b4"
[14,] "00" "0d" "1a" "27" "34" "41" "4e" "5b" "68" "75" "82" "8f" "9c" "a9" "b6" "c3"
[15,] "00" "0e" "1c" "2a" "38" "46" "54" "62" "70" "7e" "8c" "9a" "a8" "b6" "c4" "d2"
[16,] "00" "0f" "1e" "2d" "3c" "4b" "5a" "69" "78" "87" "96" "a5" "b4" "c3" "d2" "e1"
-
2\$\begingroup\$ How about: as.hexmode(outer(0:15,0:15,`*`)) \$\endgroup\$ixodesbeta– ixodesbeta2017年02月25日 00:41:50 +00:00Commented Feb 25, 2017 at 0:41
-
3\$\begingroup\$ Or better yet,
as.hexmode(0:15%o%0:15)\$\endgroup\$Giuseppe– Giuseppe2017年09月05日 14:13:37 +00:00Commented Sep 5, 2017 at 14:13
Bash, 38
- 1 byte saved thanks to @MitchellSpector
- 2 bytes saved thanks to @roblogic
printf %02x\ $[{0..15}*{0..15}]|rs 16
- Bash expands brace expansions before arithmetic expansions, so the string
$[{0..15}*{0..15}]first expands to$[0*0] $[0*1] $[0*2] ... $[0*15] $[1*0] ... $[15*15]. - The above series of arithmetic expansions then expand to the numerical table contents, as decimal integers.
- The
printf '%02x 'expresses this list of decimal integers as hex, zero-padded to two characters rs 16reshapes the output to 16 columns wide.
Alternative solution:
Bash + jot, 38
eval '
jot -s\ -w%02x 16 0 - '{0..15}
-
1\$\begingroup\$ Nice solution. It looks like you can shave one byte off by using fmt -52 (without the w). \$\endgroup\$Mitchell Spector– Mitchell Spector2016年12月18日 09:55:07 +00:00Commented Dec 18, 2016 at 9:55
-
\$\begingroup\$ nice! btw. in zsh it could be
{0..15}\*{0..15}which is 2 bytes shorter :) \$\endgroup\$ბიმო– ბიმო2019年03月16日 19:58:31 +00:00Commented Mar 16, 2019 at 19:58 -
1
C#6, 98 bytes
()=>{int i,j;for(i=-1;++i<16;)for(j=-1;++j<16;)System.Console.Write($"{i*j:x2} {j<15?"":"\n"}");};
Standard nested for-loop. Only trick is to print newline when j>=15.
-
\$\begingroup\$ @Metoniem tio.run/# is much superior \$\endgroup\$2019年03月16日 21:48:32 +00:00Commented Mar 16, 2019 at 21:48
JavaScript (ES6), (削除) 79 (削除ここまで) (削除) 78 (削除ここまで) 77 bytes
f=(i=256)=>i?f(--i)+(i%16*(i>>4)+256).toString(16).slice(1)+`
`[~i&15&&1]:``
document.write('<pre>'+f())
Edit: Saved 1 byte thanks to @ETHproductions and another byte thanks to @YairRand.
-
\$\begingroup\$ @ETHproductions Bah, the
.slice(-2)was left over from when I was doing('0'+toString(16)). I think I'd already tried' \n'[+!(~i&15)]but it's the same length. \$\endgroup\$Neil– Neil2016年12月17日 22:01:59 +00:00Commented Dec 17, 2016 at 22:01 -
\$\begingroup\$ @ETHproductions I also saved 1 bytes... \$\endgroup\$Neil– Neil2016年12月17日 22:14:26 +00:00Commented Dec 17, 2016 at 22:14
-
\$\begingroup\$ You can save a byte by replacing
(~i&15?' ':'\n')with' \n'[~i&15&&1]. \$\endgroup\$Yair Rand– Yair Rand2019年02月07日 04:57:45 +00:00Commented Feb 7, 2019 at 4:57 -
\$\begingroup\$ @YairRand I think you mean
'\n 'but I get the idea, thanks! \$\endgroup\$Neil– Neil2019年02月07日 10:21:07 +00:00Commented Feb 7, 2019 at 10:21
Haskell, 87 bytes
import Numeric
main=mapM(\x->putStrLn$do y<-s;['0'|x*y<16]++showHex(x*y)" ")s
s=[0..15]
I imagine there's a better way. Maybe depend on the printf package....
-
2\$\begingroup\$ Using no imports you could do
mapM(pure"0123456789abcdef")".."!!(x*y)++" "for the same bytecount, but you're right withprintf81 bytes. \$\endgroup\$ბიმო– ბიმო2019年03月16日 01:09:51 +00:00Commented Mar 16, 2019 at 1:09
K7, 36 bytes
` 0:" "/:'{`hex@`c$x}''{x*/:\:x}@!16
-
\$\begingroup\$
" "/:'(`hex@`c$)''x*/:x:!16should do it \$\endgroup\$Alexander Belopolsky– Alexander Belopolsky2019年04月14日 22:39:08 +00:00Commented Apr 14, 2019 at 22:39 -
1\$\begingroup\$ Better yet:
(" "/:`hex@')'x*/:x:`c$!16(26 bytes) \$\endgroup\$Alexander Belopolsky– Alexander Belopolsky2019年04月14日 22:47:17 +00:00Commented Apr 14, 2019 at 22:47
MATL, (削除) 19 (削除ここまで) 18 bytes
16:q&*1YAO3Z(!48e!
16:q % Push [0 1 ... 15]
&* % ×ばつ16 matrix of pairwise products
1YA % Convert to hexadecimal. Gives a ×ばつ2 char array
O3Z( % Assign char 0 to 3rd column. Gives a ×ばつ3 char array
!48e! % Reshape in row-major order as a 48-column char array
% Implicitly display. Char 0 is shown as space
Ruby, 49 bytes
256.times{|i|print"%02x "%(i/16*j=i%16),$/*j/=15}
Pretty straightforward use of the % operator equivalent to sprintf.
$/ is the line separator variable (\n by default.)
Note the use of assignments such as j/=15 to avoid longer parentheses (j/15)
PowerShell, 46 bytes
0..15|%{$i=$_;"$(0..15|%{"{0:X2}"-f($i*$_)})"}
Loops from 0 to 15, sets $i to be that current number, then loops again. Uses the -format operator with the X2 designation to specify the output is heXadecimal padded to 2 spaces with leading zeros.
Of special note, and really the only golf, is that instead of using a (...)-join' ' to take the hex results, encapsulate them in an array, and concatenate them together into a string, we leverage the fact that the default $OutputFieldSeparator value for stringifying an array is a space. That means we can do a string with a script block in it "$(...)" instead, saving 6 bytes.
Those strings are all left on the pipeline, and output via implicit Write-Output at program completion gives us a newline between them for free.
Python 3, 55 bytes
r=range(16)
for x in r:print(*['%02x'%(x*y)for y in r])
Using %formatting saves quite a few bytes over [2:] usage. So does using * splats on the print function.
Japt -R, (削除) 20 (削除ここまで) 15 bytes
GÆGÇ*X sGÃùT2 ̧
GÆGÇ*X sGÃùT2 ̧
G :16
Æ :Map each X in the range [0,G)
GÇ : Map the range [0,G)
*X : Multiply by X
sG : Convert to base-16 string
à : End map
ù : Left pad each
T : With 0
2 : To length 2
̧ : Join with spaces
:Implicitly join with newlines and output
-
\$\begingroup\$ You just as easily could've done
®instead ofË;P \$\endgroup\$ETHproductions– ETHproductions2017年08月22日 18:05:23 +00:00Commented Aug 22, 2017 at 18:05 -
\$\begingroup\$ @ETHproductions: Yeah, but I wanted to play with the shiny new shortcut! :D \$\endgroup\$Shaggy– Shaggy2017年08月22日 18:07:08 +00:00Commented Aug 22, 2017 at 18:07
QBasic, 73 bytes
FOR x=0TO 15
FOR y=0TO 15
?HEX$(x*y16円);HEX$(x*y MOD 16);" ";
NEXT
?
NEXT
I golfed a custom procedure for converting to hex digits, before I realized there's a builtin for that. Now my code is shorter and more boring. :^(
Original 103-byte code:
FOR x=0TO 15
FOR y=0TO 15
p x*y16円
p x*y MOD 16
?" ";
NEXT
?
NEXT
SUB p(d)
?CHR$(48+d-7*(d>9));
END SUB
Factor, 56 bytes
16 iota dup '[ _ [ * "%02X "printf ] with each nl ] each
I went through a progression as I realized matrix words are too long:
{ 16 16 } matrix-coordinates [ Π "%02X"sprintf ] matrix-map simple-table.
{ 16 16 } matrix-coordinates [ [ Π "%02X "printf ] each nl ] each
16 iota dup [ * "%02X"sprintf ] cartesian-map simple-table.
16 iota [ 16 iota [ * "%02X "printf ] with each nl ] each
16 iota dup '[ _ [ * "%02X "printf ] with each nl ] each
Mathematica, 46 bytes
Grid@Array[IntegerString[1##,16,2]&,{16,16},0]
Straightforward implementation using the built-in IntegerString, in base 16, padding to length 2. The Array[...,{16,16},0] has the two variables each run from 0 to 15.
Matlab, 53 Bytes
for i=[0:15]'*[0:15];fprintf('%02X ',i);disp(' ');end
Sample output:
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
00 02 04 06 08 0A 0C 0E 10 12 14 16 18 1A 1C 1E
00 03 06 09 0C 0F 12 15 18 1B 1E 21 24 27 2A 2D
00 04 08 0C 10 14 18 1C 20 24 28 2C 30 34 38 3C
00 05 0A 0F 14 19 1E 23 28 2D 32 37 3C 41 46 4B
00 06 0C 12 18 1E 24 2A 30 36 3C 42 48 4E 54 5A
00 07 0E 15 1C 23 2A 31 38 3F 46 4D 54 5B 62 69
00 08 10 18 20 28 30 38 40 48 50 58 60 68 70 78
00 09 12 1B 24 2D 36 3F 48 51 5A 63 6C 75 7E 87
00 0A 14 1E 28 32 3C 46 50 5A 64 6E 78 82 8C 96
00 0B 16 21 2C 37 42 4D 58 63 6E 79 84 8F 9A A5
00 0C 18 24 30 3C 48 54 60 6C 78 84 90 9C A8 B4
00 0D 1A 27 34 41 4E 5B 68 75 82 8F 9C A9 B6 C3
00 0E 1C 2A 38 46 54 62 70 7E 8C 9A A8 B6 C4 D2
00 0F 1E 2D 3C 4B 5A 69 78 87 96 A5 B4 C3 D2 E1
-
\$\begingroup\$ Try it online!(sort of...) \$\endgroup\$Pavel– Pavel2016年12月28日 21:02:39 +00:00Commented Dec 28, 2016 at 21:02
Perl, 48 bytes
for$a(@%=0..15){printf"%02x "x@%.$/,map$a*$_,@%}
I'm positive this isn't optimally golfed, but I'll be damned if I can find something better.
Code breakdown:
for$a(@%=0..15){printf"%02x "x@%.$/,map$a*$_,@%}
0..15 #Create a list of the range 0 - 15...
@%= #...and store it in the array @%
for$a( ){ } #Loop through @% with $a as the iterator
printf[ string ],[ params ] #Perl's port of the standard printf function
"%02x " #2-digit (hexit?) padding, followed by space...
x@% #...repeated 16 times (in scalar context, @% represents the size of array @%)...
.$/ #...followed by a newline
map$a*$_,@% #Loops through @%, and using $_ as the iterator, returns a list composed of each member of @% multiplied by the current $a
Perl 6, 42 bytes
.fmt("%02x").put for (^16 X*^16).rotor: 16
Expanded:
.fmt("%02x") # format each element of list to lowercase hex
.put # print with trailing newline
for # for each of the following
(
^16 # Range upto ( and excluding ) 16
X* # cross multiplied with
^16
).rotor: 16 # break it up into chunks of 16 values
JavaScript, 104 Bytes
s="";for(a=0;16>a;a++){for(b=0;16>b;b++)c=(a*b).toString(16),s=1==c.length?s+(" 0"+c):s+(" "+c);s+="\n"}
Call using variable s:
console.log("HEX Table: " + s)
Ungolfed code:
s=""; // Define s as empty string
for(a=0;16>a;a++){ // For y axis
for(b=0;16>b;b++) // For x axis
c=(a*b).toString(16),s=1==c.length?s+(" 0"+c):s+(" "+c); // Multiply and format
s+="\n" // Add line breaks
}
-
\$\begingroup\$ Isn't
"\n"line break? Wow, someone used pure ECMA for once. \$\endgroup\$Adalynn– Adalynn2016年12月28日 22:47:19 +00:00Commented Dec 28, 2016 at 22:47 -
\$\begingroup\$ And you should be able to use
s+=2>c.length?" 0"+c:" "+c. \$\endgroup\$Adalynn– Adalynn2016年12月28日 22:49:27 +00:00Commented Dec 28, 2016 at 22:49 -
\$\begingroup\$ I know this is old, but I noticed a few savings that might help in future challenges too! you can set both
aandsto""since""*0is still0. It's possible to inline yourb++to where it's being used ina*btoo for a another slight saving, but if you rewrite the string append to:s+=" "+(0+(a*b++).toString(16)).substr(-2)that'll save a chunk. Should be at 86 bytes with those! Hope that helps! \$\endgroup\$Dom Hastings– Dom Hastings2017年08月22日 07:39:32 +00:00Commented Aug 22, 2017 at 7:39
-
\$\begingroup\$ is that
iimplicitly deduced asinta standard feature of C? \$\endgroup\$sergiol– sergiol2017年01月04日 02:17:19 +00:00Commented Jan 4, 2017 at 2:17
C, (削除) 68 (削除ここまで) 66 bytes
f(i){for(i=0;i<256;)printf("%02x%c",i%16*(i++/16),i%16<15?32:10);}
-2 bytes thanks to ceilingcat!
Ungolfed:
f(i){
for(i=0; i<256;)
printf("%02x%c", i%16*(i++/16), i%16<15 ? 32 : 10);
}
Prints the zero padded result and either space or newline.
-
\$\begingroup\$ Is that
iimplicitly deduced asinta standard feature of C? \$\endgroup\$sergiol– sergiol2017年01月04日 02:18:31 +00:00Commented Jan 4, 2017 at 2:18 -
\$\begingroup\$ @sergiol yes,
intis the default assumption. \$\endgroup\$Karl Napf– Karl Napf2017年01月04日 07:51:36 +00:00Commented Jan 4, 2017 at 7:51 -
\$\begingroup\$ Sadly the output is undefined according to C standard (C99 - 6.5.2.2 Function calls). \$\endgroup\$Jasmes– Jasmes2017年08月22日 11:58:14 +00:00Commented Aug 22, 2017 at 11:58
-
\$\begingroup\$ Suggest
~i%16instead ofi%16<15\$\endgroup\$ceilingcat– ceilingcat2019年02月07日 19:24:59 +00:00Commented Feb 7, 2019 at 19:24 -
\$\begingroup\$ How about
f(i){for(i=-1;i++<256;)printf("%c%02x",i&15?32:10,i%16*(i/16));}? \$\endgroup\$12431234123412341234123– 124312341234123412341232024年08月16日 10:42:07 +00:00Commented Aug 16, 2024 at 10:42
APL (Dyalog), (削除) 44 (削除ここまで) (削除) 43 (削除ここまで) (削除) 36 (削除ここまで) 33 bytes
10 bytes saved thanks to @Adám
Crossed out 44 is still regular 44 ;(
↑,/{(⎕D,⎕A)[16 16⊤⍵],' '} ×ばつ⍨⍳16
Uses ⎕IO←0.
How?
×ばつ⍨⍳16 - multiplication table of 0 .. 15
̈ - for each item
16 16⊤⍵ - take the first 2 digits of hexadecimal encoding
(⎕D,⎕A)[...] - index into "0123456789ABCDEF..."
,' ' - append space
,/ - join each row
↑ - and columnify
Output
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
00 02 04 06 08 0A 0C 0E 10 12 14 16 18 1A 1C 1E
00 03 06 09 0C 0F 12 15 18 1B 1E 21 24 27 2A 2D
00 04 08 0C 10 14 18 1C 20 24 28 2C 30 34 38 3C
00 05 0A 0F 14 19 1E 23 28 2D 32 37 3C 41 46 4B
00 06 0C 12 18 1E 24 2A 30 36 3C 42 48 4E 54 5A
00 07 0E 15 1C 23 2A 31 38 3F 46 4D 54 5B 62 69
00 08 10 18 20 28 30 38 40 48 50 58 60 68 70 78
00 09 12 1B 24 2D 36 3F 48 51 5A 63 6C 75 7E 87
00 0A 14 1E 28 32 3C 46 50 5A 64 6E 78 82 8C 96
00 0B 16 21 2C 37 42 4D 58 63 6E 79 84 8F 9A A5
00 0C 18 24 30 3C 48 54 60 6C 78 84 90 9C A8 B4
00 0D 1A 27 34 41 4E 5B 68 75 82 8F 9C A9 B6 C3
00 0E 1C 2A 38 46 54 62 70 7E 8C 9A A8 B6 C4 D2
00 0F 1E 2D 3C 4B 5A 69 78 87 96 A5 B4 C3 D2 E1
-
\$\begingroup\$ 36, just by removing no-ops and using a variable:
⍪' ',⍨G G 2⍴⍉(⎕D,⎕A)[G⊥⍣¯1∘.×⍨⍳G←16]\$\endgroup\$Adám– Adám2017年10月26日 22:26:55 +00:00Commented Oct 26, 2017 at 22:26 -
\$\begingroup\$ @Adám thanks! I was just about to edit some of these changes, but yours is shorter \$\endgroup\$Uriel– Uriel2017年10月26日 22:30:40 +00:00Commented Oct 26, 2017 at 22:30
-
\$\begingroup\$ 33:
↑,/{(⎕D,⎕A)[16 16⊤⍵],' '}¨∘.×⍨⍳16\$\endgroup\$Adám– Adám2017年10月26日 22:39:32 +00:00Commented Oct 26, 2017 at 22:39
Julia, (削除) 83 (削除ここまで) 75 bytes
r=0:15;[([print(num2hex(i*j)[15:16]," ")for i in r],print('\n'))for j in r]
-
\$\begingroup\$ Try it online \$\endgroup\$Ashlin Harris– Ashlin Harris2024年08月03日 13:41:12 +00:00Commented Aug 3, 2024 at 13:41
PHP, (削除) 57 (削除ここまで) 56 bytes
while($z<256)printf("
"[$x=$z%16]."%02x ",$x*($z++>>4));
Run with -nr or try it online.
(削除) Note the trailing space in the first line! (削除ここまで) One byte saved by @gwaugh.
-
1
Forth (gforth), 65 bytes
: f 16. do cr 16. do i j * 16 /mod hex 1 .r . decimal loop loop ;
Explanation
Nested loop from 0 to 15. Each Iteration:
- multiply row and column numbers to get result
- split into first and second digit (first digit will be 0 if result < 16)
- set base to 16 (hexadecimal)
- print first digit with no space
- print second digit with space
- set base to 10 (decimal)
Code Explanation
: f \ start new word definition
16. do \ outer loop from 0 to 15
cr \ output newline
16. do \ inner loop from 0 to 15
i j * \ multiply loop indexes
16 /mod \ get quotient and remainder of dividing by 16
hex \ set base to 16
1 .r \ print quotient (equivalent to first digit) right-aligned in space of 1 (no space)
. \ print second digit (with space)
decimal \ set base back to 10 (decimal)
loop \ end inner loop
loop \ end outer loop
; \ end word definition
C# (Visual C# Interactive Compiler), 67 bytes
for(int i=0;i<256;)Write($"{(i%16<1?"\n":"")} {i/16*(i++%16):X2}");
APL(NARS), 42 chars, 84 bytes
{{(⎕D,⎕A)[1+16∣(⌊⍵÷16),⍵]} ×ばつ/ ̈m∘.,m←0,⍳15}
I copy something from others... ×ばつ/ ̈m∘.,m←0,⍳15 would build the multiplication table of 0,⍳15; the function
{(⎕D,⎕A)[1+16∣(⌊⍵÷16),⍵]} convert each number in the multiplication table in hex for only 2 digits.
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
00 02 04 06 08 0A 0C 0E 10 12 14 16 18 1A 1C 1E
00 03 06 09 0C 0F 12 15 18 1B 1E 21 24 27 2A 2D
00 04 08 0C 10 14 18 1C 20 24 28 2C 30 34 38 3C
00 05 0A 0F 14 19 1E 23 28 2D 32 37 3C 41 46 4B
00 06 0C 12 18 1E 24 2A 30 36 3C 42 48 4E 54 5A
00 07 0E 15 1C 23 2A 31 38 3F 46 4D 54 5B 62 69
00 08 10 18 20 28 30 38 40 48 50 58 60 68 70 78
00 09 12 1B 24 2D 36 3F 48 51 5A 63 6C 75 7E 87
00 0A 14 1E 28 32 3C 46 50 5A 64 6E 78 82 8C 96
00 0B 16 21 2C 37 42 4D 58 63 6E 79 84 8F 9A A5
00 0C 18 24 30 3C 48 54 60 6C 78 84 90 9C A8 B4
00 0D 1A 27 34 41 4E 5B 68 75 82 8F 9C A9 B6 C3
00 0E 1C 2A 38 46 54 62 70 7E 8C 9A A8 B6 C4 D2
00 0F 1E 2D 3C 4B 5A 69 78 87 96 A5 B4 C3 D2 E1
I see how using functions and ̈ problems are break in simple problems...
Explore related questions
See similar questions with these tags.