JavaScript Algorithms - 9 - Prime Number

preview_player
Показать описание

📱 Follow Codevolution

Prime Number Algorithm
JavaScript Algorithms
Algorithms with JavaScript
Рекомендации по теме
Комментарии
Автор

Thanks a lot for the lessons. Anticipating for the next class.

ogucharles
Автор

Wow the optimize way of doing it really humbled me down. I learnt that there's always more optimize and better way of writing code.

xitri
Автор

Could you please Zoom in replit for next video? My eye needs to zoom in for that theme

ajayraja
Автор

Thanks, sir, you deserve 1Million plus subscribers

gabrieludo
Автор

Hello. Thank you very much for your videos. I learn algorithms with your playlist. And also recording videos with this progress and with my solves.

veryhightower
Автор

😂Never seen a more complicated definition of prime numbers

jeremiahojo
Автор

The square root trick is interesting, but does it actually optimize the function? The first instance where n % i === 0 will trigger the return statement, so all future loops will not run.

roebucksruin
Автор

the % is not "devide by" it's "remainder" the devide by operator is this "/". They are not the same

night-knight_
Автор

What if for example we put 1 as the parameter...first if statement is success and we return false...would it be then consant O(1) time complexity?

davidpokrajac
Автор

function isPrime(n) {
if (n === 2) {
return true;
}
for (let i = 2; i < n; i++) {
if (n % i === 0) {
return false;
}
}
return true;
}

abhisheksharma
Автор

function isPrime(n) {
if (n < 2) return false
else if (n % 2 !== 0 || n === 2) return true

return false
}

CrouTonG
Автор

Second solution would be more optimal if the loop wasn't stopped by return, otherway math function only adds extra calculations, both solutions are at most O(sqrt(n))

piotrekjazz
Автор

Sir, the value of 'this' is lexical(static) scoped or dynamic scoped? I have a confusion. I think it's dynamic scoped because it gets its value through execution context. But I have already learned, JavaScript only uses lexical scope

damo
Автор

can't i do in for loop i < (n / 2 + 1);
this will reduce our steps

manishaggarwal
Автор

function isPrime(n) {
let cont = 0
for (let i=1; i<=n; i++) {
if(n%i == 0){
cont++
}
}
return cont == 2
}

nicolauk
Автор

Hey I have a little confusion in the code..When inside the for loop, I use the if condition for n%i===0 and then inside the for loop only I use the return true in an else condition, it returns wrong values for numbers like 2 and 55..but works perfect if I do as the tutorial...any idea where I am doing wrong?

Avatar-Roku
Автор

And i think we can check if n > 2 && isEven then we return false because all Prime numbers except for 2 are Odd.

oussamahamdi
Автор

Amazing tutorial as always expect ; thanks a lot but I have bug in prime(2)

demno
Автор

function primeNumber(n){
for (i = 2; i < n; i++){
if (n % i || n===2){
return ' Prime Number'
}
else {
return ' Not a Prime Number'
}
}
}

ojooladimeji
Автор

second solution does not work like if you put 9 then it will check only up to 3 and 3 is prime number and 9 is not prime number

reactworld