In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

1~9, 四則演算で2011

去年より高速化。
結果が出るまで1分ほどじっくり待ちましょう。
Get Adobe Flash player
by uwi 02 Jan 2011
    Embed
/**
 * Copyright uwi ( http://wonderfl.net/user/uwi )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/avFm
 */

package {
    import flash.display.Sprite;
    import flash.text.TextField;
    import flash.events.*;

    public class Arithmetic2011 extends Sprite {
        private var _tf : TextField;

        public function Arithmetic2011() {
            Wonderfl.capture_delay(90);
            _tf = new TextField();
            _tf.width = 465;
            _tf.height = 465;
            addChild(_tf);
            
            var i : int, w : int, f : int, lim : int;
            var unit : Array, a : Object, b : Object;
            var dp : Array = new Array(9);
            for(i = 0;i < 9;i++){
                dp[i] = new Array(9);
                dp[i][i] = [{num:i+1, den:1}];
            }
            
            w = 1; f = 0;
            addEventListener(Event.ENTER_FRAME, function(e:Event):void
            {
                lim = 0;
                for(i = f;i <= f + w - 1;i++){
                    lim += dp[f][i].length * dp[i+1][f+w].length;
                }
                var row : Array = [];
                for(i = f;i <= f + w - 1;i++){
                    for each(a in dp[f][i]){
                        for each(b in dp[i+1][f+w]){
                            row.push(add(a, b));
                            row.push(sub(a, b));
                            row.push(mul(a, b));
                            if(b[0] != 0)row.push(div(a, b));
                        }
                    }
                }
                row.sortOn(["num", "den"], [Array.NUMERIC, Array.NUMERIC]);
                unit = [];
                for(i = 0;i < row.length;i++){
                    if(i == 0 || row[i-1].num < row[i].num || row[i-1].den < row[i].den){
                        unit.push(row[i]);
                    }
                }
                dp[f][f+w] = unit;
                tr(f, f+w, unit.length);
                f++;
                if(f + w < 9)return;
                w++;
                f = 0;
                if(w <= 7)return;
                removeEventListener(Event.ENTER_FRAME, arguments.callee);
                addEventListener(Event.ENTER_FRAME, function(e:Event) : void
                {
                    var ct : uint = 0;
                    for each(var str : Array in rec(dp, 2011, 1, 0, 8)){
                        ct++;
                        tr(str[0] + "=2011");
                    } 
                    tr(ct);
                    removeEventListener(Event.ENTER_FRAME, arguments.callee);
                });
            });
        }
        
        public function rec(dp : Array, tnum : int, tden : int, f : int, w : int) : Array
        {
            if(w == 0)return [[(f + 1).toString(), ""]];
            
            var i : int;
            var a : Object, b : Object;
            var se : Object = {};
            var aa : Array, bb : Array;
            var ea : Array, eb : Array;
            var ia : String, ib : String;
            for(i = f;i <= f + w - 1;i++){
                for each(a in dp[f][i]){
                    for each(b in dp[i+1][f+w]){
                        var o : Object;
                        o = add(a, b);
                        if(o.num == tnum && o.den == tden){
                            aa = rec(dp, a.num, a.den, f, i - f);
                            bb = rec(dp, b.num, b.den, i+1, f + w - (i + 1));
                            for each(ea in aa){
                                for each(eb in bb){
                                    se[ea[0] + "+" + eb[0]] = "+";
                                }
                            }
                        }

                        o = sub(a, b);
                        if(o.num == tnum && o.den == tden){
                            aa = rec(dp, a.num, a.den, f, i - f);
                            bb = rec(dp, b.num, b.den, i+1, f + w - (i + 1));
                            for each(ea in aa){
                                for each(eb in bb){
                                    ib = eb[1] == "-" ? "(" + eb[0] + ")" : eb[0];
                                    se[ea[0] + "-" + ib] = "-";
                                }
                            }
                        }

                        o = mul(a, b);
                        if(o.num == tnum && o.den == tden){
                            aa = rec(dp, a.num, a.den, f, i - f);
                            bb = rec(dp, b.num, b.den, i+1, f + w - (i + 1));
                            for each(ea in aa){
                                for each(eb in bb){
                                    ia = ea[1] == "+" || ea[1] == "-" ? "(" + ea[0] + ")" : ea[0];
                                    ib = eb[1] == "+" || eb[1] == "-" ? "(" + eb[0] + ")" : eb[0];
                                    se[ia + "*" + ib] = "*";
                                }
                            }
                        }

                        if(b.num != 0){
                            o = div(a, b); 
                            if(o.num == tnum && o.den == tden){
                                aa = rec(dp, a.num, a.den, f, i - f);
                                bb = rec(dp, b.num, b.den, i+1, f + w - (i + 1));
                                for each(ea in aa){
                                    for each(eb in bb){
                                        ia = ea[1] == "+" || ea[1] == "-" ? "(" + ea[0] + ")" : ea[0];
                                        ib = eb[1] == "+" || eb[1] == "-" || eb[1] == "/" ? "(" + eb[0] + ")" : eb[0];
                                        se[ia + "/" + ib] = "/";
                                    }
                                }
                            }
                        }
                    }
                }
            } 
            
            var ret : Array = [];
            for(var key : String in se){
                ret.push([key, se[key]]);
            }
            return ret;
        }
        
        public function add(a : Object, b : Object) : Object
        {
            var n : int = a.num*b.den+b.num*a.den;
            var d : int = a.den*b.den;
            if(d<0){d = -d; n = -n;}
            var g : int = gcd(n, d);
            return {num:n/g, den:d/g};
        }
        
        public function sub(a : Object, b : Object) : Object
        {
            var n : int = a.num*b.den-b.num*a.den;
            var d : int = a.den*b.den;
            if(d<0){d = -d; n = -n;}
            var g : int = gcd(n, d);
            return {num:n/g, den:d/g};
        }
        
        public function mul(a : Object, b : Object) : Object
        {
            var n : int = a.num*b.num;
            var d : int = a.den*b.den;
            if(d < 0){ d = -d; n = -n;}
            var g : int = gcd(n, d);
            return {num:n/g, den:d/g};
        }

        public function div(a : Object, b : Object) : Object
        {
            var n : int = a.num*b.den;
            var d : int = a.den*b.num;
            if(d < 0){d = -d; n = -n;}
            var g : int = gcd(n, d);
            return {num:n/g, den:d/g};
        }
        
        public function gcd(a : int, b : int) : int
        {
            if(a < 0)a = -a;
            while(b > 0){
                var c : int = a; a = b; b = c % b;
            }
            return a;
        }

        private function tr(...o : Array) : void
        {
            _tf.appendText(o + "\n");
            _tf.scrollV = _tf.maxScrollV;
        }
    }
}