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

Project Euler 264(unsolved)

@see http://projecteuler.net/index.php?section=problems&id=
Get Adobe Flash player
by uwi 28 Nov 2009
/**
 * Copyright uwi ( http://wonderfl.net/user/uwi )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/8Lef
 */

package {
    import flash.display.Sprite;
    import flash.text.TextField;
    import flash.utils.getTimer;
    import flash.events.*;
    
    // @see http://projecteuler.net/index.php?section=problems&id=
    public class Euler extends Sprite {
        private var _tf : TextField;
  
        public function Euler() {
            _tf = new TextField();
            _tf.width = 465;
            _tf.height = 465;
            addChild(_tf);
            
            var s : int = getTimer();
            solve(100000); 
            var g : int = getTimer();
            tr((g - s) + " ms");
        }

        private var _i : int;
        private var _map : Object;
        private var LIMR : Number;
        private var _s : int;

        private function solve(LIMP : int) : void
        {
            LIMR = LIMP / 4;
            _i = 1;
            _map = {};
            addEventListener(Event.ENTER_FRAME, onEnterFrame);
            _s = getTimer();
        }
        
        private function onEnterFrame(e : Event) : void
        {
            // append
            for(var j : int = 0;j <= _i;j++){
                var v : String = String(_i * _i + j * j);
                if(!_map[v]){
                    _map[v] = 1;
                }else{
                    _map[v]++;
                }
//                if(!_map[v])_map[v] = [];
//                _map[v].push([_i, j]);
            }
            
            // delete
            var l : int = 0;
            /*
            for(var key : String in _map){
                if(Number(key) < _i * _i && _map[key].length < 2){
                    delete _map[key];
                }else{
                    l++;
                }
            }
            */
            
            _i++;
            if(_i % 10 == 0){
                tr(_i, l, getTimer() - _s);
            }
            if(_i == 1000)removeEventListener(Event.ENTER_FRAME, onEnterFrame);
        }
            /*
            var LIMU : Number = LIMR * LIMR;
            var i : int, j : int;
            var k : int, l : int;
            var a : int, b : int, c : int, d : int;
            var commons : Array = [];
            var sq : int;
            var div : Array;
            var sqr : int = Math.sqrt(LIMR);
            
            div = [];
            for(var u : int = 1;u <= sqr;u++){
                for(var v : int = 1 + (u % 2);v < u;v+=2){
                    if(GCD(u, v) == 1){
                        var p : int = u * u + v * v;
                        var q : int = u * u - v * v;
                        var r : int = 2 * u * v;
                        if(q < r){
                            d = q;q = r;r = d;
                        }
                        var lim : int = LIMR / p;
                        for(i = 1;i <= lim;i++){
                            div.push([p * i, q * i, r * i]);
                        }
                    }
                }
            }
            tr(div.length);
            
            var rrmap : Object = {};
            for each(var co : Array in div){
                if(!rrmap[co[0]])rrmap[co[0]] = [];
                rrmap[co[0]].push([co[1], co[2]]);
            }
            
//            tr(commons.join('\n'));
var ct : int = 0;
            for(var vv : String in rrmap){
                if(rrmap[vv].length >= 2){
                    ct++;
                }
            }
            tr(ct);

            var sum : Number = 0;
            for each(var v : Object in commonmap){
                var aaa : Array = [];
                for(var w : String in v){
                    if(v[w] != 0){
                        aaa.push([int(w), v[w]]);
                        aaa.push([int(w), -v[w]]);
                        aaa.push([-int(w), v[w]]);
                        aaa.push([-int(w), -v[w]]);
                        if(v[w] != int(w)){
                            aaa.push([v[w], int(w)]);
                            aaa.push([v[w], -int(w)]);
                            aaa.push([-v[w], int(w)]);
                            aaa.push([-v[w], -int(w)]);
                        }
                    }else{
                        aaa.push([int(w), 0]);
                        aaa.push([-int(w), 0]);
                        aaa.push([0, int(w)]);
                        aaa.push([0, -int(w)]);
                    }
                }
                
                for(i = 0;i < aaa.length;i++){
                    for(j = i + 1;j < aaa.length;j++){
                        for(k = j + 1;k < aaa.length;k++){
                            var xx : int = aaa[i][0] + aaa[j][0] + aaa[k][0];
                            var yy : int = aaa[i][1] + aaa[j][1] + aaa[k][1];
                            if(xx == 5 && yy == 0){
                                var pe : Number = 
                                    Math.sqrt((aaa[i][0] - aaa[j][0]) * (aaa[i][0] - aaa[j][0]) + (aaa[i][1] - aaa[j][1]) * (aaa[i][1] - aaa[j][1])) +
                                    Math.sqrt((aaa[j][0] - aaa[k][0]) * (aaa[j][0] - aaa[k][0]) + (aaa[j][1] - aaa[k][1]) * (aaa[j][1] - aaa[k][1])) +
                                    Math.sqrt((aaa[k][0] - aaa[i][0]) * (aaa[k][0] - aaa[i][0]) + (aaa[k][1] - aaa[i][1]) * (aaa[k][1] - aaa[i][1]));
                                if(pe <= LIMP){
                                    sum += pe;
//                                    tr(pe, aaa[i], aaa[j], aaa[k]);
                                }
                            }
                        }
                    }
                }
            }
            
            var exc : Number = 20 + 5 * Math.sqrt(10) + 15 * Math.sqrt(2);
            if(exc <= LIMP){
                sum += 2 * exc;
            }

            tr(commons.length);
            return sum;
        }
            */
        
        private function GCD(a : int, b : int) : int
        {
            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;
        }
    }
}