Prime Checker

Sort by

recency

|

343 Discussions

|

  • + 0 comments

    **Why do they add 4th and 5th together instead of individually? **

    Shouldn't the result of such a task include 5 lines of solution instead of 4?

    ob.checkPrime(n1); ob.checkPrime(n1,n2); ob.checkPrime(n1,n2,n3); ob.checkPrime(n1,n2,n3,n4,n5);

    **Why they did it like that? **

    For me better will be sth like this: import java.util.*;

    public class Solution {

    public static void main(String[] args) {
       Prime prime = new Prime();
        prime.checkPrime();
    
    
    }
    

    }

    class Prime{

    public static void checkPrime(){
        Scanner scanner = new Scanner(System.in);
        int number;
        int divisors =2;
        ArrayList<String> primeList = new ArrayList<>();
    
        for(int i=0;i<5;i++){
            number=scanner.nextInt();
            for(int j=2;j<=Math.sqrt(number);j++){
                if(number%j==0){
                    divisors++;
                }
            }
            if(divisors==2&&number!=1){
                primeList.add(String.valueOf(number));
            }
            divisors=2;
            System.out.println(String.join(" ",primeList));
        }
    
    }
    

    }

  • + 0 comments
    import java.lang.System.in;
    
    class Prime {
    	public void checkPrime(int... num){
    		int count = 0;
    		for (int n : num){
    			if(n == 2){
    				System.out.print(n + " ");
    			} 
    			if (n > 2){
    				for(int i=2;i <= Math.sqrt(n);i++){
    						if(n%i == 0){
    							count++;
    						}
    				}
    				if(count==0){
    					System.out.print(n + " ");
    				}
    				count = 0;
    			}
    		}
    		System.out.println();
    	}
    }
    
  • + 0 comments
    import java.lang.System.in;
    
    class Prime {
    	
    	public void checkPrime(int... num){
    		
    		int count = 0;
    		
    		for (int n : num){
    		
    			if(n == 2){
    				System.out.print(n + " ");
    			} 
    			if (n > 2){
    				for(int i=2;i <= Math.sqrt(n);i++){
    						if(n%i == 0){
    							count++;
    						}
    				}
    				if(count==0){
    					System.out.print(n + " ");
    				}
    				count = 0;
    			}
    		}
    		System.out.println();
    	}
    }
    
  • + 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;
    
        }
    }