Skip to content

Instantly share code, notes, and snippets.

@3Shoka
Forked from ad3n/DeretBilangan.php
Created April 26, 2016 09:24
Show Gist options
  • Save 3Shoka/88ca70b1a4df1a602e85aa3f836f3a0b to your computer and use it in GitHub Desktop.
Save 3Shoka/88ca70b1a4df1a602e85aa3f836f3a0b to your computer and use it in GitHub Desktop.
[PHPID QUIZ] Deret Bilangan
<?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