Number of primes between two numbers

Number of primes between two numbers

Created:05 Apr 2017 10:53:55 , in  Maths

A script for finding all the primes there are between two given numbers.

Primes class


/* Class: Primes - find whether number is a prime or find all primes between a pair of numbers
   Author : Sylwester Wojnowski
   WWW : wojnowski.net.pl
    
   Methods: 
     public is_prime($a)
       Description:
         check for primality of $a  
       Parameters: 
         $a positive integer
       Return Value:
         bool - true,false
         
    public find_primes($b, $a=1, $inclusive = true)
       Description:
         find primes between $a and $b
       Parameters: 
         $b positive integer
         $a positive integer - optional - default 1
         $inclusive bool - optional - default true
           if false, $a and $b will not be included in the computation
       Return Value:
         array - array of numbers         
*/

class Primes{
  private $primes = array();
   
  public function __construct(){}
  
  # find factors of number a
  private function factors($a){
    $factors = array();
    $n = 1;
    
    if(!is_int($a)){
      return $factors;
    }
    
    while($n <= $a){
      $b = $a / $n;
      
      if(is_int($b)){
        $factors[] = $b;
      }
      
      $n++;
    }
    
    return $factors;
  }
  
  # find out whether $a is a prime
  public function is_prime($a){
    return count($this -> factors($a)) == 2 ? true : false;  
  }
  
  # find primes between numbers b and a
  public function find_primes($b,$a = 1 , $inclusive = true){
     
    $this -> primes = array();     
        
    if(!$inclusive){
      $a++;
      $b--;
    }
    
    if($a < 1 || $b < 1 || $a > $b ){
      return array();
    }
  
    while($a <= $b){
    
      if($this -> is_prime($a)){
        $this -> primes[] = $a;
      }
 
      $a++;
    }
      
    return $this -> primes;
  }
}

Finding your primes using Primes

Finding out if 37 is a prime:


$pr = new Primes();
$pr->is_prime(37);

Finding all the primes up to 13 inclusive:


$pr->find_primes(13);

Finding all the primes between 90 and 120 inclusive:

        
$pr->find_primes(120,90);

Finding all the primes between 3 and 119 inclusive:


$pr->find_primes(120,2,false);

Finding all the primes less than 120:

    
$pr->find_primes(120,0,false);

This post was updated on 06 Oct 2021 21:25:41

Tags:  php 


Author, Copyright and citation

Author

Sylwester Wojnowski

Author of the this article - Sylwester Wojnowski - is a sWWW web developer. He has been writing computer code for the websites and web applications since 1998.

Copyrights

©Copyright, 2024 Sylwester Wojnowski. This article may not be reproduced or published as a whole or in parts without permission from the author. If you share it, please give author credit and do not remove embedded links.

Computer code, if present in the article, is excluded from the above and licensed under GPLv3.

Citation

Cite this article as:

Wojnowski, Sylwester. "Number of primes between two numbers." From sWWW - Code For The Web . https://swww.com.pl//main/index/number-of-primes-between-two-numbers

Add Comment

Allowed BB Code - style tags: [b][/b], [i][/i], [code=text][/code],[code=javascript][/code],[code=php][/code],[code=bash][/code],[code=css][/code],[code=html][/code]


I constent to processing my data given through this form for purposes of a reply by the administrator of this website.

Recent Comments

Nobody has commented on this post yet. Be first!