Jump to content


- - - - -

[JAVA]List of primary numbers!


  • Please log in to reply
No replies to this topic

#1 _*cipcip_*

_*cipcip_*
  • Guests

Posted 16 October 2007 - 11:48 AM

In this tutorial ... you are going to create in JAVA a small program that will show you a list of primary numbers ...

these are really basic programs and really easy to be made!

Here is the code:

import java.io.*;
class list{
 public static void main(String[]args) throws IOException{
  BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
  
 int n;
 System.out.println("Insert number:");
 n=Integer.parseInt(stdin.readLine());
 
 int[] prim=new int[n];
 prim[0]=2;
 prim[1]=3;
 
 int k=1;
 int nr=5;
 int j,primj;
 
 while(k<=n-2) {
  j=0;
  primj=prim[j];
 while((primj*primj<=nr) && (nr%primj!=0)) primj=prim[++j];
 
 if(primj*primj>nr) prim[++k]=nr;
 nr=n+2;
 }
 
 for(k=0;k<n;k++)
 System.out.println(k+ " : "+prim[k]);
 }
}

Here is the code`s screenshot:

Posted Image

Now ... this is the result, first you need to enter a number and then the list will be showed ...

Posted Image




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users