Category Archives: strings

Adding Very Large Numbers

Actionscript:
  1. // tracing out large numbers will go into exponent notation
  2. trace(1000000000000000000000000);
  3. // outputs: 1e+24
  4.  
  5. var a:String =  "1000000000000000000000009";
  6. var b:String = "12000000000000000000000001";
  7.  
  8. // bigAdd function adds two large numbers
  9. trace(bigAdd(a, b));
  10. // outputs: 13000000000000000000000010
  11.  
  12. // unclear what regular addition is doing here:
  13. trace(int(a) + int(b));
  14. // outputs: -3758096384
  15.  
  16. function bigAdd(a:String, b:String):String{
  17.        var answer:String="";
  18.        var carry:int = 0;
  19.        var zeros:int = 0;
  20.        var i:int = 0;
  21.        var max:int = 0;
  22.        
  23.         // obtain max variable and prepend zeros to the shortest string
  24.        if (a.length> b.length){
  25.           max = a.length;
  26.           zeros = max - b.length;
  27.            for (i= 0; i<zeros; i++) {
  28.                 b = "0" + b;
  29.            }
  30.        }else if (b.length>  a.length){
  31.            max = b.length;
  32.            zeros = max - a.length;
  33.            for (i= 0; i<zeros; i++) {
  34.                 a = "0" + a  ;
  35.             }
  36.        }else{
  37.            // a and b have same number of digets
  38.            max = a.length;  
  39.        }
  40.        
  41.        // add each character starting with the last (max - 1),
  42.        // carry the 1 if the sum has a length of 2
  43.        for (i = max - 1;  i> -1; i--){
  44.                var sum:String = String(int(a.charAt(i)) + int(b.charAt(i)) + carry);
  45.  
  46.                if (sum.length == 2){
  47.                        answer = sum.charAt(1) + answer;
  48.                        carry = 1;
  49.                }else{
  50.                        carry = 0;
  51.                        answer = sum + answer;
  52.                }
  53.        }
  54.        if (carry == 1){
  55.            answer = 1 + answer;
  56.        }
  57.        return answer;
  58. }

This snippet demos a function called bigAdd(), which can be used to add very large numbers that the standard numerical datatypes (int, Number, etc) won't really work with. It uses a string based addition algorithm.

When I decided to write this snippet I dug around for awhile trying to find a nice elegant algorithm to port, something done without prepending zeros etc.... but I couldn't find one that suited my needs... so I just created this one from scratch. There's room for optimization and improvement but it works well enough for just playing around.

BigDecimal & BigInteger

A few months back I needed to do some programming with very large numbers. I dug around for awhile and eventually found out that java has a built in BigInteger andBigDecimal class.... I used java to write the program I had in mind, but thought it would be fun to eventually create something similar for ActionScript....

Also posted in Number, misc, string manipulation | Tagged , , , , | 2 Comments

String.replace()

Actionscript:
  1. var words:String = "ActionSnippet.com is a website. ActionSnippet.com is a blog.";
  2.  
  3. // outputs: "ActionSnippet.com is a website. ActionSnippet.com is a blog.";
  4. trace(words);
  5.  
  6. // the "/g" tells it to replace all instances of ActionSnippet.com
  7. words = words.replace(/ActionSnippet.com/g, "SomeWebsite.com");
  8.  
  9. // outputs: SomeWebsite.com is a website. SomeWebsite.com is a blog.
  10. trace(words);

Just a quick response to a student question.

Also posted in string manipulation | Tagged , , | Leave a comment

Alphabet with Base36 and Binary

Actionscript:
  1. var canvas:BitmapData=new BitmapData(280,100,false,0xefefef);
  2. addChild(new Bitmap(canvas));
  3.  
  4. // pixel alphbet in base36
  5. var alphabet:Array=["", "67erkgi", "e3j6dss", "75rue4u", "c5ltok8", "75s2tji", "75s2tjk", "75rugj2", "95yfnf6", "21blls4", "10nt5xo", "973it1u", "85aef4u", "59lu6nl", "cnz0hbn", "67ej51o", "67eq49c", "67ej53e", "67eq7gy", "66978m4", "6ywdqpw", "95y780c", "53b00as", "8nmdpyi", "5374thm", "53avnus", "6xsfdam"];
  6.  
  7. function drawBase36(num:String, xp:int, yp:int):void {
  8.     // convert base36 to binary
  9.     num=parseInt(num,36).toString(2);
  10.     while (num.length <35) {
  11.         num="0"+num;
  12.     }
  13.     // draw letter
  14.     for (var i:int= 0; i<35; i++) {
  15.         if (num.charAt(i)=="1") {
  16.             canvas.setPixel(i % 5 + xp, int(i / 5) + yp, 0x000000);
  17.         }
  18.     }
  19. }
  20.  
  21. // draw the entire alphabet
  22. for (var i:int = 0; i<alphabet.length; i++) {
  23.     drawBase36(alphabet[i], i * 10,10);
  24. }
  25.  
  26. // draw some words
  27. var words:Array=[0,1,3,20,9,15,14,19,14,9,16,16,5,20,0,9,19,0,3,15,4,5];
  28. for (i = 0; i<words.length; i++) {
  29.     drawBase36(alphabet[words[i]], i * 10,30);
  30. }

I had lots of fun writing this. If you run it in your timeline it will draw this:


... along with the message stored in this array [0,1,3,20,9,15,14,19,14,9,16,16,5,20,0,9,19,0,3,15,4,5].

Binary can be used for representing small images. For every 0 draw white for every 1 draw black. So when the base36 numbers are converted to binary, the 1's and 0's are used to draw each letter.

An interesting side note is that counting in binary to some very high numbers will enumerate all possible two color images at a given size. There are tons of projects related to this... here is one I found with a quick google search: every icon

I also found the wikipedia entry on base36 to be interesting. Apparently base36 can be called hexatridecimal, sexatrigesimal, hexatrigesimal or alphadecimal.

Also posted in BitmapData, pixel manipulation | Tagged , , , , | 2 Comments

10,000 35 ways, .toString(radix);

Actionscript:
  1. for(var i:int = 2; i<37; i++){
  2.     trace((10000).toString(i));
  3. }
  4. /*
  5. outputs:
  6. 10011100010000
  7. 111201101
  8. 2130100
  9. 310000
  10. 114144
  11. 41104
  12. 23420
  13. 14641
  14. 10000
  15. 7571
  16. 5954
  17. 4723
  18. 3904
  19. 2e6a
  20. 2710
  21. 20a4
  22. 1cfa
  23. 18d6
  24. 1500
  25. 11e4
  26. kec
  27. iki
  28. h8g
  29. g00
  30. ekg
  31. dja
  32. cl4
  33. bpo
  34. b3a
  35. aci
  36. 9og
  37. 961
  38. 8m4
  39. 85p
  40. 7ps
  41. */

I had either totally forgotten about or never known about the Number, uint, int etc... .toString() radix argument. Brock mentioned it to me in conversation and I've been having fun with it ever since. It's especially nice for tracing out hex numbers:

Actionscript:
  1. var red:Number = 0xFF0000;
  2. trace("decimal:");
  3. trace(red);
  4. trace("hex:");
  5. trace(red.toString(16));

Also posted in Number | Tagged , , | Leave a comment