Day 25: Running Time and Complexity

  • + 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;
    }