Last active
December 23, 2015 18:59
-
-
Save dsposito/6679585 to your computer and use it in GitHub Desktop.
A simple class for interacting with numbers.
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 | |
/** | |
* Contains methods for interacting with numbers. | |
* | |
* @author Daniel Sposito <daniel.g.sposito@gmail.com> | |
*/ | |
class Number | |
{ | |
/** | |
* Builds an array contain $count number of prime numbers. | |
* | |
* @param int $count The total number of prime numbers to return. | |
* | |
* @return array | |
*/ | |
public static function primes($count) | |
{ | |
$number = 1; | |
$total_primes = 0; | |
while ($total_primes < $count) { | |
if (self::isPrime($number)) { | |
$primes[] = $number; | |
$total_primes++; | |
} | |
$number++; | |
} | |
return $primes; | |
} | |
/** | |
* Determines whether or not a number is prime. | |
* | |
* @param int $number The number to check. | |
* | |
* @return boolean | |
*/ | |
public static function isPrime($number) | |
{ | |
if ($number == 1) { | |
return false; | |
} | |
for ($i = 2; $i <= $number; $i++) { | |
if ($number != $i && $number % $i == 0) { | |
return false; | |
} | |
} | |
return true; | |
} | |
} | |
$count = 100; | |
echo "First $count Prime Numbers: " . implode(', ', Number::primes($count)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment