Count Primes

แชร์
ฝัง

ความคิดเห็น • 76

  • @KevinNaughtonJr
    @KevinNaughtonJr  5 ปีที่แล้ว +44

    ERATOSTHENES WAS THE MAN

    • @blasttrash
      @blasttrash 5 ปีที่แล้ว +2

      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 ปีที่แล้ว +78

    Let me check if it works-->
    Normal Person: hits "Run"
    Kevin: hits "Submit"

    • @KevinNaughtonJr
      @KevinNaughtonJr  4 ปีที่แล้ว +8

      hahahahah

    • @Spectraevil
      @Spectraevil 4 ปีที่แล้ว +6

      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 2 ปีที่แล้ว +7

    This is basically just a working solution. No explanation or complexity analysis.

  • @shalinisil9214
    @shalinisil9214 3 ปีที่แล้ว +19

    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 ปีที่แล้ว +23

    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 ปีที่แล้ว +11

    There is no explanation here whatsoever, only text in the video description helps

  • @mdsujan6686
    @mdsujan6686 2 ปีที่แล้ว +7

    Loved the explanation. But how is it faster than O(n^2)?

    • @cellodabest
      @cellodabest 2 ปีที่แล้ว +1

      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)?

  • @gyu-wonan7685
    @gyu-wonan7685 2 ปีที่แล้ว +1

    this guy shoots tutorials as if hes shooting an apologies video

  • @DurgaShiva7574
    @DurgaShiva7574 2 ปีที่แล้ว +1

    Can anyone please guide why we did i*i into the 1st loop..why didn't we simply check for I

  • @deeja-y
    @deeja-y 3 ปีที่แล้ว +2

    For anyone confused about i*i part, watch th-cam.com/video/T0XbxCYLBmc/w-d-xo.html (no affiliation, I was one of the confused ones that googled around)

    • @Chloe-si2hq
      @Chloe-si2hq ปีที่แล้ว

      Thank you!!! This link you provided is very helpful!!

  • @blasterzm
    @blasterzm 5 ปีที่แล้ว +8

    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

  • @RetroGamingClashOfClans
    @RetroGamingClashOfClans 4 ปีที่แล้ว +2

    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

  • @antoniokhan9367
    @antoniokhan9367 2 ปีที่แล้ว +1

    Thank You So Much Handsome Person!

  • @karljay7473
    @karljay7473 5 ปีที่แล้ว +3

    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.

  • @macleanpinto6406
    @macleanpinto6406 3 ปีที่แล้ว +2

    Can you explain the time complexity of this solution

  • @qaipak1
    @qaipak1 4 ปีที่แล้ว +8

    Could you explain why the check is only till i*i? I'm confused about that.

    • @dishantsheth9592
      @dishantsheth9592 4 ปีที่แล้ว +25

      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 3 ปีที่แล้ว +2

    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 ปีที่แล้ว +1

      Yes. Calculate primes upto square root of the number you seek, then use that to calculate the desired prime number.

  • @Chloe-si2hq
    @Chloe-si2hq ปีที่แล้ว

    Kevin, I submitted this approach on leetcode and it still said Time Limit Exceeded :(

  • @smirkedShoe
    @smirkedShoe 4 ปีที่แล้ว +2

    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

  • @susheeldwivedi5916
    @susheeldwivedi5916 5 ปีที่แล้ว +1

    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 :)

  • @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.

  • @saeeduchiha5537
    @saeeduchiha5537 4 ปีที่แล้ว +5

    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)

  • @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 ปีที่แล้ว +1

      @@Endlessvoidsutidos Thanks

    • @Endlessvoidsutidos
      @Endlessvoidsutidos 4 ปีที่แล้ว +1

      @@rahulratra6672 np was confused on this myself till I spent some time digging into it

  • @WhatIsThisAllAbout
    @WhatIsThisAllAbout 3 ปีที่แล้ว +2

    i wtched 4-5 times, still not sure why its that way

  • @shelcyshajithekkedathu8498
    @shelcyshajithekkedathu8498 ปีที่แล้ว

    Time Limit Exceeded

  • @arnoclaude317
    @arnoclaude317 4 ปีที่แล้ว +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 11 หลายเดือนก่อน

      ofc

  • @indiansoftwareengineer4899
    @indiansoftwareengineer4899 5 ปีที่แล้ว +2

    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.

  • @59sharmanalin
    @59sharmanalin 2 ปีที่แล้ว

    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)

  • @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

  • @beonthego8725
    @beonthego8725 ปีที่แล้ว +1

    you have beautiful eyes

  • @harshalpatil7173
    @harshalpatil7173 4 ปีที่แล้ว

    can this be solved by dynammic programming ?

  • @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

  • @joesyriac5278
    @joesyriac5278 3 ปีที่แล้ว

    Very poor explanation why i*i < primes.length. And you very conveniently skipped that in description part too

  • @emmanuelkehinde
    @emmanuelkehinde 4 ปีที่แล้ว

    Awesome!

  • @rak590
    @rak590 4 ปีที่แล้ว +2

    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

  • @Endlessvoidsutidos
    @Endlessvoidsutidos 4 ปีที่แล้ว +2

    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

  • @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.

  • @ajourney179
    @ajourney179 4 ปีที่แล้ว

    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 4 ปีที่แล้ว +3

      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

  • @devanshsolani2711
    @devanshsolani2711 3 ปีที่แล้ว

    The way you solve these problems is so Effortless

    • @ThiagoRibeiroo
      @ThiagoRibeiroo 2 ปีที่แล้ว

      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 ปีที่แล้ว +1

      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 ปีที่แล้ว +1

      @@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