Euler 14

by enecre forked from Euler 10 (diff: 72)
♥0 | Line 82 | Modified 2010-10-09 20:27:59 | MIT License
play

ActionScript3 source code

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

// forked from enecre's Euler 10
// forked from enecre's Euler 9
// forked from enecre's Euler 8
// forked from enecre's Euler 7
// forked from enecre's Euler 4
// forked from enecre's Euler3
// forked from enecre's forked from: Euler6
// forked from enecre's Euler6
// forked from enecre's Euler2
package {
    import flash.events.Event;
    import flash.text.TextField;
    import flash.display.Sprite;
    import org.libspark.thread.EnterFrameThreadExecutor;
    import org.libspark.thread.Thread;
    public class Euler extends Sprite {
        
        private var tf:TextField = new TextField();
        
        public function Euler() {
            // write as3 code here..
            tf.width = tf.height = 465;
            addChild(tf);
            Thread.initialize(new EnterFrameThreadExecutor());
            init();
        }
        
        private function init():void{
            new MainThread(tf).start();
        }

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

    }
}
import flash.text.TextField;

import org.libspark.thread.Thread;

class MainThread extends Thread{
    private var _tf:TextField;
    
    private var i:int;
    private var currentThread:CollatzThread;
    private var max:Array;
        
    public function MainThread(tf:TextField){
        _tf = tf;
    }
    
    override protected function run():void{
        max = [0,0];
        i = 2;
        next(main);
    }
    
    private function main():void{
        if(currentThread){
            if(currentThread.result > max[1]){
                max[0] = currentThread.num;
                max[1] = currentThread.result;
                tr(max);
            }
        }
        currentThread = new CollatzThread(i);
        currentThread.start();
        currentThread.join();
        if(i < 1000000){
            next(main);
            i++;
        }
        else next(end);
    }
    
    private function end():void{
        tr(max);
    }



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

}

class CollatzThread extends Thread{
    private var i:int;
    public var num:int;
    public var result:int;
    
    public function CollatzThread(start:int){
        i = start;
        num = start;
        result = 0;
    }
    
    protected override function run():void{
        next(main);
    }
    
    private function main():void{
        if(i % 2 == 0)i /= 2;
        else i = i * 3 + 1;
        result++;
        if(i != 1)next(main);
    }
}