Day 25: Running Time and Complexity

  • + 0 comments

    `**Java Best Solution to Understand ** import java.io.; import java.util.;

    public class Solution {

    public static String prime(int n){
    
        for (int i = 2; i < Math.sqrt(n); i++) {
            if (n%i==0) {
                return "Not prime";
            }
        }
        return "Prime";
    }
    
    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        ArrayList<Integer> a = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            int j = sc.nextInt();
            a.add(j);
        }
    
        for (Integer i : a) {
                System.out.println(prime(i));
        }
    }
    

    } `