Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Prime Number program Help!
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Simplicity




PostPosted: Sat Feb 28, 2009 11:06 pm   Post subject: Prime Number program Help!

Hi, so I need to code a program that outputs all the prime numbers between 2 numbers the user enters.
I'm not sure how to do this. I tried to do that but it didn't work properly. My program counts all the mod values when it is 0, how do I make it so it counts each individual number? Or is there an easier way to code this? Thanks in advance! Very Happy
Java:
 
 import java.util.Scanner;
   import java.lang.Math;

    public class  PrimeNumber {
   
       public static void main (String [] args) {
     
         Scanner input=new Scanner (System.in);
         int num1,num2;
         int number;
         int count=0;
         int answer=0;
   
         int primeNumber=0;
 
         System.out.print("Enter a number :");
         num1=input.nextInt();
       
         System.out.print("Enter another number :");
         num2=input.nextInt();
     
       
     
         for (int num=(num1+1);num<num2;num++) {
            for (int i=1; i<=9;i++) {
               answer=num % i;
               primeNumber=answer;
           
             
               if (answer==0) {
                  count+=1;
               
 
               
               }
            }
         
            if (count==2) {
               primeNumber=answer;
               System.out.print ("The prime numbers are :"+primeNumber+" "); }
           
            else { System.out.println ("No prime numbers between those two numbers ");}
         
         }
         
      }
   }
Sponsor
Sponsor
Sponsor
sponsor
jbking




PostPosted: Sun Mar 01, 2009 12:35 am   Post subject: Re: Prime Number program Help!

Why does that i loop from 1 to 9? It would appear to me to be more logical to start at 2 and if the mod is 0 then break and try the next number. Also, you only need to go up so far in determining if a number is prime, e.g. for given integer n, it should make sense that you don't need to mod n-2 or n-3 if n is 10 or more so that may be some help. Also, you may have a bug involving the count if you trace through your program a few times that you should see fairly easily.
Euphoracle




PostPosted: Sun Mar 01, 2009 8:58 am   Post subject: RE:Prime Number program Help!

Here's an idea. You already know the first prime number, which is 2. You can create an array of size n, an array of integers to hold your prime numbers. Given that a prime number is a number which cannot be divided by any other numbers than 1 and itself, you can therefore say that a prime number is also a number that cannot be divided by another prime number. Starting at 2, you can create a for loop to n. Each iteration, with number i, test to see if any of your prime numbers can divide evenly with i. If no prime number can divide evenly with i, add i to your array and continue your loop.

eg. If i = 3, i mod 2 > 0, i is prime
eg. If i = 4, i mod 2 = 0, i is not prime
eg. If i = 7, i mod 2 > 0, i mod 3 > 0, i mod 5 > 0, i is prime.
eg. If i = 12, i mod 2 = 0 or i mod 3 = 0, i is not prime.

In the end, you'll have something like

{ 2, 3, 5, 7, 13 } as your array to a given point, n. You can output these numbers afterwards Smile

Issues? You have to start at 2 for your prime numbers, because every other number is a product of 2 and something else, so you can't start at your starting range and end at your ending range. However, you can start at 2 and end at your ending range Smile
A.J




PostPosted: Sun Mar 01, 2009 10:53 am   Post subject: Re: Prime Number program Help!

There is an easy way to check if a number is prime : If a number 'n' doesn't divide into any number from 2 -> sqrt(n) evenly, then it is prime.

So :
Java:
 
int n = 37;
bool prime = true;

for (int i = 2; i <= floor(sqrt(n)); i++)
{
    if (n % i == 0)
    {
        prime = false;
        break;
    }
}

if (prime)
    System.out.println(n + " is a prime number.");
else
    System.out.println(n + " is not a prime number.");


So, instead of checking from 2 -> n - 1 for a prime, one just has to make sure that all the numbers from 2 -> sqrt(n) does not divide evenly into n to ensure that n is prime.

However, this method does get slow as 'n' gets bigger. Another method for generating primes (a much faster method), is called "The Sieve of Eratosthenes". I don't won't go in detail in explaining it. Look it up, it actually is VERY helpful Very Happy
syntax_error




PostPosted: Sun Mar 01, 2009 1:02 pm   Post subject: RE:Prime Number program Help!

Sift the Two's and sift the Three's,
The Sieve of Eratosthenes.
When the multiples sublime,
The numbers that remain are Prime.


EDIT: Sorry A.J. Didn't notice till now you already included the above.
A.J




PostPosted: Sun Mar 01, 2009 2:48 pm   Post subject: Re: Prime Number program Help!

syntax_error wrote:

EDIT: Sorry A.J. Didn't notice till now you already included the above.


No problem, syntax_error.

Now that I think of it, the sieve method isn't that necessary (unless the numbers you are checking are huge).

The checking-till-the-square-root method should work fine.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: