Тёмный

Count Primes 

Kevin Naughton Jr.
Подписаться 138 тыс.
Просмотров 61 тыс.
50% 1

Опубликовано:

 

23 окт 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 76   
@KevinNaughtonJr
@KevinNaughtonJr 5 лет назад
ERATOSTHENES WAS THE MAN
@blasttrash
@blasttrash 5 лет назад
lol, of course coz he was not a WOMAN. see negated the logic like you told at the end. :P
@djfedor
@djfedor 5 лет назад
Algorithm's authorship appears to belong to David Gries, Jayadev Misra. A Linear Sieve Algorithm for Finding Prime Numbers [1978] :)
@algorithmimplementer415
@algorithmimplementer415 5 лет назад
What is the time complexity?
@dipakkumarmandal7248
@dipakkumarmandal7248 4 года назад
@@algorithmimplementer415 on log log n
@madhuvamsimachavarapu5267
@madhuvamsimachavarapu5267 4 года назад
Let me check if it works--> Normal Person: hits "Run" Kevin: hits "Submit"
@KevinNaughtonJr
@KevinNaughtonJr 4 года назад
hahahahah
@Spectraevil
@Spectraevil 4 года назад
I think sometimes he tries and submits the ques beforehand 1 time before making the video. I have seen "Submitted 5 minutes : Accepted" from in the previous submission list a few times. xD Btw not saying it in a bad way, no one likes to do retakes for videos :| - I'm a big fan
@ritikmishra3643
@ritikmishra3643 4 года назад
@@Spectraevil haha I think that's the reason why he moved his facecam video on top of it. Smart!
@AsifIqbalR
@AsifIqbalR 3 года назад
This is basically just a working solution. No explanation or complexity analysis.
@shalinisil9214
@shalinisil9214 3 года назад
There are indeed prime numbers greater than the number in its square. The smallest prime factor of a composite number n is less than or equal to root(n). So there is no point in checking further than the number as those get covered in the next numbers in the list and reduces complexity.
@ianchui
@ianchui 5 лет назад
I hate math problems too, I was working on this today. and got pretty frustrated that I couldn't solve an 'easy' question /:
@Makwayne
@Makwayne 2 года назад
it's medium now
@annawilson3824
@annawilson3824 4 года назад
There is no explanation here whatsoever, only text in the video description helps
@ravastarasdasd9810
@ravastarasdasd9810 3 года назад
for real, what a terrible explanation
@gyu-wonan7685
@gyu-wonan7685 2 года назад
this guy shoots tutorials as if hes shooting an apologies video
@DurgaShiva7574
@DurgaShiva7574 2 года назад
Can anyone please guide why we did i*i into the 1st loop..why didn't we simply check for I
@mdsujan6686
@mdsujan6686 3 года назад
Loved the explanation. But how is it faster than O(n^2)?
@cellodabest
@cellodabest 2 года назад
I think because the for loops aren’t nested, that way the program doesn’t reiterate over the same dataset
@GiridharaSaiPavanKumarGurram
@GiridharaSaiPavanKumarGurram 2 года назад
because there is an 'if'
@marcyanus1430
@marcyanus1430 2 года назад
how is the naieve solution of checking each number below n, O(n^2) anyway? why am I mistaken when i think it's O(n)?
@RetroGamingClashOfClans
@RetroGamingClashOfClans 4 года назад
wouldnt be better checking all the numbers from 2 to half of the upper bound.. so 2 to n/2.. cause if u think about it, the factors must be less than or equal to half of the upper bound.. cause if it is bigger than n/2... (2 * factor ) > n
@blasterzm
@blasterzm 5 лет назад
You didn't explain why this solution is faster than the O(n sqrt n) basic solution. Basically you have the sum n + n/2 + n/3 + n/5 +... + n/prime With prime
@antoniokhan9367
@antoniokhan9367 2 года назад
Thank You So Much Handsome Person!
@deeja-y
@deeja-y 3 года назад
For anyone confused about i*i part, watch ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-T0XbxCYLBmc.html (no affiliation, I was one of the confused ones that googled around)
@Chloe-si2hq
@Chloe-si2hq 2 года назад
Thank you!!! This link you provided is very helpful!!
@beonthego8725
@beonthego8725 Год назад
you have beautiful eyes
@rahulratra6672
@rahulratra6672 4 года назад
hey kevin i know it must be a silly doubt to as but could you please elaborate why the first for loop has i*i
@Endlessvoidsutidos
@Endlessvoidsutidos 4 года назад
think of it as an if statement to check if the value of the current int squared is less than the value of n which is also the length of your primes array I think using n here makes more sense and involves less typing
@rahulratra6672
@rahulratra6672 4 года назад
@@Endlessvoidsutidos Thanks
@Endlessvoidsutidos
@Endlessvoidsutidos 4 года назад
@@rahulratra6672 np was confused on this myself till I spent some time digging into it
@karljay7473
@karljay7473 5 лет назад
Couldn't you also not do the count and just count how many you turned to true? In other words, if you have 20 elements in the array, you turn 15 of them from false to true, then you know the count would be 20 - 15, or 5. Saves that extra loop.
@susheeldwivedi5916
@susheeldwivedi5916 5 лет назад
I think there is also no need for counting loop because when we go for negation in first loop that time we increment primeCount. may be Kevin I'm wrong :)
@ethanengland6186
@ethanengland6186 3 года назад
I was thinking the same
@macleanpinto6406
@macleanpinto6406 3 года назад
Can you explain the time complexity of this solution
@59sharmanalin
@59sharmanalin 3 года назад
For those who are confused about i*i, let's consider-: 1. non prime number let's say 42 factors are 2*21(both are factors), 3*14, 6*7, 7*6, 14*3, 21*2 note these factors start repeating identically after square root i.e 6 (6*6) for numbers that are not prime and not multiple of primes, we would quickly find factor and break but for prime * prime type numbers we have to go till square root ex (11*13) (go till 12* 12) 2. prime number lets say 31 if this number was non prime based on factors done in step 1, we would have found something till square root of the number since, we didn't find we assume it to be prime, so ideally the analogy of factor is taken from non prime and applied to all numbers but it will optimised the prime and prime * prime type numbers because we stop at square root Complexity - O(sq1 + sq2 + ...sqN)
@saeeduchiha5537
@saeeduchiha5537 4 года назад
Coming from leetcode, knowing the solution already, this doesn't add much to it! ...I thought you could focus more on the non-obvious parts (e.g. i*i < n)
@smirkedShoe
@smirkedShoe 4 года назад
Hey Kevin, love your work. Great job. Its been a while I have been trying to learn dp and I always get stuck in the resources I should use and where should I start. It would be great if you could make some to help us
@KevinNaughtonJr
@KevinNaughtonJr 4 года назад
Thanks! I'll see what I can do :)
@smirkedShoe
@smirkedShoe 4 года назад
@@KevinNaughtonJr thanks Kevin.
@KevinNaughtonJr
@KevinNaughtonJr 4 года назад
Naman Gupta anytime
@shelcyshajithekkedathu8498
@shelcyshajithekkedathu8498 2 года назад
Time Limit Exceeded
@qaipak1
@qaipak1 4 года назад
Could you explain why the check is only till i*i? I'm confused about that.
@dishantsheth9592
@dishantsheth9592 4 года назад
Any non-prime number will always have a factor that is less than or equal to its square root. For example, pick 36. It's factors are - 1*36, 2*18, 3*12, 4*9, 6*6. In each case, there is at least one number satisfying the above condition. So here, by considering all numbers up to the square root of n, we can successfully eliminate the non-prime numbers up to n. Hope this helps.
@mateus-996
@mateus-996 4 года назад
This could easily exceed the heap size, is there any way to overcome that without actually increasing heap size or physical memory?
@simpletongeek
@simpletongeek 3 года назад
Yes. Calculate primes upto square root of the number you seek, then use that to calculate the desired prime number.
@sekhardutta3333
@sekhardutta3333 3 года назад
Hi Kevin, This was a great solution, but i was not able to understand line #9 (if(prime[i])), I understood that its optimizing the solution but not able to understand mathematically. Could you please explain with some example.
@joesyriac5278
@joesyriac5278 3 года назад
Very poor explanation why i*i < primes.length. And you very conveniently skipped that in description part too
@Chloe-si2hq
@Chloe-si2hq 2 года назад
Kevin, I submitted this approach on leetcode and it still said Time Limit Exceeded :(
@WhatIsThisAllAbout
@WhatIsThisAllAbout 3 года назад
i wtched 4-5 times, still not sure why its that way
@the_real_cookiez
@the_real_cookiez 3 года назад
I think I'm dumb, but why wouldn't looping 0, n and checking if each number in between is a prime number work? I tried this but it would only pass some test cases. Can't figure out why.
@Makwayne
@Makwayne 2 года назад
0 and 1 are not primes
@Endlessvoidsutidos
@Endlessvoidsutidos 4 года назад
think I just sieve of Eratosthenesed IN MY PANTS thanks for another great solution actually was able to get this one brute force styles but I was like mannn dis is ugly let's see what Kevin got up his sleeve was not disappointed lols
@NdaJret
@NdaJret 5 лет назад
Hey man can you do shortest Subarray with Sum at Least K? Leetcode 862. I don’t understand the solution the gave. The brute force is just too slow
@doloreshaze10
@doloreshaze10 3 года назад
does ' if primes[i] ' mean if the primes array has the value i?
@raaghavaadithya
@raaghavaadithya 3 года назад
primes[i] is a boolean array. if(primes[i]) has the same meaning as if(primes[i] == true) or != false. this is only for boolean values.
@indiansoftwareengineer4899
@indiansoftwareengineer4899 5 лет назад
Bro, thanks for videos. Where is first Leetcode problem eg. problem number 1, this is problem no 204, I have to start from first, please do help.
@ajr1791ze
@ajr1791ze 5 лет назад
Hello, how you know that this problem was asked in google? can you tell more such problems which are not on leetcode. How to practice for google and other companies interviews.
@Cr4zyVib3
@Cr4zyVib3 5 лет назад
He knows because LeetCode premium will tell you which companies ask the question under the "companies" tab of the question
@anikkumarroy1102
@anikkumarroy1102 3 года назад
Why are we using i*I again
@arnoclaude317
@arnoclaude317 4 года назад
The bruteforce method is actually super easy but leetcode won't accept my submission because of its n^2 runtime. Do you think most interviewers would accept the bruteforce method but then ask you to think about a potential better solution?
@Rob-J-BJJ
@Rob-J-BJJ Год назад
ofc
@emmanuelkehinde
@emmanuelkehinde 4 года назад
Awesome!
@harshalpatil7173
@harshalpatil7173 4 года назад
can this be solved by dynammic programming ?
@rak590
@rak590 4 года назад
what is the time complexity of this solution?
@thedevsaddam
@thedevsaddam 4 года назад
time complexity is O(n log log n) and space complexity is O(n), you know memory is cheaper :D
@devanshsolani2711
@devanshsolani2711 3 года назад
The way you solve these problems is so Effortless
@ThiagoRibeiroo
@ThiagoRibeiroo 3 года назад
He DOES NOT solve them while recording it...I don't understand why people think he does lolol he learns about the problem, gets to a solution first, plans an explanation, and THEN records the video.
@yicai7
@yicai7 4 года назад
How can we come up with this awesome solution?
@KevinNaughtonJr
@KevinNaughtonJr 4 года назад
Hopefully this video helped!
@treyquattro
@treyquattro 4 года назад
just one of those things that as a software engineer you need to know. Alternatively be a mathematical genius...
@yicai7
@yicai7 4 года назад
@@treyquattro Got it! Thanks bud!
@4747surya
@4747surya 4 года назад
👍
@lifeofme3172
@lifeofme3172 3 года назад
I don't get this problem weird
@hika5564
@hika5564 3 года назад
Dude your videos are consistently unintuitive and explanations are bad -- i go to other videos and understand the solution in 30s
Далее
Reorganize String
12:44
Просмотров 78 тыс.
L6. Sieve of Eratosthenes | Maths Playlist
18:27
Просмотров 54 тыс.
Paint Projects
00:17
Просмотров 5 млн
3 лайфхака для УШМ
01:00
Просмотров 37 тыс.
Boats to Save People
7:06
Просмотров 20 тыс.
An Entire Computer Science Degree in 11 Minutes
11:13
Просмотров 834 тыс.
The longest mathematical proof ever
19:30
Просмотров 59 тыс.
Next Closest Time
8:47
Просмотров 32 тыс.
Someone improved my code by 40,832,277,770%
28:47
Просмотров 2,6 млн
String Compression
11:48
Просмотров 94 тыс.
Unique Paths
7:07
Просмотров 60 тыс.
The HARSH Reality of Working in Big Tech
8:42
Просмотров 22 тыс.