Skip to content

Instantly share code, notes, and snippets.

@dsposito
Last active December 23, 2015 18:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dsposito/6679585 to your computer and use it in GitHub Desktop.
Save dsposito/6679585 to your computer and use it in GitHub Desktop.
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