how to check if x is a prime?
Hey I think this is what you are looking for:
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
thank you, but now its returning this error : isPrimeNumber cannot be resolved to a variable
here’s my code:
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);
}
You need to pass a variable to the function with (x)
Also use == not single = (which is for assignment not comparison)
what do you mean by that and how do i do it?
Just after isPrimeNumber write (x) ==
Not sure you really mean this since it would give always 1
Try x = x-1;
this is a for loop, can a for loop work outside of draw?
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
so it only checks in the beginning?
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
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 usei<=sqrt(number);
My code
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;
}
Thanks, those tips are helpful. However, 1 is not a prime number. From Wikipedia: 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:
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:
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);
}
This is the line that contains the function header:
boolean isPrime(int x) {
This line includes one of your function calls:
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.
I fixed it but now it returns 6442100. but it is supposed to return 6437 as the first few numbers
What has convinced you that the output should begin with 6437?
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
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.