Project Euler 109

by uwi
@see http://projecteuler.net/index.php?section=problems&id=109
♥0 | Line 61 | Modified 2009-07-19 22:25:52 | MIT License
play

ActionScript3 source code

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

package {
    import flash.display.Sprite;
    import flash.text.TextField;
    import flash.utils.getTimer;
    // @see http://projecteuler.net/index.php?section=problems&id=109
    public class Euler109 extends Sprite {
        private var _tf : TextField;
  
        public function Euler109() {
            _tf = new TextField();
            _tf.width = 465;
            _tf.height = 465;
            addChild(_tf);
            
            var s : int = getTimer();
            _tf.appendText("" + solve(100) + "\n");
            var g : int = getTimer();
            _tf.appendText("" + (g - s) + " ms\n");
        }
        
        private function solve(N : int) : int
        {
            var ds : Array = [];
            var alls : Array = [];
            
            var i : int, j : int;
            var v : int, sc : int;
            var ct : int = 0;
            for(i = 1;i <= 20;i++){
                ds.push(2 * i);
            }
            ds.push(2 * 25);
            
            for(i = 1;i <= 20;i++){
                alls.push(1 * i);
                alls.push(2 * i);
                alls.push(3 * i);
            }
            alls.push(1 * 25);
            alls.push(2 * 25);
            
            for each(v in ds){
                if(v < N)ct++;
            }
            
            sc = 0;
            for(i = 0;i < alls.length;i++){
                sc += alls[i];
                for each(v in ds){
                    if(sc + v < N)ct++;
                }
                sc -= alls[i];
            }
            
            sc = 0;
            for(i = 0;i < alls.length;i++){
                sc += alls[i];
                for(j = i;j < alls.length;j++){
                    sc += alls[j];
                    for each(v in ds){
                        if(sc + v < N)ct++;
                    }
                    sc -= alls[j];
                }
                sc -= alls[i];
            }
            
            return ct;
        }
    }
}