Skip to content

Instantly share code, notes, and snippets.

@desarrolla2
Last active December 16, 2015 14:09
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 desarrolla2/5446551 to your computer and use it in GitHub Desktop.
Save desarrolla2/5446551 to your computer and use it in GitHub Desktop.
PrimeNumber.php
<?php
/**
* This file is part of the planetubuntu proyect.
*
* Copyright (c)
* Daniel González <daniel.gonzalez@freelancemadrid.es>
*
* This source file is subject to the MIT license that is bundled
* with this package in the file LICENSE.
*/
/**
*
* Description of PrimeNumbers
*
* @author : Daniel González <daniel.gonzalez@freelancemadrid.es>
* @file : PrimeNumbers.php , UTF-8
* @date : Apr 23, 2013 , 8:31:33 PM
*/
class PrimeNumbers {
/**
*
* @var array
*/
protected $primes = array();
/**
*
* @var int
*/
protected $from = 0;
/**
*
* @var int
*/
protected $to = 0;
/**
*
* @param string $from
* @param type $to
*/
public function calculate($from, $to) {
$this->from = (int) $from;
$this->to = (int) $to;
$this->primes[] = array();
for ($i = $this->from; $i <= $this->to; $i++) {
if (self::isPrime($i)) {
$this->primes[] = $i;
}
}
}
/**
*
* @param type $number
* @return boolean
*/
protected function isPrime($number) {
if ($number == 1) {
return true;
}
for ($i = 2; $i < $number; $i++) {
if ($number % $i == 0) {
return false;
}
}
return true;
}
}
$p = new PrimeNumbers();
print_r($p->calculate(pow(10, 4), pow(10, 4) * 4));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment