Jump to content

- - - - -

[JAVA]Primary number tutorial!

  • Please log in to reply
No replies to this topic

#1 _*cipcip_*

  • Guests

Posted 16 October 2007 - 11:47 AM

Now we are going to do a little math in java ... well ... most of the next tutorial will be basic math ..

In this tutorial we are going to create a small little program that show us if a number is primary or not!

Here is the code:

import java.io.*;
class primary {
public static void main(String[]args) throws IOException {
  long x;
  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  System.out.print("x= ");
  if(estePrim(x)) System.out.println(x+" is a primary number");
	else System.out.println(x+" is NOT a primary number");
static boolean estePrim(long nr) {
  if((nr==0) || (nr==1)) return false;
  if((nr==2) || (nr==3)) return true;
if(nr%2==0) return false;
long d=3;
while ((nr%d!=0) && (d*d<=nr)) d=d+2;
  if(nr%d==0) return false;
else return true;

Here is the code screenshot:

Posted Image

Now, here is the compiled version screenshot:

Posted Image

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users