Prime Checker

Sort by

recency

|

340 Discussions

|

  • + 0 comments

    Here is my solution to this problem:

    ` public class Solution {

    public static void main(String[] args) {
    
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            int n1 = Integer.parseInt(br.readLine());
            int n2 = Integer.parseInt(br.readLine());
            int n3 = Integer.parseInt(br.readLine());
            int n4 = Integer.parseInt(br.readLine());
            int n5 = Integer.parseInt(br.readLine());
    
            Prime prime = new Prime();
            prime.checkPrime(n1);
            prime.checkPrime(n1, n2);
            prime.checkPrime(n1, n2, n3);
            prime.checkPrime(n1, n2, n3, n4, n5);
    
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    

    static class Prime { public void checkPrime(int... numbers) { for (int num : numbers) { if (isPrime(num)) { System.out.print(num + " "); } } System.out.println(); }

    private boolean isPrime(int num) {
        if (num <= 1) return false;
        if (num == 2) return true;
        if (num % 2 == 0) return false;
    
        for (int i = 3; i <= Math.sqrt(num); i += 2) {
            if (num % i == 0) return false;
        }
        return true;
    }
    

    } } `

  • + 0 comments

    this is a very simple way to check PrimeNumber static class Prime {

        public boolean checkPrime(int num) {
            boolean isPrime = false;
            if (num == 2) {
                isPrime = true;
            }
            for (int i = 2; i < num; i++) {
                if (num % i == 0) {
                    isPrime = false;
                    break;
    
                } else if (num % i != 0) {
                    isPrime = true;
                }
    
            }
    
            return isPrime;
    
        }
    }
    
  • + 0 comments
    class Prime {
        
        public void checkPrime(int ... numbers) {
            StringBuilder message = new StringBuilder();
            for (int number : numbers) {
                if (BigInteger.valueOf(number).isProbablePrime(1)) {
                     message.append(number).append(" ");
                }
            }
            System.out.println(message);
        }
        
    }
    
  • + 0 comments
    import static java.lang.System.in;
        
    class Prime{
        public void checkPrime(int ...nbr) {
            StringBuilder tmp = new StringBuilder();
            for (int n : nbr) {if (isPrime(n)) tmp.append(n).append(" ");}
            System.out.println(tmp);
        }
        private boolean isPrime(int n) {
            if (n <= 1) return false;
            for (int i = 2; i <= Math.sqrt(n); i++) 
               {if (n % i == 0) return false;}
            return true;
        }
        
    };
    
  • + 0 comments

    Here is the solution:

    import java.io.; import java.util.; import java.lang.reflect.Method;

    class Prime { public void checkPrime(int ... n) { for (int j=0;j

        }  System.out.println();
    

    } }

    public class Solution {

    public static void main(String[] args) {
        try{
        InputStream in = System.in;
        BufferedReader br=new BufferedReader(new InputStreamReader(in));
        int n1=Integer.parseInt(br.readLine());
        int n2=Integer.parseInt(br.readLine());
        int n3=Integer.parseInt(br.readLine());
        int n4=Integer.parseInt(br.readLine());
        int n5=Integer.parseInt(br.readLine());
        Prime ob=new Prime();
        ob.checkPrime(n1);
        ob.checkPrime(n1,n2);
        ob.checkPrime(n1,n2,n3);
        ob.checkPrime(n1,n2,n3,n4,n5);    
        Method[] methods=Prime.class.getDeclaredMethods();
        Set<String> set=new HashSet<>();
        boolean overload=false;
        for(int i=0;i<methods.length;i++)
        {
            if(set.contains(methods[i].getName()))
            {
                overload=true;
                break;
            }
            set.add(methods[i].getName());
    
        }
        if(overload)
        {
            throw new Exception("Overloading not allowed");
        }
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
    }
    

    }