One day, there’s some one came to me and intent to start learning Java programming, she asked a lot about Java and it’s technology. One of her question based on her story-told is how can we make a Java application which is able to generate an array of prime number?, the problem is, I have forgotten what is the definition of prime number it self, so we had a problem to make the application that will appropriate with her idea. ha..ha..ha..(what an excuse…!!!).

Yesterday, I remembered that, and I go *Googling, *to find out what is prime number definition. From the Indonesian version of wikipedia site, I found prime number definition, but I tried to make my own comprehension of this prime number, **Prime Number is a real number beginning with 2, this prime number is a number that is not exactly divisible by any number less then it self, except 1. **That’s my comprehension of prime number, so let’s take a number and try to check with my comprehension, lets take 15 as a sample. Is 15 is prime number?, we will arrange our inspection like this :

- Is 15 is more or equals to 2 (15 >= 2 ) ?, because first condition of prime number is
**real number that start from 2**. - Is 15 exactly divisible by a numbers started from 2 until 14 ?, because other condition of prime number is
**a number that is not exactly divisible by any number less then it self, except 1**.

OK, let’s run this inspection one by one, for the first step of the inspection, 15 is fulfill the condition of a prime number, meaning that 15 >= 2 is true or correct. And then let’s continue with the 2^{nd} step of the inspection. Is 15 is exactly divisible by 2, and the result is 15 got reminder when it devise b,y 2, because 15/2 =7 and the reminder is 1. The checking is continue by devise again with next number which is 3, at this division, it’s clear that 15 is not a prime number, because 15 devise by 3 is got no reminder. And we can assume that **15 is not a prime number because 15 is exactly divisible by 3.**

Now, let’s take a new number to check, the new number is 17, and we will check whether this 17 is a prime number or not using same inspection above. For the first check, it’s clear that 17 is highest than 2, and for 2^{nd} inspection, let’s take a look the picture below:

```
17 / 2 = 8 remind 1
17 / 3 = 5 remind 2
17 / 4 = 4 remind 1
17 / 5 = 3 remind 1
17 / 6 = 2 remind 5
17 / 7 = 2 remind 3
17 / 8 = 2 remind 1
17 / 9 = 1 remind 8
17 / 10 = 1 remind 7
17 / 11 = 1 remind 6
17 / 12 = 1 remind 5
17 / 13 = 1 remind 4
17 / 14 = 1 remind 3
17 / 15 = 1 remind 2
17 / 16 = 1 remind 1
```

On the above picture we can see that 17 always got reminder when we devise it by any number that less then 17, except 1. And now we can assume that **17 is a prime number.**

How about, if with those steps we try to make a Java application that have an ability to check a number whether is a prime number or not. OK, let’s try to make an application that have an ability to check a number whether it’s prime number or not, here comes the codes:

```
package org.mojo.blog.app.integer; //ignore this, if this program is not on package
public class isPrimeApplication {
public static void main(String[] args) {
int valueToCheck = 17; // value to check
boolean isPrime = false;
if (valueToCheck >= 2) {
isPrime = true; // first check and assume it's prime number
// try divide valueToCheck
// with all number less than it self
// and begining from 2
for (int i = 2; i < valueToCheck; i++) {
if (valueToCheck % i == 0) {
//if divides exactly so stop the loop and it must be not prime
isPrime = false;
break; // no need to check again
}
}
}
System.out.println("is " + valueToCheck + " Prime ? ");
System.out.println("the answer is " + isPrime);
}
}
```

In above codes, we can see that we have 2 variables, **valueToCheck** and **isPrime, **and the process flow will look like exactly same with the inspection above, where :

**if (valueToCheck > = 2)**is first inspection, the number which entering this block will temporary assumed as prime number.**for (int i=2; i < valueToCheck; i++),**we will do loop, start from 2 until the value of checked number minus 1 (valueToCheck – 1) . And then we will do checking, if from this loop operation there’s a number can exactly divisible, then stop the loop, cos that number is not a prime number clearly.

Hmm, what if we continue the development of this application become an application that have an ability to find out prime numbers of given range, such as, find out prime numbers between 0-50. It looks like, we can make it by adding just one looping-logic before the existed loop. Let’s take a look of this codes :

```
package org.mojo.blog.app.integer; //ignore this, if this program is not on package
public class PrimeAdvance {
public static void main(String[] args) {
// int valueToCheck = 17; // value to check
int nRange = 50;
boolean isPrime = false;
for (int i = 2; i <= nRange; i++) {
if (i >= 2) {
isPrime = true; // first check and assume it's prime number
// try divide valueToCheck
// with all number less than it self
// and begining from 2
for (int j = 2; j < i; j++) {
if (i % j == 0) {
//if divides exactly so stop the loop and it must be not prime
isPrime = false;
break; // no need to check again
}
}
}
if(isPrime){
System.out.println(i);}
}
//System.out.println("is " + valueToCheck + " Prime ? ");
//System.out.println("the answer is " + isPrime);
}
}
```

On the above code, we can see that we just add one looping-logic before the existed looping. And then we add lines to check if i is prime number, then print out i to console. And we put this lines inside the block of looping the range data that will check.

That’s it, a little story about the my wondering of prime number, triggered by friend’s story. You can use it freely if it’s helpful for your case, and it’s freely too for you to develop mode if you have any brand new idea.

To more understand the prime number it self, here’s a link of Java program source code, that I have added some console print out, in order to watch the process of finding out a prime number from a given range

I hope this helpfully

Menteng, 01 January 2009

**josescalia**