Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 1 of 2 12 LastLast
Results 1 to 25 of 43

Thread: Prime number problem (please help)

  1. #1
    Member
    Join Date
    Jun 2014
    Posts
    65
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Prime number problem (please help)

    the prime numbers from 1 to 2500 can be obtained as follows. From a list of the numbers of 1 to 2500,cross out al multiples of 2(but not 2 itself)Then, find the next number (n, say) that is not crossed out and cross out all multiples of n (but not
    including n). Repeat this last step provided that n has not exceeded 50 (the square
    root of 2500). The numbers remaining in the list (except 1) are prime. Write a
    program which uses this method to print all primes from 1 to 2500. Store your
    output in a file called primes.out.


    Can someone please tell me how to go about doing this?


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Prime number problem (please help)

    There are several algorithms on the 'net that describe how to find prime numbers. Google 'java prime numbers', 'sieve of Eratosthenes', 'euler prime numbers', or similar.

  3. #3
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: Prime number problem (please help)

    I am not insulting you here but here are just a few questions:

    1.) Do you how to figure how prime numbers?
    2.) Figure it out on a piece of paper first, do 1-25 for example.
    3.) Post some code and we will help you out, but I am feeling nice and will give you a nice push in the right direction....


    //checks whether an int is prime or not.
    boolean isPrime(int n) {
        for(int i=2;2*i<n;i++) {
            if(n%i==0)
                return false;
        }
        return true;
    }

  4. #4
    Member
    Join Date
    Jun 2014
    Posts
    65
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Prime number problem (please help)

    Quote Originally Posted by jocdrew21 View Post
    I am not insulting you here but here are just a few questions:

    1.) Do you how to figure how prime numbers?
    2.) Figure it out on a piece of paper first, do 1-25 for example.
    3.) Post some code and we will help you out, but I am feeling nice and will give you a nice push in the right direction....


    //checks whether an int is prime or not.
    boolean isPrime(int n) {
        for(int i=2;2*i<n;i++) {
            if(n%i==0)
                return false;
        }
        return true;
    }
    yes i already have that piece of code.I dont know how to get the list of numbers from 1 - 2500. And i dont understand the part about n exceeding 50 from the question.

  5. #5
    Member Abhilash's Avatar
    Join Date
    Jun 2014
    Location
    Kolkata, West Bengal, INDIA
    Posts
    108
    My Mood
    Busy
    Thanks
    5
    Thanked 10 Times in 10 Posts

    Lightbulb Re: Prime number problem (please help)

    This method is called Sieve of Eratosthenes.

    1 2 3 4 5 6 7 8 9 10
    11 12 13 14 15 16 17 18 19 20
    21 22 23 24 25 26 27 28 29 30
    31 32 33 34 35 36 37 38 39 40
    41 42 43 44 45 46 47 48 49 50
    51 52 53 54 55 56 57 58 59 60
    61 62 63 64 65 66 67 68 69 70
    71 72 73 74 75 76 77 78 79 80
    81 82 83 84 85 86 87 88 89 90
    91 92 93 94 95 96 97 98 99 100

    The ones marked red are prime. For this method, an array is used (not this one, single dimensional). First, starting from after 2, every 2nd no. is crossed out. Then find out the smallest remaining no. >2, which is 3. So cross out every 3rd no. Now find smallest remaining no. >3 which is 5. So cross out every 5th no. This goes on till you have crossed out every no. divisible by [root of N] where [] stands for floor function. Use a 1D array for this. Good luck.

  6. #6
    Member
    Join Date
    Jun 2014
    Posts
    65
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Prime number problem (please help)

    how do i get 2500 numbers into the array?

    --- Update ---

    Quote Originally Posted by Abhilash View Post
    This method is called Sieve of Eratosthenes.

    1 2 3 4 5 6 7 8 9 10
    11 12 13 14 15 16 17 18 19 20
    21 22 23 24 25 26 27 28 29 30
    31 32 33 34 35 36 37 38 39 40
    41 42 43 44 45 46 47 48 49 50
    51 52 53 54 55 56 57 58 59 60
    61 62 63 64 65 66 67 68 69 70
    71 72 73 74 75 76 77 78 79 80
    81 82 83 84 85 86 87 88 89 90
    91 92 93 94 95 96 97 98 99 100

    The ones marked red are prime. For this method, an array is used (not this one, single dimensional). First, starting from after 2, every 2nd no. is crossed out. Then find out the smallest remaining no. >2, which is 3. So cross out every 3rd no. Now find smallest remaining no. >3 which is 5. So cross out every 5th no. This goes on till you have crossed out every no. divisible by [root of N] where [] stands for floor function. Use a 1D array for this. Good luck.

    this is what i have so far

     import java.util.*;
    public class  Prime{
    	public static void main (String [] args){
    		Scanner in = new Scanner (System.in);
     
    int [] primes = new int [2500];
     
    	int num=1,i=0;
    	while(num <= 2500){
    		primes[i] = num;
    		num ++;
    		System.out.printf("%d\n",primes[i]);
    	}	
    }//end main
     
    public static void sieve(int [] primes)
     
     
     
    	}

  7. #7
    Member
    Join Date
    Jun 2014
    Posts
    65
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Populate an array?

    How can i populate an array with numbers 1 to 2500???

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Populate an array?

    Use a for loop containing an assignment statement.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Prime number problem (please help)

    Threads merged.

  10. #10
    Member
    Join Date
    Jun 2014
    Posts
    65
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Populate an array?

    Quote Originally Posted by Norm View Post
    Use a for loop containing an assignment statement.
    for (int i=0;i <=2500; array[i] ++)

    is that it??

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Prime number problem (please help)

    Try it and see what happens. Print the contents of the array using:
    System.out.println("an ID "+ java.util.Arrays.toString(theArrayNameHere));
    Recommend doing it for 25 elements vs 2500 for testing.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12
    Member
    Join Date
    Jun 2014
    Posts
    65
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Prime number problem (please help)

    Quote Originally Posted by Norm View Post
    Try it and see what happens. Print the contents of the array using:
    System.out.println("an ID "+ java.util.Arrays.toString(theArrayNameHere));
    Recommend doing it for 25 elements vs 2500 for testing.

    ok im making some progress with the problem.But im trying to output the results to a file however im getting an error.
    	if(isPrime[i - 1]){
     			out.printf("%d",i);
     			out.println();

    everything worked well before i tried to output the results to a file.
    im getting an error at the two out.printf lines that says cannot find symbol.

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Prime number problem (please help)

    cannot find symbol.
    What symbol is not found?
    If you don't understand my answer, don't ignore it, ask a question.

  14. #14
    Member
    Join Date
    Jun 2014
    Posts
    65
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Prime number problem (please help)

    Quote Originally Posted by Norm View Post
    What symbol is not found?
    idk it just says symbol not found. I made sure to close the file. However im not getting my data from an input file.It is populated into an array.Everything worked b4 i tried to print to a file. So im stuck

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Prime number problem (please help)

    symbol not found
    That's a compiler error which means your code is not ready for execution. You need to find out what symbol is not found. Try using the javac command to compile the file. It gives nice error messages:
    The error message shows the source with a ^ under the location of the error.
    Here is a sample from the javac compiler:
    TestSorts.java:138: cannot find symbol
    symbol  : variable var
    location: class TestSorts
             var = 2;
             ^
    If you don't understand my answer, don't ignore it, ask a question.

  16. #16
    Member
    Join Date
    Jun 2014
    Posts
    65
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Prime number problem (please help)

    Quote Originally Posted by Norm View Post
    That's a compiler error which means your code is not ready for execution. You need to find out what symbol is not found. Try using the javac command to compile the file. It gives nice error messages:
    The error message shows the source with a ^ under the location of the error.
    Here is a sample from the javac compiler:
    TestSorts.java:138: cannot find symbol
    symbol  : variable var
    location: class TestSorts
             var = 2;
             ^
    public static void Sieve(int n)throws IOException{
     
     	boolean [] isPrime = new boolean [n];
     	isPrime [0] = false;
     
     	for(int c=1;c <n; c++){
     		isPrime[c] = true;
     	}//end for
     	for(int i=2;i <=n;i++){
     		if(isPrime[i - 1]){
     			out.printf("%d",i);
     			out.println();
     		for(int j= i * i;j <=n;j+=i){
     			isPrime[j-1]=false;
     		}
     		}
     	}	
     }//end method
    }//end class

    thats my method. it says cannot find symbol
    out.println();
    ^
    symbol: variable out
    location: class
    2 errors

    Process completed.

    --- Update ---

    the method works without the implementation of the output file. How can someone help me plz.How can i send the data to an output file

  17. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Prime number problem (please help)

    Where is the variable: out defined? Is it in scope where the error happens?
    If you don't understand my answer, don't ignore it, ask a question.

  18. #18
    Member
    Join Date
    Jun 2014
    Posts
    65
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Prime number problem (please help)

    Quote Originally Posted by Norm View Post
    Where is the variable: out defined? Is it in scope where the error happens?
    no its defined in the main .
    still getting the same error

  19. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Prime number problem (please help)

    its defined in the main
    If it is defined inside of a method, then it is OUT OF SCOPE in any other method. Move its definition out of the method to the class level so all methods can see it.
    A variable can be defined in one place and given a value at another place.
    String var;
    ...

    var = "AS";
    If you don't understand my answer, don't ignore it, ask a question.

  20. #20
    Member
    Join Date
    Jun 2014
    Posts
    65
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Prime number problem (please help)

    Quote Originally Posted by Norm View Post
    If it is defined inside of a method, then it is OUT OF SCOPE in any other method. Move its definition out of the method to the class level so all methods can see it.
    A variable can be defined in one place and given a value at another place.
    String var;
    ...

    var = "AS";
    im still not getting it. Here's the entire thing.
    [
    Last edited by javaman1; June 20th, 2014 at 09:44 PM.

  21. #21
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Prime number problem (please help)

    Please read post#19
    Where is out defined?
    class A_Class {
       int classVar; // define at class level;  in Scope for all methods
     
       void meth1() {
           int aVar;    // define aVar; in Scope only within meth1
           //  classVar in Scope here
           //...
       } // end meth1()
     
       void meth2() {
           //  aVar in NOT in SCOPE here
           //  classVar in Scope here
           //...
       } //  end meth2()
    } // end class
    If you don't understand my answer, don't ignore it, ask a question.

  22. #22
    Member
    Join Date
    Jun 2014
    Posts
    65
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Prime number problem (please help)

    Quote Originally Posted by Norm View Post
    Please read post#19
    Where is out defined?
    class A_Class {
       int classVar; // define at class level;  in Scope for all methods
     
       void meth1() {
           int aVar;    // define aVar; in Scope only within meth1
           //  classVar in Scope here
           //...
       } // end meth1()
     
       void meth2() {
           //  aVar in NOT in SCOPE here
           //  classVar in Scope here
           //...
       } //  end meth2()
    } // end class
    Ok i did that and its saying expected. I dont know what else to do

  23. #23
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Prime number problem (please help)

    its saying ...
    You need to copy the full text of the error message and paste it here. I can't see your code from here.
    If you don't understand my answer, don't ignore it, ask a question.

  24. #24
    Member
    Join Date
    Jun 2014
    Posts
    65
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Prime number problem (please help)

    Quote Originally Posted by Norm View Post
    You need to copy the full text of the error message and paste it here. I can't see your code from here.
    --------------------Configuration: <Default>--------------------
    C:\Users\Desktop\Primes.java:32: error: <identifier> expected
    out.close();
    ^
    1 error

    Process completed.

  25. #25
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Prime number problem (please help)

    Please post the whole class where that statement is.

    The error message says that statement is in the wrong place in the code.

    Do you know how to define a variable?
    If you don't understand my answer, don't ignore it, ask a question.

Page 1 of 2 12 LastLast

Similar Threads

  1. Check if number is prime number or not problem[SOLVED]
    By kinkita in forum Loops & Control Statements
    Replies: 9
    Last Post: July 17th, 2013, 10:59 AM
  2. determining a prime number
    By ob7ivion in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 14th, 2013, 09:27 AM
  3. Problem with my prime number finder program
    By Truck35 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 6th, 2012, 11:25 PM
  4. Prime number solver.
    By Danny123 in forum What's Wrong With My Code?
    Replies: 17
    Last Post: June 4th, 2012, 05:30 AM
  5. Prime Number Code Help!
    By aandcmedia in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 7th, 2012, 12:07 AM