N to the power of N (with strings)

Actionscript:
  1. var input:String =  "a, b, c";
  2.  
  3. var words:Array = input.split(", ");
  4. var max:String = "";
  5. var maxD:String =  (words.length - 1).toString();
  6. for (var i:int = 0; i<words.length; i++){
  7.     max += maxD;
  8. }
  9. var maxInt:int = parseInt(max,words.length);
  10.  
  11. for(i = 0; i<=maxInt; i++){
  12.      var indices:String = i.toString(words.length);
  13.      var r:String = "";
  14.      var k:int=0;
  15.      for (var j:int = 0; j<indices.length; j++){
  16.          r += words[parseInt(indices.charAt(j))] +" ";
  17.          k++;
  18.      }
  19.      while(k <words.length) {
  20.         r = words[0] +" "+ r;
  21.         k++;
  22.      }
  23.     trace(r);
  24. }
  25. 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...

This entry was posted in Math, misc, string manipulation and tagged , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*