Brijesh Shah

Ranch Hand

Posts: 34

Ulf Dittmer

Rancher

Posts: 42972

73

Brijesh Shah

Ranch Hand

Posts: 34

posted 11 years ago

As requested prior, please provide (ie: post here) what you have done so far (ie: post your code), explain what it is that you expect to happen and show what is occuring. That way the great folks here can help you understand where you are "going wrong" as well as documenting learnings for others.

posted 11 years ago

In other words, are you asking somebody to do your homework for you? Please do your own homework - homework is for you to learn something, and you learn by doing it, not by asking other people to do it for you.

So write some code yourself. If there are specific issues that you get stuck with, feel free to ask for hints and tips here.

[ August 14, 2006: Message edited by: Jesper Young ]

Originally posted by Vicky Shah:

If possible can you provide me the code that will return the prime numbers between 1 to N

In other words, are you asking somebody to do your homework for you? Please do your own homework - homework is for you to learn something, and you learn by doing it, not by asking other people to do it for you.

So write some code yourself. If there are specific issues that you get stuck with, feel free to ask for hints and tips here.

[ August 14, 2006: Message edited by: Jesper Young ]

Ulf Dittmer

Rancher

Posts: 42972

73

Rusty Shackleford

Ranch Hand

Posts: 490

posted 11 years ago

If you haven't started, start with the basics. Use the definition of prime numbers as your starting point to build an algorithm. There are some optimizations to cut down on how many numbers you need to test for any N, but just get it working, and go from there.

"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra

Jeroen T Wenting

Ranch Hand

Posts: 1847

Brijesh Shah

Ranch Hand

Posts: 34

posted 11 years ago

Hi All,

So many comments!!!

Ah...

Well I could not reply you guys as i was not connected to the net.

I have a program here:

/*Program to print prime numbers between 1 to n*/

while(i<=n-1)

{

if((n%i)==0)

{

break;

}

i++;

}

if(i==n)

{

System.out.println("The prime numbers between 1 and " +n+ " are:");

System.out.println(""+n);

}

n--;

}

}

Please help me why this code does not lead me to the end results???

Thanks in advance....

So many comments!!!

Ah...

Well I could not reply you guys as i was not connected to the net.

I have a program here:

/*Program to print prime numbers between 1 to n*/

while(i<=n-1)

{

if((n%i)==0)

{

break;

}

i++;

}

if(i==n)

{

System.out.println("The prime numbers between 1 and " +n+ " are:");

System.out.println(""+n);

}

n--;

}

}

Please help me why this code does not lead me to the end results???

Thanks in advance....

Campbell Ritchie

Marshal

Posts: 55768

163

posted 11 years ago

Please use the code tags whenever you post code on screen; it makes it much easier to read and preserves the indentation.

Your algorithm only test one number, "n," and sees whether it is a prime number. So if you pass 32, which is not a prime number, it finds it is not a prime number, and doesn't print anything.

You are using an inefficient algorithm; any non-prime number which is not a perfect square of a prime number (eg 32, not 49) will have at least one factor which is smaller than its square root. So you only need to count as far as the square root of the largest number you are interested in.

You need to go back to ancient Greece; find the algorithm called the "sieve of Eratosthenes." I think that is the correct spelling.

And I agree about this being a "beginner's" question.

CR

Your algorithm only test one number, "n," and sees whether it is a prime number. So if you pass 32, which is not a prime number, it finds it is not a prime number, and doesn't print anything.

You are using an inefficient algorithm; any non-prime number which is not a perfect square of a prime number (eg 32, not 49) will have at least one factor which is smaller than its square root. So you only need to count as far as the square root of the largest number you are interested in.

You need to go back to ancient Greece; find the algorithm called the "sieve of Eratosthenes." I think that is the correct spelling.

And I agree about this being a "beginner's" question.

CR

Brijesh Shah

Ranch Hand

Posts: 34

posted 11 years ago

Hi Campbell,

I have his program to find the prime numbers between 1 to N

import java.io.*;

class Prime

{

//if error occurs during writing IOException is thrown

public static void main(String[] args) throws IOException

{

int n,i=2;

//reads a character from the input stream and returns it as an integer value

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter the value of n:");

//reads the integers using readLine() and uses parseInt() to convert these strings

//into their int equivalents

n=Integer.parseInt(br.readLine());

while(i<=n-1)

{

if((n%i)==0)

{

break;

}

i++;

}

if(i==n)

{

System.out.println("The prime numbers between 1 and " +n+ " are:");

System.out.println(""+n);

}

n--;

}

}

If possiblem can you provide me the code for this program....

I have his program to find the prime numbers between 1 to N

import java.io.*;

class Prime

{

//if error occurs during writing IOException is thrown

public static void main(String[] args) throws IOException

{

int n,i=2;

//reads a character from the input stream and returns it as an integer value

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter the value of n:");

//reads the integers using readLine() and uses parseInt() to convert these strings

//into their int equivalents

n=Integer.parseInt(br.readLine());

while(i<=n-1)

{

if((n%i)==0)

{

break;

}

i++;

}

if(i==n)

{

System.out.println("The prime numbers between 1 and " +n+ " are:");

System.out.println(""+n);

}

n--;

}

}

If possiblem can you provide me the code for this program....