DEV Community

Discussion on: Monday Express BigO Notation [Day 1]

Collapse
 
miami profile image
Larry Miami

Done!I have 3 evaluations at first "n==1 (not prime) or if n==2 (for even numbers) and (n%2)==0, if this is true, returns 'Not prime'."Then we loop from 3 to sqrt(n),counter i increases by 2 because we don't have to check for even numbers.And if n% counter i == 0 the number is not a prime.After all the evaluations,we are left with prime numbers,does that make sense?

function primality(n) {

    if((n==1)|(n%2)==0 && n!=2 ){
        return 'Not prime';
    }

    for(let i = 3; i<=Math.sqrt(n);i+=2){
            console.log(i);
            if((n%i)==0){
                return 'Not prime';
            }
    }

    return 'Prime';

}

Collapse
 
cliffgor profile image
Cliff Gor

awesome Miami Larry for this awesome explanation of your solution