zsh, 8686 56 bytes
thanks to @Dennis for saving 30(!) bytes
function { (for a in {1..$1};do for;for b in {1..$1};do echo;echo $((a*b));done;done$[a*b])|sort -nu}
Explanation / ungolfed:
function { # declare an anonymous function
( ( # begin subshell
for a in {1..$1} # loop through every pair of multiplicands
do
for b in {1..$1}
do
echo $[a*b] echo $((a*b)) # calculate a * b, output to stdout
done
done
) | sort -nu # pipe output of subshell to `sort -nu', sorting
# numerically (-n) and removing duplicates (-u for uniq)
}
This doesn't work in Bash because Bash doesn't expand {1..$1}
—it just interprets it literally (so, a=5; echo {1..$a}
outputs {1..5}
instead of 1 2 3 4 5
).
Bash doesn't have anonymous functions.
Bash does not expand
{1..$1}
—it just interprets it literally (so,a=5; echo {1..$a}
outputs{1..5}
instead of1 2 3 4 5
).