Euler 10
forked from Euler 9 (diff: 107)
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/6nVP
*/
// 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 EratosthenesThread(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 EratosthenesThread extends Thread{
private var _tf:TextField;
private var firstItem:List;
private var currentItem:List;
public function EratosthenesThread(tf:TextField){
_tf = tf;
firstItem = new List(2);
var i:int = 3;
for(var li:List = this.firstItem; i <= 2000000; li = li.next){
li.next = new List(i);
i++;
}
}
override protected function run():void{
currentItem = this.firstItem;
tr("Initialization Finished.");
next(main);
}
private function main():void{
var n:int = currentItem.num;
_tf.text = String(n);
removeBy(n);
if(!currentItem.next)next(end);
else {
currentItem = currentItem.next;
next(main);
}
}
private function removeBy(key:int):void{
try{
for (var a:List = currentItem; a.next != null; a = a.next) {
if (a.next.num % key == 0) {
if (a.next.next == null) {
a.next = null;
break;
}
else a.next = a.next.next;
}
else if (!a.next.next) break;
}
}
catch (e:Error) {
tr(e.message, a == null);
}
}
private function end():void{
tr(sum());
}
private function sum():int{
var sum:int = 0;
for (var a:List = firstItem; a != null; a = a.next) {
sum += a.num;
}
return sum;
}
private function tr(...o:Array):void{
_tf.appendText(o + "\n");
_tf.scrollV = _tf.maxScrollV;
}
}
class List{
private var _num:int;
public var next:List;
public function List(val:int){
_num = val;
next = null;
}
public function get num():int{
return _num;
}
}
