-
-
Save 3Shoka/88ca70b1a4df1a602e85aa3f836f3a0b to your computer and use it in GitHub Desktop.
[PHPID QUIZ] Deret Bilangan
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class DeretBilangan | |
{ | |
private function isPrima($number) | |
{ | |
$flag = 0; | |
for ($i = 1; $i <= $number; $i++) { | |
if (0 === $number % $i) { | |
$flag++; | |
} | |
} | |
if (2 === $flag) { | |
return true; | |
} | |
return false; | |
} | |
private function isGenap($number) | |
{ | |
if (0 === $number % 2) { | |
return true; | |
} | |
return false; | |
} | |
public function count($limit) | |
{ | |
$ganjil = array(); | |
$genap = array(); | |
$prima = array(); | |
$numbers = array(); | |
for ($i=1; $i <= $limit; $i++) { | |
$angka = ($i + 1) * ($i - 1); | |
$numbers[] = $angka; | |
if ($this->isGenap($angka)) { | |
$genap[] = $angka; | |
} else { | |
$ganjil[] = $angka; | |
} | |
if ($this->isPrima($angka)) { | |
$prima[] = $angka; | |
} | |
} | |
echo sprintf('Deret (%d): %s', count($numbers), implode(', ', $numbers)); | |
echo PHP_EOL; | |
echo sprintf('Ganjil (%d): %s', count($ganjil), implode(', ', $ganjil)); | |
echo PHP_EOL; | |
echo sprintf('Genap (%d): %s', count($genap), implode(', ', $genap)); | |
echo PHP_EOL; | |
echo sprintf('Prima (%d): %s', count($prima), implode(', ', $prima)); | |
echo PHP_EOL; | |
} | |
} | |
$deret = new DeretBilangan(); | |
$deret->count(100); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment