Skip to content

Instantly share code, notes, and snippets.

@dsposito
Last active December 23, 2015 18:59
Show Gist options
  • Select an option

  • Save dsposito/6679585 to your computer and use it in GitHub Desktop.

Select an option

Save dsposito/6679585 to your computer and use it in GitHub Desktop.

Revisions

  1. dsposito renamed this gist Nov 26, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. dsposito created this gist Sep 24, 2013.
    59 changes: 59 additions & 0 deletions Numbers
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,59 @@
    <?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));