Project Euler 自分用

by shohei909
中身のコードはころころ変わります。
現在 275
♥0 | Line 65 | Modified 2010-08-17 13:54:46 | MIT License
play

ActionScript3 source code

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

//中身のコードはころころ変わります。
//現在 275
package  {
import flash.display.Sprite;
    import com.bit101.components.TextArea;    
    public class FlashTest extends Sprite {
        public function FlashTest() {
			
            text = new TextArea(this, 0, 0, "");
            text.text = answer();
        }
    }
}
import com.bit101.components.TextArea; 
var text:TextArea;
var ans:Number = 0;
var val:int = 5;
function answer():String {
	for (var i:uint = 1; i <= val; i++ ) {
		for ( var j:uint = 0; j <= (val-i)/2 ; j++ ) {
			var m:uint = val - i - j;
			ans += by( massArray(i,j), massArray(i,m) )
		}
	}
	return massArray(4,4).toString()
}

//配列の重さを返す
function mass(arr:Array):uint { 
	var l:int = arr.length;
	var g:int = 0
	for (var i:int = 0; i < l;i++ ) {
		g += arr[i] * i;
	}
	return g;
}

//重さの配列を返す
function massArray(center:uint,m:int):Array { 
	var gArr:Array = []
	for (var j:int = 0; j <= (m * m + 1) / 2; j++) {
		gArr[j] = 0;
	}
	var arr:Array = [center,m]
	while(true) {
		gArr[ mass(arr) ]++;
		for (var i:int = 0; i < arr.length; i++ ) {
			if ( arr[i] > 1 ) { 
				 if ( arr[i+1] == null ) {
					 arr[i] --; 
					 arr[i + 1]=1;
					 break;
				 }else if ( arr[i + 1] == 1 ) {
					 arr[i] --; 
					 arr[i + 1]++;
					 break;
				 }
			}
		}
		if (i == arr.length) { break; }
	}
	return gArr;
}

//二つの配列を掛け合わせた数字を返します
function by(arr1:Array, arr2:Array):uint { 
	var a:uint = 0
	var l:int = arr1.length
	if(l>arr2.length){l=arr2.length}
	for ( var i:int=0; i < l; i++  ) {
		a += arr1[0] * arr2[0];
	}
	return a;
}