Prime number function with boolean

#1

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).


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;
    }
    
 }
2 Likes
#2
2 Likes
#3

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:

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 that have built-in methods for primes (with some probability it seems):

/** 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 );
   
2 Likes
#4

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.

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
		}

	}
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
2 Likes