# Prime number function with boolean

**URL:** <https://discourse.processing.org/t/prime-number-function-with-boolean/11228>\
**Category:** Coding Questions\
**Created:** [May 14, 2019, 3:09pm UTC](https://discourse.processing.org/t/prime-number-function-with-boolean/11228 "2019-05-14T15:09:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![thatone](https://avatars.discourse-cdn.com/v4/letter/t/97f17d/32.png) [@thatone](https://discourse.processing.org/u/thatone)\
**Post date:** [May 14, 2019, 3:09pm UTC](https://discourse.processing.org/t/prime-number-function-with-boolean/11228/1 "2019-05-14T15:09:12Z")

</div>

Hi,

I would really appreciate if someone knows how to do make a function with boolean to check if the numbers 2-50 are prime numbers or not. I’ve looked at example, but I don’t get the hang of it really. Thanks in advance?

(My code so far, but it’s far from ready).

```auto

boolean isPrimeNumber =false;
void setup() {
   size(200, 600);
   fill(0);
   for(int number=2; number < 50; number++){
      if(isPrimeNumber(number)){
         text(number+" is a prime number",15,12*number);
      } else {
       text(number+ "is not a prime number",15,12*number);
            
   }
   
   }
}

boolean isPrimeNumber(int number) {
 for(int i=0; i <sqrt(number) ; i++){
    if(number%i ==0){
       return isPrimenumber== false;
    }
    
 }

```

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [May 14, 2019, 3:21pm UTC](https://discourse.processing.org/t/prime-number-function-with-boolean/11228/2 "2019-05-14T15:21:41Z")

</div>

- [Forum.Processing.org/two/discussions/tagged/prime](http://Forum.Processing.org/two/discussions/tagged/prime)

---

<div class="post-metadata">

**Author:** ![raron](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/raron/32/13651_2.png) [@raron](https://discourse.processing.org/u/raron)\
**Post date:** [May 14, 2019, 10:56pm UTC](https://discourse.processing.org/t/prime-number-function-with-boolean/11228/3 "2019-05-14T22:56:05Z")

</div>

Interesting approach, but I from testing just now it seems testing factorials up to sqrt(number) isn’t enough.

I took the liberty of changing it a bit:

```auto
void setup() {
   size(200, 600);
   fill(0);
   for(int number=2; number < 50; number++){
     if(isPrimeNumber(number)){
       fill(200,0,0);
       text(number+" is a prime number",15,12*number);
     } else {
       fill(0);
       text(number+ " is not a prime number",15,12*number);
    }
  }
}

boolean isPrimeNumber(int number) {
  boolean test = true;
  for(int i=2; i<number; i++){
    if(number%i == 0){
       test = false;
       break; // skips rest of loop
    }
  }
  return test;
}

```

There’s also a Java library [BigIntegers](http://www.tutorialspoint.com/java/math/java_math_biginteger.htm) that have built-in methods for primes (with some probability it seems):

```auto
/** Test of really big integers
http://www.tutorialspoint.com/java/math/java_math_biginteger.htm

Description
The java.math.BigInteger.nextProbablePrime() returns the first integer greater than this BigInteger that is probably prime.
The probability that the number returned by this method is composite does not exceed 2^-100.

This method will never skip over a prime when searching: if it returns p, there is no prime q such that this < q < p.

Declaration
Following is the declaration for java.math.BigInteger.nextProbablePrime() method

public BigInteger nextProbablePrime()

Parameters
NA

Return Value
This method returns the first integer greater than this BigInteger that is probably prime.

Exception
ArithmeticException - if this < 0

Example
The following example shows the usage of math.BigInteger.nextProbablePrime() method
*/

//import java.math.*;
import java.math.BigInteger;
//import java.util.*;

  
  // create 2 BigInteger objects
  BigInteger bi1, bi2;
  
  bi1 = new BigInteger("20148832325468531");
  
  // assign nextProbablePrime value of bi1 to bi2
  bi2 = bi1.nextProbablePrime();
  
  String str = "Next probable prime after " + bi1 +" is " +bi2;
  println( str );
   

```

---

<div class="post-metadata">

**Author:** ![InferNova](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/infernova/32/6009_2.png) [@InferNova](https://discourse.processing.org/u/InferNova)\
**Post date:** [May 15, 2019, 12:51am UTC](https://discourse.processing.org/t/prime-number-function-with-boolean/11228/4 "2019-05-15T00:51:53Z")

</div>

Hey there! From what it seems your asking is that you want to generate the prime numbers between a start index and an end index ( 2 , 50 ) so here’s a little bit of code how to do so.

```auto
void isPrimeNumbers(int startNumber , int endNumber){
	boolean prime = false;
		for(int i = startNumber ; i<=endNumber ; i++){
			prime = true;
			for(int j = 2 ; j*j<=i;j++){
				if( i%j == 0 ) { prime = false; break; }   
			}
			if(prime) System.out.println( i );//Prints out the prime number
		}

	}

```

```auto
I made it that it prints out the numbers do whatever you need with them!
if your interested in a faster implementation of finding prime numbers, here's a good article.

https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
```
