Contents ::
|
|
package k2;
;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;sub PaiRnd{
my ($n) = @_;
my $r = 0;
for(1..$n){$r++ if(rand() ** 2 + rand() ** 2 <= 1)}
return($r);
}
sub BinomRand{
my ($p, $n) = @_;
my $r = 0;
for(1..$n){$r++ if($p > rand())}
return($r);
}
sub NormalRand{
my ($m, $s, $c) = @_;
my ($r1, $r2, $rr);
$r1 = rand();
$r2 = rand();
if($c % 2){
$rr = $s * sqrt(-2 * log($r1)) * sin(2 * 3.14159 * $r2) + $m;
}else{
$rr = $s * sqrt(-2 * log($r1)) * cos(2 * 3.14159 * $r2) + $m;
}
return($rr);
}
;;;sub HashCode{
my ($s) = @_[0];
my $h = 0;
my $ret = '';
my $ls = length($s);
for(my $i = 0; $i < $ls; $i++){
$h += 37 * ord(substr($s, $i, 1));
}
$ret = PrimeFactor($h, 1);
return($ret);
}
sub isPrime{
my ($v) = @_;
return 0 if ($v < 2);
return 1 if ($v == 2);
return 0 if ($v % 2 == 0);
for(my $i = 3; $i * $i <= $v; $i += 2){
return(0) unless($v % $i);
}
return(1);
}
sub PrimeNear{
my ($v, $f) = @_;
my $expr = 0;
$f = MinMax($f, 1, 2);
$f = ($f == 1) ? -1 : 1;
for(my $i = $v - 1; $i > 0; $i = $i + $f){
if(isPrime($i)){
$expr = $i;
last;
}
}
return($expr);
}
sub PrimeFactor{
my ($v, $f) = @_;
$f = 1 unless($f);
$f = MinMax($f, 1, 2);
$v = eval($v) if($v =~ /[1-9]\d* *(\+|-|\*|\*\*) *[1-9]\d*/);
return('') if($v !~ /^[1-9]\d{0,8}$/);
my ($ret, %result);
my @prime = (2, 3);
for(my $i = 0; $prime[$i] ** 2 <= $v; $i++){
unless($v % $prime[$i]){
$result{$prime[$i]}++;
$v /= $prime[$i];
$i--;
next;
}
if($i == $#prime){
my $p = $prime[-1] + 2;
for(my $j = 1; $prime[$j] ** 2 <= $p; $j++){
unless($p % $prime[$j]){
$p += 2;
$j = 1;
redo;
}
}
push(@prime, $p);
}
}
if($f == 1){
if(%result){
$result{$v}++;
my $expr = join ' x ',
map{$result{$_} == 1 ? "$_" : "$_ ^ $result{$_}"}
sort{$a <=> $b} keys %result;
$ret = $expr;
}else{
}
}else{
$ret = (%result) ? 0 : 1;
}
return($ret);
}
;;;sub Log2{
my ($v, $e) = @_;
if($e){
return(log($v) / log($e));
}else{
return(log($v));
}
}
sub Total{
my (@dat) = @_;
my $tl = 0;
foreach(@dat){$tl += $_}
return($tl);
}
sub Av{
my (@dat) = @_;
return(0)unless(@dat);
my $av = Total(@dat);
$av /= @dat;
return($av);
}
sub Vx{
my (@dat) = @_;
return(Variance(1, @dat));
}
sub Sd{
my (@dat) = @_;
return(Variance(2, @dat));
}
sub Se{
my (@dat) = @_;
return(0)unless(@dat);
return(Sqr(Vx(@dat) / @dat));
}
sub t0{
my ($d1, $d2) = @_;
return(0)unless($d1.$d2);
my $t0 = abs(Av(@$d1) - Av(@$d2)) / Sqr(Se(@$d1) + Se(@$d2));
return($t0);
}
sub Variance{
my ($sw, @dat) = @_;
my $ret = 0;
my $num = @dat;
$sw = 1 unless($sw);
if($num){
my $ave = Av(@dat);
while(@dat){
my $v = shift(@dat);
$ret += Power(($v - $ave), 2);
}
$ret = $ret / $num;
if($sw == 2){$ret = Sqr($ret)}
}
return($ret);
}
sub Sd1{
my (@dat) = @_;
my $sum1 = 0;
my $sum2 = 0;
foreach my $x(@dat){
$sum1 += $x;
$sum2 += Power($x, 2);
}
my $nm = @dat;
my $sd = sqrt( ($sum2 - Power($sum1, 2) / $nm) / ($nm - 1) );
return ($sd);
}
sub Permutation{
my ($n, $r) = @_;
my $p = 1;
for(my $i = 1; $i <= $r; $i++){
$p *= ($n - $i + 1);
}
return($p);
}
sub Combination{
my ($n, $r) = @_;
if($n == $r){
return(1);
}elsif($r == 1){
return($n);
}elsif($n < $r){
return(0);
}else{
return(Combination($n - 1, $r) + Combination($n - 1, $r - 1));
}
}
sub Homogeneous{
my ($n, $r) = @_;
my $h = Combination($r + $n - 1, $r);
return($h);
}
sub Factorial{
my ($n) = @_;
my $p = 1;
for(my $i = 1; $i <= $n; $i++){
$p *= $n;
}
return($p);
}
;;;use constant MTH_PAI_1 => 3.1415926535;
use constant MTH_PAI_2 => MTH_PAI_1 * 2;
sub Pai{
my ($n) = @_;
$n = 1 unless($n);
return(MTH_PAI_1 * $n);
}
sub Rad2Deg{
my ($r) = @_;
$r = 360 * ($r / MTH_PAI_2);
return($r);
}
sub Deg2Rad{
my ($d) = @_;
$d = $d * (MTH_PAI_2 / 360);
return($d);
}
sub Sin{
my ($d) = @_;
my $r = Deg2Rad($d);
$r = sin($r);
return($r);
}
sub Cos{
my ($d) = @_;
my $r = Deg2Rad($d);
$r = cos($r);
return($r);
}
sub Tan{
my ($d) = @_;
my $r = Deg2Rad($d);
$r = sin($r) / cos($r);
return($r);
}
sub Cot{
my ($d) = @_;
my $r = Deg2Rad($d);
$r = ($r == 0) ? '' : 1 / tan($r);
return($r);
}
sub Asin{
my ($d) = @_;
my $r = Deg2Rad($d);
$r = atan2($r, sqrt(1 - $r * $r));
return($r);
}
sub Acos{
my ($d) = @_;
my $r = Deg2Rad($d);
$r = atan2(sqrt(1 - $r * $r), $r);
return($r);
}
sub Atan2{
my ($x, $y) = @_;
my $r = atan2($y, $x);
$r = Rad2Deg($r);
return($r);
}
;;;sub cnv10To2{
my ($v) = @_;
$v = sprintf("%b", $v);
return($v);
}
sub cnv2To10{
my ($v) = @_;
my $v = oct("0b".$v);
return($v);
}
sub cnv10To16{
my ($v) = @_;
$v = sprintf("%x", $v);
return($v);
}
sub cnv16To10{
my ($v) = @_;
$v = hex($v);
return($v);
}
sub rgb{
my($col) = @_;
my(@RGB, $i, $s);
$col = '000000'.$col;
$col = substr($col, length($col) - 6, 6);
for($i = 0; $i < 6; $i += 2){
$s = substr($col, $i, 2);
$RGB[@RGB] = hex($s);
}
return(@RGB);
}
sub color{
my ($R, $G, $B) = @_;
my $col = '';
foreach($R, $G, $B){
$_ = unpack("H2", pack("C", $_));
$col .= "$_";
}
return($col);
}
1;