Skip to content

Instantly share code, notes, and snippets.

@dsposito dsposito/Numbers.php
Last active Dec 23, 2015

Embed
What would you like to do?
A simple class for interacting with numbers.
<?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
You can’t perform that action at this time.