N to the power of N (with strings)

var input:String =  "a, b, c";

var words:Array = input.split(", ");
var max:String = "";
var maxD:String =  (words.length - 1).toString();
for (var i:int = 0; i<words.length; i++){
	max += maxD;
}
var maxInt:int = parseInt(max,words.length);

for(i = 0; i<=maxInt; i++){
	 var indices:String = i.toString(words.length);
	 var r:String = "";
	 var k:int=0;
	 for (var j:int = 0; j<indices.length; j++){
		 r += words[parseInt(indices.charAt(j))] +" ";
		 k++;
	 }
	 while(k < words.length) {
		r = words[0] +" "+ r;
		k++;
	 }
	trace(r);
}
trace(i, " variations");

Like many things on this site, I coded this rather quickly and it can probably be cleaned up…

Setting the input variable of the above snippet to “a, b” will output:

a a
a b
b a
b b
4 variations

Setting the input to “a, b, c” will output:

a a a
a a b
a a c
a b a
a b b
a b c
a c a
a c b
a c c
b a a
b a b
b a c
b b a
b b b
b b c
b c a
b c b
b c c
c a a
c a b
c a c
c b a
c b b
c b c
c c a
c c b
c c c
27 variations

I created this to work with words… inputs like “bread, breath, blobs, backwards”. Be careful because you can quickly get millions of outputs:

1 to the power of 1 is 1
2 to the power of 2 is 4
3 to the power of 3 is 27
4 to the power of 4 is 256
5 to the power of 5 is 3125
6 to the power of 6 is 46,656
7 to the power of 7 is 823,543
8 to the power of 8 is 16,777,216
etc…

Leave a Reply

Your email address will not be published. Required fields are marked *