Ask Experts Questions for FREE Help !
Ask
    Navcool's Avatar
    Navcool Posts: 3, Reputation: 1
    New Member
     
    #1

    Sep 11, 2013, 09:53 AM
    Prime Number Search using function
    In the main method, read the start and end integers. In a separate function, find the prime numbers that are available in between the start and end integers given in the main. Return the founded prime numbers back to the main (think we have to use arrays) and print it in the main by calling the function.
    I have done part of the code like this but I don't know how to find the prime numbers between it.. pls correct the code!
    Code:
    import java.util.*;
    public class primeno {
        public static void main (String[]args) 
        {
            Scanner in = new Scanner (System.in);
            System.out.println("Enter the start and end numbers: ");
            int a = in.nextInt();
            int b = in.nextInt();
           int[] result= search(a, b);
            System.out.println(result);
        }
        public static int [] search (int x, int y) {
            int count =0;
            int len = y-x;
            int [] arr = new int[len];
            for (int i=x; i<=y; i++)
            {
                arr[count] = i;
                count++;
                for (int j=2; j<i; j++) {
                    int d = (i)%j;
                    if (d==0) {
                        //not a prime number
                        break;
                    }
                }
            }
            return arr;
        }
    }
    ebaines's Avatar
    ebaines Posts: 12,131, Reputation: 1307
    Expert
     
    #2

    Sep 11, 2013, 10:14 AM
    I'm not a Java expert, but I will comment on your search routine. If I understand your technique for each candidate number between the start and end values of x and y you are trying a division first by 2 and seeing there is a remainder, then by 3, then by 4, etc up to the number.

    Couple of thoughts: first, after testing division by 2 there is no need to test any other even numbers. So you can do the test with 2, then do a loop starting with 3 and incrementing by 2 to get only odd numbers for any further tests. Also there is no need to test any numbers larger than the square root of the number - if you haven't found any factors by then there won't be any. So for example in testing whether 53 is prime you only need to test values 2, 3, 5, and 7 - once you find that none of those divide 53 without a remainder there is no need to test any larger numbers. Making these will cut down on the compute time significantly - for the example of 53 your technique would run through 51 tests, as opposed to only four that are actually needed.

Not your question? Ask your question View similar questions

 

Question Tools Search this Question
Search this Question:

Advanced Search

Add your answer here.


Check out some similar questions!

Windows 7 search function not working [ 1 Answers ]

Search function on the PC doesn't work when I am logged on as administrator. Works when I'm logged on as another user. Help anyone please.

Prime numbers 400-digit number [ 26 Answers ]

I need to find 2 prime numbers that, if multiplied, would generate a 400-digit number. Any idea how to go about this. I have all the prime numbers but no matter what I do I do not get 400. Am I looking at this question wrong maybe? Thanks ::confused:

Can't find prime number [ 1 Answers ]

Can anyone tell me the prime number for this equation? I can not find the calculator to do this. Thanks John (2^1279-1)??

Prime numbers and number theory [ 8 Answers ]

Show that: n^4+4 cannot be prime for n>3.


View more questions Search