| 1 |
/* |
|---|
| 2 |
* Copyright 2007 Michiyasu Wada |
|---|
| 3 |
* |
|---|
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
|---|
| 5 |
* you may not use this file except in compliance with the License. |
|---|
| 6 |
* You may obtain a copy of the License at |
|---|
| 7 |
* |
|---|
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
|---|
| 9 |
* |
|---|
| 10 |
* Unless required by applicable law or agreed to in writing, software |
|---|
| 11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
|---|
| 12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|---|
| 13 |
* See the License for the specific language governing permissions and |
|---|
| 14 |
* limitations under the License. |
|---|
| 15 |
*/ |
|---|
| 16 |
|
|---|
| 17 |
import com.seyself.core.data.PrimeNumber; |
|---|
| 18 |
|
|---|
| 19 |
/** |
|---|
| 20 |
* com.seyself.math.Math2 |
|---|
| 21 |
* |
|---|
| 22 |
* 数値の計算もろもろ。 |
|---|
| 23 |
* static関数のみで構成されています。 |
|---|
| 24 |
* |
|---|
| 25 |
* @author Michiyasu Wada |
|---|
| 26 |
*/ |
|---|
| 27 |
class com.seyself.math.Math2 |
|---|
| 28 |
{ |
|---|
| 29 |
|
|---|
| 30 |
private function Math2(){} |
|---|
| 31 |
|
|---|
| 32 |
/** |
|---|
| 33 |
* 渡された数値が、指定された桁数に満たない場合、数値の先頭に"0"を付けて桁数を揃えます。 |
|---|
| 34 |
* 引数に渡す値はどちらも整数値であり、正数でなければ正しい値は得られません。 |
|---|
| 35 |
* |
|---|
| 36 |
* @param n : Number - 成形元となる整数値 |
|---|
| 37 |
* @param leng : Number - 桁数 |
|---|
| 38 |
* @return String - 成形された数値 |
|---|
| 39 |
* @usage <code> |
|---|
| 40 |
* import com.seyself.math.Math2; |
|---|
| 41 |
* trace( Math2.figure( 32 , 4 ) ); // 出力 : 0032 |
|---|
| 42 |
* </code> |
|---|
| 43 |
*/ |
|---|
| 44 |
public static function figure( n:Number , leng:Number ):String |
|---|
| 45 |
{ |
|---|
| 46 |
var _n = n+""; |
|---|
| 47 |
if(_n.length<leng) return (""+Math.pow( 10 , leng-_n.length )).substr(1,leng-_n.length)+_n; |
|---|
| 48 |
return _n.substr( _n.length-leng , leng ); |
|---|
| 49 |
} |
|---|
| 50 |
|
|---|
| 51 |
/** |
|---|
| 52 |
* +1か-1をランダムで返します |
|---|
| 53 |
* |
|---|
| 54 |
* @return Number - 1 もしくは -1 を返します |
|---|
| 55 |
* @usage <code> |
|---|
| 56 |
* import com.seyself.math.Math2; |
|---|
| 57 |
* trace( Math2.random() ); // 出力 : -1 |
|---|
| 58 |
* </code> |
|---|
| 59 |
*/ |
|---|
| 60 |
public static function random():Number |
|---|
| 61 |
{ |
|---|
| 62 |
return (Math.random()<0.5) ? -1 : 1; |
|---|
| 63 |
} |
|---|
| 64 |
|
|---|
| 65 |
/** |
|---|
| 66 |
* 最小値以上、最大値以下の値を返します。 |
|---|
| 67 |
* 指定した数値が最小値を下回る場合は最小値が返され、最大値より大きい場合は最大値を返します。 |
|---|
| 68 |
* 数値が最小値以上、最大値以下の場合は、数値がそのまま返されます。 |
|---|
| 69 |
* |
|---|
| 70 |
* @param param : Number - 数値 |
|---|
| 71 |
* @param min : Number - 最小値 |
|---|
| 72 |
* @param max : Number - 最大値 |
|---|
| 73 |
* @return Number - 最小値と最大値の間で該当する値を返します |
|---|
| 74 |
* @usage <code> |
|---|
| 75 |
* import com.seyself.math.Math2; |
|---|
| 76 |
* trace( Math2.relation( 256 , 0 , 100 ) ); // 出力 : 100 |
|---|
| 77 |
* </code> |
|---|
| 78 |
*/ |
|---|
| 79 |
public static function relation( param:Number , min:Number , max:Number ) |
|---|
| 80 |
{ |
|---|
| 81 |
return Math.max( Math.min( param , max ) , min ); |
|---|
| 82 |
} |
|---|
| 83 |
|
|---|
| 84 |
/** |
|---|
| 85 |
* 渡された引数の合計値を返します。 |
|---|
| 86 |
* 配列を使用する場合はapplyを使用してください。 |
|---|
| 87 |
* |
|---|
| 88 |
* @param arguments : Number - 数値 |
|---|
| 89 |
* @return Number - 引数の合計値 |
|---|
| 90 |
* @usage <code> |
|---|
| 91 |
* import com.seyself.math.Math2; |
|---|
| 92 |
* trace( Math2.sum( 6, 10, 200, 40 ) ); // 出力 : 256 |
|---|
| 93 |
* </code> |
|---|
| 94 |
*/ |
|---|
| 95 |
public static function sum():Number |
|---|
| 96 |
{ |
|---|
| 97 |
var a = 0; |
|---|
| 98 |
for(var i=0;i<arguments.length;i++) a+=arguments[i]; |
|---|
| 99 |
return a; |
|---|
| 100 |
} |
|---|
| 101 |
|
|---|
| 102 |
/** |
|---|
| 103 |
* 渡された引数の平均値を返します。 |
|---|
| 104 |
* 配列を使用する場合はapplyを使用してください。 |
|---|
| 105 |
* |
|---|
| 106 |
* @param arguments : Number - 数値 |
|---|
| 107 |
* @return Number - 引数の平均値 |
|---|
| 108 |
* @usage <code> |
|---|
| 109 |
* import com.seyself.math.Math2; |
|---|
| 110 |
* trace( Math2.average( 6, 10, 200, 40 ) ); // 出力 : 64 |
|---|
| 111 |
* </code> |
|---|
| 112 |
*/ |
|---|
| 113 |
public static function average():Number |
|---|
| 114 |
{ |
|---|
| 115 |
return Math2.sum.apply(null,arguments)/arguments.length; |
|---|
| 116 |
} |
|---|
| 117 |
|
|---|
| 118 |
/** |
|---|
| 119 |
* 渡された正数の値から正と負の値を交互に変換した値を返します。 |
|---|
| 120 |
* |
|---|
| 121 |
* @param n : Number - 正数の値 |
|---|
| 122 |
* @return Number - 引数が奇数なら負数、偶数なら正数が返ります |
|---|
| 123 |
* @usage <code> |
|---|
| 124 |
* import com.seyself.math.Math2; |
|---|
| 125 |
* for(var i=0;i<6;i++) trace( i+" => "+Math2.xcount( i ) ); |
|---|
| 126 |
* |
|---|
| 127 |
* 出力 --------- |
|---|
| 128 |
* 0 => 0 |
|---|
| 129 |
* 1 => 1 |
|---|
| 130 |
* 2 => -1 |
|---|
| 131 |
* 3 => 2 |
|---|
| 132 |
* 4 => -2 |
|---|
| 133 |
* 5 => 3 |
|---|
| 134 |
* </code> |
|---|
| 135 |
*/ |
|---|
| 136 |
public static function xcount( n:Number ):Number |
|---|
| 137 |
{ |
|---|
| 138 |
var a = Math.floor(n/2); |
|---|
| 139 |
if(n%2>0) a = -(a+1); |
|---|
| 140 |
a *= -1; |
|---|
| 141 |
return a; |
|---|
| 142 |
} |
|---|
| 143 |
|
|---|
| 144 |
/** |
|---|
| 145 |
* 小数点以下 指定桁で四捨五入します。 |
|---|
| 146 |
* |
|---|
| 147 |
* @param param : Number - 数値 |
|---|
| 148 |
* @param leng : Number - 小数点以下の桁数 |
|---|
| 149 |
* @return Number - 小数点以下 指定桁で四捨五入した値 |
|---|
| 150 |
* @usage <code> |
|---|
| 151 |
* import com.seyself.math.Math2; |
|---|
| 152 |
* trace( Math2.round( Math.PI , 3 ) ); // 出力 : 3.142 |
|---|
| 153 |
* </code> |
|---|
| 154 |
*/ |
|---|
| 155 |
public static function round( param:Number , leng:Number ):Number |
|---|
| 156 |
{ |
|---|
| 157 |
var _mgn = 1; |
|---|
| 158 |
for(var i=0;i<leng;i++) _mgn*=10; |
|---|
| 159 |
return Math.round(param*_mgn)/_mgn; |
|---|
| 160 |
} |
|---|
| 161 |
|
|---|
| 162 |
/** |
|---|
| 163 |
* 小数点以下 指定桁数で切り捨てた値を返します。 |
|---|
| 164 |
* |
|---|
| 165 |
* @param param : Number - 数値 |
|---|
| 166 |
* @param leng : Number - 小数点以下の桁数 |
|---|
| 167 |
* @return Number - 数点以下 指定桁数で切り捨てた値 |
|---|
| 168 |
* @usage <code> |
|---|
| 169 |
* import com.seyself.math.Math2; |
|---|
| 170 |
* trace( Math2.floor( Math.PI , 3 ) ); // 出力 : 3.141 |
|---|
| 171 |
* </code> |
|---|
| 172 |
*/ |
|---|
| 173 |
public static function floor( param:Number , leng:Number ):Number |
|---|
| 174 |
{ |
|---|
| 175 |
var _mgn = 1; |
|---|
| 176 |
for(var i=0;i<leng;i++) _mgn*=10; |
|---|
| 177 |
return Math.floor(param*_mgn)/_mgn; |
|---|
| 178 |
} |
|---|
| 179 |
|
|---|
| 180 |
/** |
|---|
| 181 |
* 0 から指定した最大値未満の値を返します。 |
|---|
| 182 |
* 値が最大値を上回る場合は、上回った分 0 からカウントします。 |
|---|
| 183 |
* 値が 0 より下回る場合も、同様に最大値からカウントします。 |
|---|
| 184 |
* |
|---|
| 185 |
* @param param : Number - 数値 |
|---|
| 186 |
* @param max : Number - 最大値 |
|---|
| 187 |
* @return Number - 0 から最大値未満までの値 |
|---|
| 188 |
* @usage <code> |
|---|
| 189 |
* import com.seyself.math.Math2; |
|---|
| 190 |
* for(var i=1;i<85;i*=3) trace( i+" => "+Math2.loop( i , 7 ) ); |
|---|
| 191 |
* |
|---|
| 192 |
* 出力 --------- |
|---|
| 193 |
* 1 => 1 |
|---|
| 194 |
* 3 => 3 |
|---|
| 195 |
* 9 => 2 |
|---|
| 196 |
* 27 => 6 |
|---|
| 197 |
* 81 => 4 |
|---|
| 198 |
* </code> |
|---|
| 199 |
*/ |
|---|
| 200 |
public static function loop( param:Number , max:Number ):Number |
|---|
| 201 |
{ |
|---|
| 202 |
if(param<0) return (max+(param%max))%max; |
|---|
| 203 |
else if(param>=max) return param%max; |
|---|
| 204 |
else return param; |
|---|
| 205 |
} |
|---|
| 206 |
|
|---|
| 207 |
/** |
|---|
| 208 |
* 引数の値が正数の場合のみ、その数値を返し、負数の場合は0を返します。 |
|---|
| 209 |
* |
|---|
| 210 |
* @param n : Number - 数値 |
|---|
| 211 |
* @return Number - 正数(0以上の数値) |
|---|
| 212 |
* @usage <code> |
|---|
| 213 |
* import com.seyself.math.Math2; |
|---|
| 214 |
* trace( Math2.positive( 20 ) ); // 出力 : 20 |
|---|
| 215 |
* trace( Math2.positive( -20 ) ); // 出力 : 0 |
|---|
| 216 |
* </code> |
|---|
| 217 |
*/ |
|---|
| 218 |
public static function positive( n:Number ):Number |
|---|
| 219 |
{ |
|---|
| 220 |
if(n>0) return n; |
|---|
| 221 |
return 0; |
|---|
| 222 |
} |
|---|
| 223 |
|
|---|
| 224 |
/** |
|---|
| 225 |
* 引数の値が負数の場合のみ数値を返し、正数の場合は0を返します。 |
|---|
| 226 |
* |
|---|
| 227 |
* @param n : Number - 数値 |
|---|
| 228 |
* @return Number - 負数(0以下の数値) |
|---|
| 229 |
* @usage <code> |
|---|
| 230 |
* import com.seyself.math.Math2; |
|---|
| 231 |
* trace( Math2.negative( 20 ) ); // 出力 : 0 |
|---|
| 232 |
* trace( Math2.negative( -20 ) ); // 出力 : -20 |
|---|
| 233 |
* </code> |
|---|
| 234 |
*/ |
|---|
| 235 |
public static function negative( n:Number ):Number |
|---|
| 236 |
{ |
|---|
| 237 |
if(n<0) return n; |
|---|
| 238 |
return 0; |
|---|
| 239 |
} |
|---|
| 240 |
|
|---|
| 241 |
/** |
|---|
| 242 |
* 引数が正数の場合は+1を、負数の場合は-1、その他の場合は0を返します。 |
|---|
| 243 |
* |
|---|
| 244 |
* @param n : Number - 数値 |
|---|
| 245 |
* @return Number - 1 もしくは -1 、0 |
|---|
| 246 |
* @usage <code> |
|---|
| 247 |
* import com.seyself.math.Math2; |
|---|
| 248 |
* trace( Math2.filter( 5 ) ); // 出力 : 1 |
|---|
| 249 |
* trace( Math2.filter( -5 ) ); // 出力 : -1 |
|---|
| 250 |
* trace( Math2.filter( 0 ) ); // 出力 : 0 |
|---|
| 251 |
* </code> |
|---|
| 252 |
*/ |
|---|
| 253 |
public static function filter( n:Number ):Number |
|---|
| 254 |
{ |
|---|
| 255 |
if(n>0) return 1; |
|---|
| 256 |
if(n<0) return -1; |
|---|
| 257 |
return 0; |
|---|
| 258 |
} |
|---|
| 259 |
|
|---|
| 260 |
/** |
|---|
| 261 |
* 引数から約数を求めて配列で返します。 |
|---|
| 262 |
* |
|---|
| 263 |
* @param n : Number - 数値 |
|---|
| 264 |
* @return Array - 約数の配列 |
|---|
| 265 |
* @usage <code> |
|---|
| 266 |
* import com.seyself.math.Math2; |
|---|
| 267 |
* trace( Math2.measure( 30 ) ); // 出力 : 1,2,3,5,6,10,15,30 |
|---|
| 268 |
* </code> |
|---|
| 269 |
*/ |
|---|
| 270 |
public static function measure( param:Number ):Array |
|---|
| 271 |
{ |
|---|
| 272 |
var m = param; |
|---|
| 273 |
var res = [1,param]; |
|---|
| 274 |
var maxcount = Math.floor(Math.sqrt(m)+1); |
|---|
| 275 |
for(var i=2;i<maxcount ;i++){ |
|---|
| 276 |
if(param%i==0){ |
|---|
| 277 |
var s = 0; |
|---|
| 278 |
var e = 0; |
|---|
| 279 |
for(var k=0;k<res.length;k++){ |
|---|
| 280 |
if( res[k] ==i ) s++; |
|---|
| 281 |
if( res[k] ==param/i ) e++; |
|---|
| 282 |
} |
|---|
| 283 |
if(s==0) res.push(i); |
|---|
| 284 |
if(param/i!=i && e==0) res.push(param/i); |
|---|
| 285 |
} |
|---|
| 286 |
} |
|---|
| 287 |
return res.sort( Array.NUMERIC ); |
|---|
| 288 |
} |
|---|
| 289 |
|
|---|
| 290 |
/** |
|---|
| 291 |
* 素数の一覧を取得します。 |
|---|
| 292 |
* 取得できる素数は 2 から 9973 までの 1229 個の数値です。 |
|---|
| 293 |
* 引数 param が無い場合は配列で全ての素数を返します。 |
|---|
| 294 |
* |
|---|
| 295 |
* @param param : Number - 何番目の素数を取得するかを指定する数値 |
|---|
| 296 |
* @return Number or Array - 指定した位置の素数、もしくは素数の配列 |
|---|
| 297 |
* @usage <code> |
|---|
| 298 |
* import com.seyself.math.Math2; |
|---|
| 299 |
* trace( Math2.prime( 24 ) ); // 出力 : 97 |
|---|
| 300 |
* </code> |
|---|
| 301 |
*/ |
|---|
| 302 |
public static function prime( param:Number ) |
|---|
| 303 |
{ |
|---|
| 304 |
var _prime:Array = PrimeNumber.getMembers(); |
|---|
| 305 |
if(param==undefined) return _prime; |
|---|
| 306 |
return _prime[param]; |
|---|
| 307 |
} |
|---|
| 308 |
|
|---|
| 309 |
/** |
|---|
| 310 |
* 渡されたラジアン値を角度に変換します。 |
|---|
| 311 |
* |
|---|
| 312 |
* @param param : Number - ラジアン |
|---|
| 313 |
* @return Number - 角度 |
|---|
| 314 |
* @usage <code> |
|---|
| 315 |
* import com.seyself.math.Math2; |
|---|
| 316 |
* trace( Math2.toDeg( Math.PI/2 ) ); // 出力 : 90 |
|---|
| 317 |
* </code> |
|---|
| 318 |
*/ |
|---|
| 319 |
public static function toDeg( rad:Number ):Number |
|---|
| 320 |
{ |
|---|
| 321 |
return rad/Math.PI*180; |
|---|
| 322 |
} |
|---|
| 323 |
|
|---|
| 324 |
/** |
|---|
| 325 |
* 渡された角度をラジアン値に変換します。 |
|---|
| 326 |
* |
|---|
| 327 |
* @param param : Number - 角度 |
|---|
| 328 |
* @return Number - ラジアン |
|---|
| 329 |
* @usage <code> |
|---|
| 330 |
* import com.seyself.math.Math2; |
|---|
| 331 |
* trace( Math2.toRad( 45 ) ); // 出力 : 0.785398163397448 |
|---|
| 332 |
* </code> |
|---|
| 333 |
*/ |
|---|
| 334 |
public static function toRad( deg:Number ):Number |
|---|
| 335 |
{ |
|---|
| 336 |
return deg/180*Math.PI; |
|---|
| 337 |
} |
|---|
| 338 |
|
|---|
| 339 |
} |
|---|