パスカルの三角形

by shohei909 forked from 順列・組み合わせ (diff: 292)
ひたすら、パスカルの三角形を書き出す。
* 長時間動かしてると重くなるかもしれない。
♥2 | Line 104 | Modified 2010-08-21 23:19:55 | MIT License
play

ActionScript3 source code

/**
 * Copyright shohei909 ( http://wonderfl.net/user/shohei909 )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/lsA6
 */

// forked from kawamura's 順列・組み合わせ
/*
 * ひたすら、パスカルの三角形を書き出す。
 * 長時間動かしてると重くなるかもしれない。
*/ 

package {
    import com.bit101.components.*;
    import flash.display.MovieClip;
    import flash.display.SimpleButton;
    import flash.events.Event;
    import flash.text.TextField;
    import flash.text.TextFieldAutoSize;
    import flash.text.TextFormat;
    import flash.events.MouseEvent;
    public class FlashTest extends MovieClip {
        private var ta:TextArea;
        private var p:PushButton;
        private var c:Combination = new Combination();
        public function FlashTest():void {
            var tf:TextFormat=new TextFormat();
            tf.size = 24;
            
            ta=new TextArea();
            ta.x=5;
            ta.y=30;
            ta.width=400;
            ta.height=300;
            addChild(ta);
            push()
            
            new PushButton(this, 3, 3, "+", push);
        }
        public function push(e:*=null):void {
            ta.text += c.process();
        }
    }
}
import flash.utils.getTimer;
import flash.utils.ByteArray;


    
class Combination {
    private var triangle:Vector.< Vector.<ByteArray> >;
    private var n:uint = 0;
    private var r:uint = 0;
    private var finish:Boolean = true;
    private var startTime:uint;
    private var limit:uint; 
    public function Combination(limit:int = 1):void {
        this.limit = limit;
        triangle = new Vector.< Vector.<ByteArray> > ();
        triangle[0] = new Vector.<ByteArray>();
        triangle[0][0] = new ByteArray();
        triangle[0][0].writeBoolean(true);
    }
    //計算を進めて、進んだ分の文字列を返す。
    public function process():String {
        var str:String = ""
        startTime = getTimer();
        finish = false;
        var sn:uint = n;
        var sr:uint = r;
        while (true) {
            r ++
            if (n < r) { r=0; n++; triangle[n] = new Vector.<ByteArray>();triangle[n][r] =  triangle[n-1][r] }
            else if (n == r) { triangle[n][r] = triangle[n - 1][r - 1]; }
            else{ triangle[n][r] = add( triangle[n - 1][r - 1] , triangle[n-1][r] ); }
            if ( getTimer() - startTime > limit ) {
                break;
            }
        }
        while (true) {
            if (sr == 0) { str += sn + ":  " }
            str += numString( triangle[sn][sr] ) + "";
            sr++;
            if (sr > sn) { sr = 0; sn++; str += "\n" }
            else { str += "," }
            if ( sn == n && sr == r ) { break; }
        }
        return str;
    }
}

function add( n1:ByteArray, n2:ByteArray ):ByteArray{
    var re:ByteArray = new ByteArray();
    var a:int = 0;
    var l:int = n1.length > n2.length ? n1.length : n2.length;
    for (var i:int = 0; i < l; i++) {
        re[i] = a + int(n1[i]) + int(n2[i]);
        if( re[i] > 9 ){
            re[i] -= 10;
            a = 1;
        }else {
            a = 0;
        }
    }
    if(a>0){re[l]=a}
    return re
}

function clone( n:ByteArray ):ByteArray{
    var re:ByteArray = new ByteArray();
    var l:int = n.length;
    for (var i:int = 0; i < l; i++) {
        re[i] = n[i];
    }
    return re
}

function numString( n:ByteArray ):String{
    var str:String = "";
    var l:int = n.length;
    for (var i:int = 0; i < l; i++) {
        str = n[i]+str;
    }
    return str;
}