Create every combination of variable groups up to order n
LATEX, 354 bytes
When I saw this I knew it had to be done in Latex. Equations just look so crisp and clean in Latex and I can't stand using ^
for power.
\documentclass{article}\input tikz\usepackage{intcalc}\usepackage{ifthen}\begin{document}\typein[\a]{}\typein[\b]{}\foreach\x in{1,...,\intcalcPow{\b+1}{\a}}{\begin{equation}\foreach[count=\i]\y in{a,...,z}{\ifthenelse{\(\i<\a\)\OR\(\i=\a\)}{\y^\intcalcDiv{\intcalcMod{\x}{\intcalcPow{\b+1}{\i}}}{\intcalcPow{\b+1}{\i-1}}}{}}\end{equation}}\end{document}
Explanation
There are three main forces at work here \typein
which is what allows us to take input from the command line, the intcalc
package which is what allows us to make calculations with our variables, and the Latexequation
environment.
Once we have taken in input we begin a loop we loop \intcalcPow{\b+1}{\a}
times, once for each result we want to print. Each loop we begin an equation
environment and loop through the alphabet keeping track of \y
for the current letter and \i
for the current number of runs. If \i
is greater than or equal to \a
we don't print anything at all (according to the specs this is not strictly necessary however Latex will overflow for values greater than 1 if we don't do this). We then print \y
to our equation and raise it to the power of
\intcalcDiv{\intcalcMod{\x}{\intcalcPow{\b+1}{\i}}}{\intcalcPow{\b+1}{\i-1}}
That whole mess simply means take the \i
th digit of \x
in base \b+1
. This ensures that the powers are decoded properly.
Example output:
Here is the output for 3, 2
Mathematica, 51 50 bytes
Rest[1##&@@@PowerRange[1,#^#2,#]~Distribute~List]&
Assumes "given m
variables" means the first input is a list of variables.
If first input is an integer, 69 bytes
Rest[1##&@@@PowerRange[v=Unique[]~Table~#;1,v^#2,v]~Distribute~List]&
The variables are in the form $<integer>
(e.g. $5
)
Haskell, 71 58 54 53 bytes
n#m=tail$concat<$>mapM(\x->(\i->x<$[1..i])<$>[0..n])m
Returns a list of strings and uses the output format "aabbb"
for "a^2 b^3"
.
Usage example: 3 # "ab"
-> ["b","bb","bbb","a","ab","abb","abbb","aa","aab","aabb","aabbb","aaa","aaab","aaabb","aaabbb"]
. Try it online!.
Many bytes are spent for output formatting. A more flexible output, e.g. pairs of (variable, power) -> [('a',2),('b',3),('c',1)]
for "a^2 b^3 c^1"
would save a lot.
How it works
mapM(\x-> )m -- for each variable x in the input list m
\i->x<$[1..i] -- make i copies of x
<$>[0..n] -- for all numbers from 0 to n
-- in fact mapM makes all possible combinations hereof, i.e.
-- [["",""], ["", "b"], ["", "bb"] ... ["a",""], ["a","b"], ...]
concat<$> -- join all inner lists
-- e.g ["aa","bbb"] -> "aabbb"
tail -- drop the first (all powers = ^0)
With maximum flexibility, i.e. output format as (variable, power) pairs and including all-zero powers ("a^0 b^0 c^0"
) it boils down to
Haskell, 25 bytes:
f n=mapM((<$>[0..n]).(,))
Usage example: f 2 "ab"
:
[[('a',0),('b',0)],
[('a',0),('b',1)],
[('a',0),('b',2)],
[('a',1),('b',0)],
[('a',1),('b',1)],
[('a',1),('b',2)],
[('a',2),('b',0)],
[('a',2),('b',1)],
[('a',2),('b',2)]]
Dropping all-zero powers costs 5 bytes for a total of 30: f n=tail.mapM((<$>[0..n]).(,))
.