PHP Cheat: number

influence seri kedua, eplorasi basic number di PHP

// number

// generate number range
$x=10; $y=17;
$range = range($x, $y);
$kelipatan = range($x, $y, 3);
print_r($range); // Array ( [0] => 10 [1] => … [7]=> 17 )
print_r($kelipatan); // Array ( [0] => 10 [1] => 13 [2] => 16 )

// membulatkan(rounding) nilai floating (desimal)

$num = (2/3);
//rounding biasa
echo round($num); // 1
// penempatan 1 desimal
echo round($num, 1); // 0.7
// penempatan 3 desimal
echo round($num, 3); // 0.667

// pembulatan atas (ceil), bawah (floor)
$num = (1/3);
echo ceil($num); // 1
echo floor($num); // 0

// Maximal dan minimal
$ary = array(76, 12, 3, 0, 1.5, -0.333);
sort($ary);
echo “Minimum: ” . $ary[0]; // 76
echo “Maximum: ” . $ary[sizeof($ary)-1]; // -0.333

// mencari nilai ganjil-genap
$num = 31;
echo (1&$num) ? “ganjil” : “genap”; // ganjil // cara bitwise
echo ($num % 2) “ganjil” : “genap”; // ganjil // cara modus

// memformat angka dengan koma
$num = 123456789.12345;
echo number_format($num); // 123,456,789
echo number_format($num, 2); // 123,456,789.12
echo number_format($num, 2, ‘, ‘, ‘\”); // 123’456’789,12

// format angka dengan nol didepannya
printf(“%02.2f”, 1234.12345); // 1234.12

// convert antar base (binary,octal, hexadecimal, atau custom)
$num = 100;
echo “Binary : ” . decbin($num); // Binary : 1100100
echo “Octal : ” . decoct($num); // Octal : 144
echo “Hexadecimal : ” . dechex($num); // Hexadecimal : 64
echo “Base6 : ” . base_convert($num, 10, 6); // Base6 : 244
echo “Base5 : ” . base_convert($num, 10, 5); // Base6 : 400

// convert RGB ke nilai hexa
function rgb2hex($r, $g, $b) {
return “#” . dechex($r) . dechex($g) . dechex($b);
}
echo rgb2hex(0, 255, 64); //#00ff40

// convert radian ke degre dan sebaliknya
$deg = 90;
echo deg2rad($deg); //  1.5707963267949
$rad = 1.5707963267949;
echo rad2deg($rad); // 90

// menghitung factorial
$num = 5; $fac = 1;
for ($x=$num; $x>=1; $x–)
$fac *= $x;
echo $fac; // 120

// menghitung logaritma
echo log(6); // 1.7917594692281
echo log10(6); // 0.77815125038364

// trigonometri
$num = 45;
echo sin($num); // 0.85090352453412
echo cos($num); // 0.52532198881773
ech0 tan($num); // 1.6197751905439
echo asin($num); //arcsin: NAN
echo acos($num); //arccos: NAN
echo atan($num); //arctan: 1.5485777614682
echo sinh($num); //  hyperbolic sin : 1.7467135528743E+19
echo cosh($num); //  hyperbolic sin : 1.7467135528743E+19
echo tanh($num); //  hyperbolic sin : 1

// nilai random yang sulit ditebak
echo md5(uniqid(rand(), true)); //5c491b82811fa4f1478319a2f0022bbb

// list nilai prime
function prime($end) {
$sieve =  range(2, $end);
$size = sizeof($sieve);
reset($sieve);
// iterate pada array
while(list($key, $val) =  each($sieve)) {
for($x=$key+1; $x<$size; $x++)
if($sieve[$x] % $val == 0)
unset($sieve[$x]);
}
return $sieve;
}
echo implode(” “, prime(21)); // 2 3 5 7 11 13 17 19

// prime dengan iterasi for
function prime($num) {
for($x=($num-1); $x>1; $x–)
if(($num % $x) == 0)
return false;
return true;
}
echo prime(9) ? “Prime” : “Not”;

Published by

G3n1k

just to remember what i had known :)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s