Prime Checker

Sort by

recency

|

357 Discussions

|

  • + 0 comments

    Sieve of Eratosthenes

    public class Solution {
    
        public static void main(String[] args) {
            
            Scanner sc=new Scanner(System.in);
           
            int[] inp=new int[5];
            for(int i=0;i<5;i++){
                inp[i]=sc.nextInt();
            }
            
            int upto=Arrays.stream(inp).max().getAsInt();
            boolean[] sieve=new boolean[upto+1];
             Arrays.fill(sieve,true);
             sieve[0]=false;
             sieve[1]=false;
             
             for(int i=2;i*i<=upto;i++){
                int ele=i;
                if(sieve[ele]){
                    for(int j=i*i;j<=upto;j+=i){
                        sieve[j]=false;
                    }
                }
             }
             Solution.printHelper(sieve, inp[0]);
             Solution.printHelper(sieve, inp[0],inp[1]);
             Solution.printHelper(sieve, inp[0],inp[1],inp[2]);
             Solution.printHelper(sieve, inp[0],inp[1],inp[2],inp[3],inp[4]);
        }
        public static void printHelper(boolean[] primes,int... inps){
            for(int ele:inps){
                System.out.print(primes[ele]?String.valueOf(ele)+" ":"");
            } 
            System.out.println();
        }
    }
    
  • + 1 comment

    I never use import static before, this problem gives me good insight. Also, searching for math equation for checkPrime and adapt it to java is fun, and it remind me that (int / int( will automatically truncate decimal point !!

    here is my solution:

    import static java.lang.System.in;
    
    class Factorial {
        public static int calFactorial(int in) {
            int cal = 1;
            
            while (in > 0) {
                cal = cal * in;
                in--;
            }
            
            return cal;
        }
    }
    
    class Prime {
        public void checkPrime(int ...args) {
            String res = "";
            
            for (int i : args) {
                if (i != 0 && i != 1) {    
                    // this is Willans' formula            
                    double result = Math.pow(Math.cos(Math.PI * ((Factorial.calFactorial(i - 1) + 1) / (double) i)), 2);
                    
                    if (result >= 1) {
                        if (res.length() > 0) {
                            res = res + " " + i;
                        } else {
                            res = res + i;
                        }
                    }
                }
            }
            
            if (res.isEmpty()) {
                System.out.println("");
            } else {
                System.out.println(res);
            }
        }
    }
    
  • + 0 comments

    solution for java 8 import java.io.; import java.util.;

    public class Solution {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        Prime ob = new Prime();
    
        int a = scan.nextInt();
        int b = scan.nextInt();
        int c = scan.nextInt();
        int d = scan.nextInt();
        int e = scan.nextInt();
    
        ob.checkPrime(a);
        ob.checkPrime(a,b);
        ob.checkPrime(a,b,c);
        ob.checkPrime(a,b,c,d,e);
    
        scan.close();
    }
    

    }

    class Prime{
        void checkPrime(int... numbers){
            for(int num: numbers){
                if(isPrime(num)){
                    System.out.print(num + " ");
                }
            }
            System.out.println();
        }
        private boolean isPrime(int n){
            if(n < 2) return false;
            for(int i = 2; i <= Math.sqrt(n); i++){
                if(n % i == 0) return false;
            }
            return true;
        }
    }
    
  • + 0 comments

    you can add

    import static java.lang.System.in;
    in java 7 to fix the compilation error

    and enjoy

  • + 0 comments
    class Prime{
        public void checkPrime(int... input){
            boolean isPrime;
            for(int i: input){
                isPrime = true;
                for(int j=2; j*j<=i; j++){
                    if(i%j == 0){
                        isPrime=false;
                        break;
                    }
                }
                if(isPrime && i!=1){
                    System.out.print(i + " ");
                }
            }
            System.out.println();
        }
    }