Day 25: Running Time and Complexity

Sort by

recency

|

807 Discussions

|

  • + 0 comments

    import java.io.; import java.util.;

    public class Solution {

    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 scan= new Scanner(System.in);
    
        int casos= scan.nextInt();
    
        for (int i = 0; i < casos; i++) {
            int number= scan.nextInt();
    
             if(number<=1){
            System.out.println("Not prime");
        }else{
        boolean esPrimo=true;
    
        for (int j = 2; j <= Math.sqrt(number); j++) {
            if(number%j==0){
                esPrimo=false;
                break;
            }   
        }
        if(!esPrimo){
            System.out.println("Not prime");
        }else{
            System.out.println("Prime");
        }
    
        }
    
    }
        }
    

    }

  • + 0 comments
    import math
    
    def is_prime(n: int) -> str:
        if n == 1:
            return "Not prime"
        ans = (
            next(
                filter(
                    lambda x: n % x == 0,
                    (div for div in range(2, math.floor(math.sqrt(n) + 1)))
                ), None
            )
        )
        return "Prime" if ans is None else "Not prime"
        
    tests = [int(input()) for i in range(int(input()))]
    for test in tests:
        print(is_prime(test))
    
  • + 0 comments

    Java Solution

     Scanner sc = new Scanner(System.in);
            int n = sc.nextInt();
            for(int j=n;j>0;j--)
            {
                int a = sc.nextInt();
                boolean pr = true;
                if(a==1)
                {
                    System.out.println("Not prime");
                    continue;
                }
                for(int i=2;i<=a/2;i++)
                {
                    if(a%i==0)
                    {
                        pr = false;
                        System.out.println("Not prime");
                        break;
                    }
                }
                if(pr==true)
                {
                        System.out.println("Prime");
                    
                }
            }
     
    
  • + 0 comments

    JavaScript Solution

    function processData(input) {
        let data = input.split(/\n/);  
        data = data.slice(1);  
    
        data.forEach(number => {  
            let n = parseInt(number);
            if (isNaN(n)) return;  
    
            if (isPrime(n)) {
                console.log("Prime");
            } else {
                console.log("Not prime");
            }
        });
    }
    
    function isPrime(num) {
        if (num <= 1) return false;
        if (num <= 3) return true;
        if (num % 2 === 0 || num % 3 === 0) return false;
    
        for (let i = 5; i * i <= num; i += 6) {
            if (num % i === 0 || num % (i + 2) === 0) return false;
        }
        return true;
    }
    
  • + 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));
        }
    }
    

    } `