# Check if variable is a prime

**URL:** <https://discourse.processing.org/t/check-if-variable-is-a-prime/31216>\
**Category:** Coding Questions\
**Created:** [July 12, 2021, 9:24pm UTC](https://discourse.processing.org/t/check-if-variable-is-a-prime/31216 "2021-07-12T21:24:18Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![MoonAlien822](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@MoonAlien822](https://discourse.processing.org/u/MoonAlien822)\
**Post date:** [July 12, 2021, 9:24pm UTC](https://discourse.processing.org/t/check-if-variable-is-a-prime/31216/1 "2021-07-12T21:24:18Z")

</div>

how to check if x is a prime?

---

<div class="post-metadata">

**Author:** ![Flolo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/flolo/32/13874_2.png) [@Flolo](https://discourse.processing.org/u/Flolo)\
**Post date:** [July 12, 2021, 9:27pm UTC](https://discourse.processing.org/t/check-if-variable-is-a-prime/31216/2 "2021-07-12T21:27:22Z")

</div>

Hey I think this is what you are looking for:

```auto
boolean isPrimeNumber(int number) {
        
        //number should be greater than 1
        if(number < 2)
            return false;
        
        for(int i = 2; i < number / 2; i++){
            
            //if number is perfectly divisible, it is not prime 
            if(number % i == 0)
                return false;
        }
        
        return true;
    }

```

Code from: [Check if a number is prime number in Java Example - Java Code Examples](https://www.javacodeexamples.com/check-if-a-number-is-prime-number-in-java-example/473)

---

<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:** [July 12, 2021, 10:48pm UTC](https://discourse.processing.org/t/check-if-variable-is-a-prime/31216/3 "2021-07-12T22:48:04Z")

</div>

[forum.Processing.org/two/discussion/7526/generating-large-prime-numbers#Item\_14](http://forum.Processing.org/two/discussion/7526/generating-large-prime-numbers#Item_14)

---

<div class="post-metadata">

**Author:** ![MoonAlien822](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@MoonAlien822](https://discourse.processing.org/u/MoonAlien822)\
**Post date:** [July 13, 2021, 6:49am UTC](https://discourse.processing.org/t/check-if-variable-is-a-prime/31216/4 "2021-07-13T06:49:38Z")

</div>

thank you, but now its returning this error : isPrimeNumber cannot be resolved to a variable  
here’s my code:

```auto

int x=(int(random(10-1)));
float y= 10-x;
float devider;
boolean isPrimeNumber(int number){//number should be greater than 1
        if(number < 2)
            return false;
        
        for(int i = 2; i < number / 2; i++){
            
            //if number is perfectly divisible, it is not prime 
            if(number % i == 0)
                return false;
        }
        
        return true;
    }

void setup(){
  print(x);
       if (isPrimeNumber=true){x-=x-1;}
     if(x==10/2){x=x+1;}
     print(x);
   size(10,10);
   if(x/3==(int(x/3))){devider=3;}
   
   if(x/2==(int(x/2))){devider=2;}

   
 }
   
   void draw(){
      background(255,255,255);
      fill(0,0,0);
      
      if(x==0){x=(int(random(10-1)));}
      
      if(x>y){y=y+(int(y/devider));}
      
      if(y>x){x=x+(int(x/devider));}
      
      ellipse(width-x, height/2, 1,1);
      print(x);
      
   }

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [July 13, 2021, 7:29am UTC](https://discourse.processing.org/t/check-if-variable-is-a-prime/31216/5 "2021-07-13T07:29:25Z")

</div>

> [@MoonAlien822](#):
>
> `if (isPrimeNumber=true)`

You need to pass a variable to the function with (x)

Also use == not single = (which is for assignment not comparison)

---

<div class="post-metadata">

**Author:** ![MoonAlien822](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@MoonAlien822](https://discourse.processing.org/u/MoonAlien822)\
**Post date:** [July 13, 2021, 7:32am UTC](https://discourse.processing.org/t/check-if-variable-is-a-prime/31216/6 "2021-07-13T07:32:05Z")

</div>

> [@Chrisir](#):
>
> You need to pass a variable to the function with (x)

what do you mean by that and how do i do it?

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [July 13, 2021, 7:40am UTC](https://discourse.processing.org/t/check-if-variable-is-a-prime/31216/7 "2021-07-13T07:40:02Z")

</div>

> [@MoonAlien822](#):
>
> `if (isPrimeNumber=true){x-=x-1;}`

Just after isPrimeNumber write (x) ==

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [July 13, 2021, 7:43am UTC](https://discourse.processing.org/t/check-if-variable-is-a-prime/31216/8 "2021-07-13T07:43:12Z")

</div>

> [@MoonAlien822](#):
>
> x-=x-1;}

Not sure you really mean this since it would give always 1

Try x = x-1;

---

<div class="post-metadata">

**Author:** ![MoonAlien822](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@MoonAlien822](https://discourse.processing.org/u/MoonAlien822)\
**Post date:** [July 13, 2021, 8:41am UTC](https://discourse.processing.org/t/check-if-variable-is-a-prime/31216/9 "2021-07-13T08:41:05Z")

</div>

> [@MoonAlien822](#):
>
> ```auto
> for(int i = 2; i < number / 2; i++){
>             
> //if number is perfectly divisible, it is not prime 
> if(number % i == 0)
> return false;
> }
>         
> return true;
> }
> 
> ```

this is a for loop, can a for loop work outside of draw?

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [July 13, 2021, 8:48am UTC](https://discourse.processing.org/t/check-if-variable-is-a-prime/31216/10 "2021-07-13T08:48:56Z")

</div>

Outside of a function such is not allowed

It sits in its own function that is allowed

You call this function from draw with the parameters that are declared in the definition of the function

---

<div class="post-metadata">

**Author:** ![MoonAlien822](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@MoonAlien822](https://discourse.processing.org/u/MoonAlien822)\
**Post date:** [July 13, 2021, 8:49am UTC](https://discourse.processing.org/t/check-if-variable-is-a-prime/31216/11 "2021-07-13T08:49:42Z")

</div>

so it only checks in the beginning?

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [July 13, 2021, 8:52am UTC](https://discourse.processing.org/t/check-if-variable-is-a-prime/31216/12 "2021-07-13T08:52:31Z")

</div>

> [@MoonAlien822](#):
>
> `isPrimeNumber`

The for loop is used in this function

draw() is also a function

You can declare as many as you want

You can also move your function after draw at the end of the code

It is run when you call the function, not automatically

Other functions such as mousePressed or keyReleased are known to processing and called automatically

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [July 13, 2021, 9:50am UTC](https://discourse.processing.org/t/check-if-variable-is-a-prime/31216/13 "2021-07-13T09:50:46Z")

</div>

Some tips to speed up checking of primes:

- implement a +2 increment (3-\>5-\>7-\>9-\>11,…) since all even numbers (except 2) are not primes.
- instead of using for(int i = 0; i \< number/2; i++) use \<=sqrt(number) instead (square root) since all numbers that divide it will be \<= sqrt(num).  
EXAMPLE: 36 is a highly devisable number (anti-prime)  
it can be divided by 1, 2, 3, 4, 6, 9, 12, 18, 36  
so the pairs are:  
1 \* 36  
2 \* 18  
3 \* 12  
4 \* 9  
6 \* 6  
9 \* 4  
12 \* 3  
18 \* 2  
36 \* 1  
since it all just repeats but “flips” (a \* b = b \* a) you can just check till the square root. You must include the square root, so in the for loop use `i<=sqrt(number);`

> **My code**
>
> ```auto
> int n = 2, d = 0;
> void setup() {
> } 
> void draw() {
> for (int i = 0; i < 100; i++) {
> if (isPrime(n)) {
> print(n, "");
> d++;
> }
> n++;
> if (d>9) {
> println();
> d=0;
> }
> }
> }
> boolean isPrime(int x) {
> if (x<4) return true;
> else if (x%2==0) return false;
> for (int i = 1; i <= sqrt(x); i+=2) {
> if (x%i==0&&i>1) return false;
> }
> return true;
> }
> 
> ```

---

<div class="post-metadata">

**Author:** ![javagar](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/javagar/32/11434_2.png) [@javagar](https://discourse.processing.org/u/javagar)\
**Post date:** [July 13, 2021, 11:19am UTC](https://discourse.processing.org/t/check-if-variable-is-a-prime/31216/14 "2021-07-13T11:19:35Z")

</div>

> [@CodeMasterX](#):
>
> … tips to speed up checking of primes …

Thanks, those tips are helpful. However, 1 is not a prime number. From [Wikipedia: Prime number](https://en.wikipedia.org/wiki/Prime_number):

> A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers.

Your `isPrime` function classifies 1 as a prime.

This revision works, and utilizes some of your optimizations:

```auto
boolean isPrime(int x) {
  if (x == 2) return true;
  if (x < 2 || x % 2 == 0) return false;
  for (int i = 3; i <= sqrt(x); i += 2) {
    if (x % i == 0) return false;
  }
  return true;
}

```

---

<div class="post-metadata">

**Author:** ![MoonAlien822](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@MoonAlien822](https://discourse.processing.org/u/MoonAlien822)\
**Post date:** [July 13, 2021, 4:41pm UTC](https://discourse.processing.org/t/check-if-variable-is-a-prime/31216/15 "2021-07-13T16:41:32Z")

</div>

> [@javagar](#):
>
> ```auto
> boolean isPrime(int x) {
> if (x == 2) return true;
> if (x < 2 || x % 2 == 0) return false;
> for (int i = 3; i <= sqrt(x); i += 2) {
> if (x % i == 0) return false;
> }
> return true;
> }
> 
> ```

i changed it to this but now it gives the error,The function isPrimeNumber(int) does not exist.

here is my new code:

```auto
int x=6;
int y= 10-x;
boolean isPrime(int x) {
  if (x == 2) return true;
  if (x < 2 || x % 2 == 0) return false;
  for (int i = 3; i <= sqrt(x); i += 2) {
    if (x % i == 0) return false;
  }
  return true;
}
    

void setup(){

   size(10,10);
   print (x);
   print(y);
 }
   
   void draw(){
      background(255,255,255);
      fill(0,0,0);

      if (isPrimeNumber(x)==true){x=x-1;}
      
      if(x==4&&y==4){x=x+1; y=y+1; print(x);}
             
      if (x==0){x=1;}
       
      if(x==10/2){stop();
      }
      
      if(x>y){y=y+x/2;
      x=x/2;
    if (isPrimeNumber(x)==true){x=x-1; y=y-1;}}
      
      
      if(y>x){x=x+y/2;
      y=y/2;
      if (isPrimeNumber(x)==true){x=x-1; y=y-1;}
       }
      
      ellipse(width-x, height/2, 1,1);
      
      if (x==5&&y==5){stop();}
      print(x);
      print(y);
      
   }

```

---

<div class="post-metadata">

**Author:** ![javagar](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/javagar/32/11434_2.png) [@javagar](https://discourse.processing.org/u/javagar)\
**Post date:** [July 13, 2021, 4:51pm UTC](https://discourse.processing.org/t/check-if-variable-is-a-prime/31216/16 "2021-07-13T16:51:59Z")

</div>

This is the line that contains the function header:

```auto
boolean isPrime(int x) {

```

This line includes one of your function calls:

```auto
      if (isPrimeNumber(x)==true){x=x-1;}

```

The name of the function is `isPrime`, but you are attempting to call a function named `isPrimeNumber`. You’ll need to change either the name of the function or the name used in the calling statements, so that the names match.

---

<div class="post-metadata">

**Author:** ![MoonAlien822](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@MoonAlien822](https://discourse.processing.org/u/MoonAlien822)\
**Post date:** [July 13, 2021, 5:52pm UTC](https://discourse.processing.org/t/check-if-variable-is-a-prime/31216/17 "2021-07-13T17:52:54Z")

</div>

I fixed it but now it returns 6442100. but it is supposed to return 6437 as the first few numbers

---

<div class="post-metadata">

**Author:** ![javagar](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/javagar/32/11434_2.png) [@javagar](https://discourse.processing.org/u/javagar)\
**Post date:** [July 13, 2021, 6:55pm UTC](https://discourse.processing.org/t/check-if-variable-is-a-prime/31216/18 "2021-07-13T18:55:30Z")

</div>

> [@MoonAlien822](#):
>
> … it is supposed to return 6437 as the first few numbers

What has convinced you that the output should begin with 6437?

---

<div class="post-metadata">

**Author:** ![MoonAlien822](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@MoonAlien822](https://discourse.processing.org/u/MoonAlien822)\
**Post date:** [July 13, 2021, 6:58pm UTC](https://discourse.processing.org/t/check-if-variable-is-a-prime/31216/19 "2021-07-13T18:58:32Z")

</div>

well, it starts out with 64, thats just the value i gave x and y. but then it’s supposed to add half of the highest one to the lowest one and get rid of that half for itself. which for 6 and 4 would be (6/2=3  
4+3=7) 37 so the total would be 6437

---

<div class="post-metadata">

**Author:** ![javagar](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/javagar/32/11434_2.png) [@javagar](https://discourse.processing.org/u/javagar)\
**Post date:** [July 13, 2021, 7:27pm UTC](https://discourse.processing.org/t/check-if-variable-is-a-prime/31216/20 "2021-07-13T19:27:10Z")

</div>

> [@MoonAlien822](#):
>
> … it’s supposed to add half of the highest one to the lowest one and get rid of that half for itself.

Is this a challenge problem that you encountered on a web site or somewhere else? If so, please post a link to it or a copy of the original description of the problem.

[Next page](https://discourse.processing.org/t/check-if-variable-is-a-prime/31216.md?page=2)
