Prime Number Generator Logic

后端 未结 15 2224
旧时难觅i
旧时难觅i 2021-01-07 04:10

I am supposed to make a class PrimeNumberGenerator which has a method nextPrime that will print out all prime numbers up to a number the user input

相关标签:
15条回答
  • 2021-01-07 04:36

    I know the question is for a while out here but since no one posted java8/stream approach solution, here is one of the possible ways.

    Gist can be forked here.

    Print output: [1, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71]

    import java.util.*;
    import java.util.stream.Stream;
    
    import static java.util.stream.Collectors.toList;
    
    
    public class PrimeNumber {
    
         /**
         * Java 8 / Lambda approach to generate Prime number.
         * Prime always start to look from number 1.
         * @param series Number of how many Prime number should be generated
         * @return List holding resulting Prime number.
         */
        public static List<Integer> generate(int series) {
            Set<Integer> set = new TreeSet<>();
            return Stream.iterate(1, i -> ++i)
                    .filter(i -> i %2 != 0)
                    .filter(i -> {
                        set.add(i);
                        return 0 == set.stream()
                                .filter(p -> p != 1)
                                .filter(p -> !Objects.equals(p, i))
                                .filter(p -> i % p == 0)
                                .count();
                    })
                    .limit(series)
                    .collect(toList());
        }
    
        // Let's test it!
        public static void main(String[] args) {
            List<Integer> generate = PrimeNumber.generate(20);
            System.out.println(generate);
        }
    }
    
    0 讨论(0)
  • 2021-01-07 04:38

    Check out my code:

    import java.util.Scanner;
    
    public class c4 {
    
            public static void main(String[] args) {
                Scanner sn = new Scanner(System.in);
    
                System.out.println("Enter number");
    
                int num = sn.nextInt();
    
                if (num==1){
                    System.out.println("Nor Prime Neither Composite");
                }
                for (int i=2;i<num;i++){
                    int n=num%i;
    
                    if (n==0){
                        System.out.println("It is a composite number");
                        break;
                    }
                   else{
                       System.out.println("it is a prime number");
                       break;
                   }
               }
           }
        }
    
    0 讨论(0)
  • I think there could be a faster solution for that....

    We all know that 2 is the first prime number,and a prime number is one which is just divisible by 1 and itself.

    Let N = number entered by user till where we have to print the prime. Now,

    if(N<2)
    // No prime numbers
    
    if(N==2)
    // Print 2
    
    if(N>2)
    //print 2
     Create a list(or any resize able data structure)   and add 2 to it.
     now,
     run a loop from i= 3 to i<=n
    {
             count how many numbers in the list are able to divide i completely(Let c                   
    denotes it)
      if(c==0)
    //print the number as it is prime
     }
    
    0 讨论(0)
  • 2021-01-07 04:48

    To generate prime number simply loop through a given number and check if that number is prime or not. For efficient prime number generation IsPrime method must be very efficient and fast. So here is code to check if given number is prime or not very efficiently.

    public static boolean IsPrime(int n) {
    
        if (n > 2 && n %2 == 0){
            return false;
        }
        int top = (int)Math.sqrt(n)+1;
        for (int i=3;i<top;i+=2){
            if (n%i==0){
                return false;
            }
        }
        return true;
    }
    

    Here is the code that will generate prime number between 1 and given number.

     public class GeneratePrimeNumber {
        public static void main(String[] args) {
        System.out.println("Enter number to get prime number");
        int n = new Scanner(System.in).nextInt();
            for (int j=0;j<n;j++){
                if (IsPrime(j)){
                    System.out.print(j + " ");
                }
            }
    
        }
     }
    
    0 讨论(0)
  • 2021-01-07 04:48
    package test;
    
    import java.util.Scanner;
    
    public static void main(String[] args) {
            // TODO Auto-generated method stub
            Scanner reader = new Scanner(System.in);  // Reading from System.in
            System.out.println("Please Enter number you wanted prime number to be generated");
            int n = reader.nextInt();
            reader.close();
    
            Prime t1 = new Prime();
            for (int i = 2; i <= n; i++) {
                t1.x(i);
            }
        }
    
        public void x(int n) {
            // TODO Auto-generated method stub
            // TODO Auto-generated constructor stub
            int k = n - 1;
            int f = 0;
            if (n == 2) {
                f = 1;
            }
            for (int i = 2; i <= k; i++) {
                if (n % i == 0)
                    break;
                else if (k == i) {
                    f = 1;
                }
            }
            if (f == 1) {
                System.out.println("is prime" + n);
            }
    
        }
    }
    
    0 讨论(0)
  • 2021-01-07 04:49

    Try this code to generate prime number series

    public class prime1 {

    public static void main(String[] args) {
    
        int max = 100;
    
        System.out.println("Generate Prime numbers between 1 and " + max);
    
        // loop through the numbers one by one
        for (int i = 1; i < max; i++) {
    
            boolean isPrimeNumber = true;
    
            // check to see if the number is prime
            for (int j = 2; j < i; j++) {
                if (i % j == 0) {
                    isPrimeNumber = false;
                    break; // exit the inner for loop
                }
            }
    
            // print the number if prime
            if (isPrimeNumber) {
                System.out.print(i + " ");
            }
        }
    
    }
    

    }

    0 讨论(0)
提交回复
热议问题