generating prime number

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vrkraju
    New Member
    • Feb 2007
    • 6

    generating prime number

    import java.io.*;
    import java.lang.*;
    class Prime
    {
    public static void main(String[] args)
    {
    int a;
    int b;
    String s;
    try{
    BufferedReader br = new BufferedReader (new InputStreamRead er(System.in));
    System.out.prin tln("Enter the number a:");
    s = br.readLine();
    a = Integer.parseIn t(s);
    System.out.prin tln("Enter b");
    s = br.readLine();
    b = Integer.parseIn t(s);
    ?
    ? /// now can u help me in writting the logic between the two numbers to generate prime numbers tat are in betweem them?
    ?
    ?
    ?


    br.close();
    }catch (IOException e){}
    }
    }
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by vrkraju
    import java.io.*;
    import java.lang.*;
    class Prime
    {
    public static void main(String[] args)
    {
    int a;
    int b;
    String s;
    try{
    BufferedReader br = new BufferedReader (new InputStreamRead er(System.in));
    System.out.prin tln("Enter the number a:");
    s = br.readLine();
    a = Integer.parseIn t(s);
    System.out.prin tln("Enter b");
    s = br.readLine();
    b = Integer.parseIn t(s);
    ?
    ? /// now can u help me in writting the logic between the two numbers to generate prime numbers tat are in betweem them?
    ?
    ?
    ?


    br.close();
    }catch (IOException e){}
    }
    }
    1.) Use code tags when posting tags.
    2.)Use the same thread for the same problem.
    3.)What do you want to do now? Do you want to get the prime numbers between two numbers? If so you will need to use a loop testing if the each number is a prime. Consider including a method isPrime(int a) which returns true if the given number is a prime.

    Comment

    • DeMan
      Top Contributor
      • Nov 2006
      • 1799

      #3
      One approach would be try to develop a "stream" of primes
      If you start with a list of all the numbers from 2 to n........
      loop through the list, removing all numbers that are multiples of the current number........

      when someone enters a and b, if b is less than n, you already have the primes, if b is greater than, you will need to add some more numbers to your list (to at least b) and repeat the Sieve process eg
      Code:
      Start list
      2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
      iterate through removing multiples of a[0] (in this case 2)
      2 3 5 7 9 11 13 15 17 19 21 23 25
      then a[1] 
      2 3 5 7 11 13 17 19 23 25
      then a[2]
      2 3 5 7 11 13 17 19 23
      etc....
      Now if a =4 and b = 13 since b < a[last] (23)you can search through the list for prime gretaer than 4 and continue through to 13

      If a=9 b=37 then you nweed to add elements (last +1, last+2 .......) and repeat the process (
      Code:
      2 3 5 7 11 13 17 19 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
      2 3 5 7 11 13 17 19 23 25 27 29 31 33 35 37
      2 3 5 7 11 13 17 19 23 25 29 31 35 37
      2 3 5 7 11 13 17 19 23 29 31 37 etc
      If you were really keen, you could save a little effort by storing what the highes removed number is (so you son't have to reinsert 24 and 25 in the second list)

      Comment

      Working...