Finding Primes in Python with the Sieve of Eratosthenes

แชร์
ฝัง
  • เผยแพร่เมื่อ 27 มิ.ย. 2024
  • Implement this algorithm for finding primes in Python.
    ― mCoding with James Murphy (mcoding.io)
    Source code: github.com/mCodingLLC/VideosS...
    SUPPORT ME ⭐
    ---------------------------------------------------
    Patreon: / mcoding
    Paypal: www.paypal.com/donate/?hosted...
    Other donations: mcoding.io/donate
    BE ACTIVE IN MY COMMUNITY 😄
    ---------------------------------------------------
    Discord: / discord
    Github: github.com/mCodingLLC/
    Reddit: / mcoding
    Facebook: / james.mcoding
    MUSIC
    ---------------------------------------------------
    Track: Stardust - JayJen [Audio Library Release]
    Music provided by Audio Library Plus
    Watch: • Stardust - JayJen | Fr...
    Free Download / Stream: alplus.io/stardust
  • วิทยาศาสตร์และเทคโนโลยี

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

  • @SirCorrino
    @SirCorrino 3 ปีที่แล้ว +56

    One little issue I had when trying higher numbers. range(2, isqrt(n)) stops too early.
    For n = 1000, isqrt(n) = 31, which is prime. But since we don't include the stop value of range(), we don't mark 961 as False, giving an erroneous answer.
    The fix is easy, we stop at isqrt(n)+1 instead.

    • @OlliS71
      @OlliS71 6 หลายเดือนก่อน

      You have to stop at ceil( sqrt( n ) ).

  • @0xfrijolito
    @0xfrijolito 3 ปีที่แล้ว +54

    OMG x = [True] * n makes a list of Truths with a len of 10, this is the most useful thing I have learned in my life

    • @robbert-janmerk6783
      @robbert-janmerk6783 3 ปีที่แล้ว +4

      You can also do the same thing with strings, like "a" * 4 == "aaaa" :)

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

      Be careful with mutable types, though. Here is an example.
      >>> xs = [[]] * 3
      >>> xs
      [[], [], []]
      >>> xs[0].append(42)
      >>> xs
      [[42], [42], [42]]
      Basically, all elements of xs are the same list. To avoid this, you can write xs = [[] for _ in range(3)].

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

      same, bro. Whenever I have to do that my ctrl and V buttons cower in fear. Advanced stuff.

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

      @@abt8601 is this a bug or a feature. I cannot think of an example where this behavior could come handy.

  • @bradleyshrader7758
    @bradleyshrader7758 3 ปีที่แล้ว +24

    Great video, but I believe the range on "i" go to "isqrt(n) + 1" since the square of a prime is a composite number which has only two factors that are both equal to "isqrt(n)". If "n = p*p + 1" where p is a prime I believe the code you've shown here will incorrectly mark n-1 as prime (e.g. n=10 will return 9 as a prime).

    • @mCoding
      @mCoding  3 ปีที่แล้ว +18

      You are absolutely correct. If n happens to be one more than a square of a prime my code gives the wrong answer! Please see my updated prime sieve video that does not suffer from this bug!

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

      Personally I skip root functions all together here and use a while:
      while i**2

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

      @@oida10000 o wise man!

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

    Your 2 minute explanation was enough, thank you

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

    Great Explanation!
    Cheers!

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

      Glad it was helpful!

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

    thanks. i am a huge fan of yours from india. your coding lessons are very good

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

    Really Great content! Hope you keep making these kind of videos!

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

      Thanks for the support!

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

      @@mCoding Hello! Do you have any playlists over project Euler problems? I am beginner in python and some of these problems are difficult. If you have that'll be great!

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

      Not yet, only so much time in the day. If you have a specific problem you'd like to see, I take requests!

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

      @@mCoding yeah man I need your help! I am stuck on problem 11 of project Euler. I cannot share link because TH-cam's deleting it.
      Plus sir can you explainme how this block of code returns false for num=2( this code finds prime num. Ik 2 is a prime number lol but 2%2==0. Kinda confused here)
      def Prime_num(num):
      for i in range(2,int(num ** 0.5)+1):
      if num % i == 0:
      return False
      return True

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

      Also thanks for helping

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

    Man your video is awesome hope to see more from you like this

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

      Thanks! More on the way...

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

      @@mCoding Can you do videos on complexity of various python functions and which one is best for some common Problems?

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

      @@achuthkarthik4689 Do you have any problems in mind that you'd like to see?

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

      @@mCoding I got a simple problem to find maximum sum of increasing range in an array though i solved it i need a more efficient algorithm.

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

      If you can post a link to the problem description, I'll take a look.

  • @aynuayex
    @aynuayex 7 หลายเดือนก่อน +1

    here is a simplified one for the loop causing errors without a need to import the library
    for i in range(2, int(n ** 0.5) + 1):
    the int method always returns the integer part of the fraction always.

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

    Great Video Dr Murphy
    I think NumPy can help speed it up as follows:
    import numpy as np
    n = int(1e6) # can be made into a function of course
    A = np.ones(n+1, dtype='int8')
    for i in range(2, int(n**0.5)+1):
    if A[i]:
    A[i**2::i] = 0
    np.flatnonzero(A)[2:] # can be converted to a list
    Would appreciate your thoughts on this.

    • @user-ld8lc4ex4m
      @user-ld8lc4ex4m 10 หลายเดือนก่อน

      I'm just wondering what is the time complexity of this code?

  • @Taha-fw7ey
    @Taha-fw7ey 3 ปีที่แล้ว +2

    Great video!
    Keep it up 😉

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

      Thank you! Let me know if there are any topics you would like me to cover!

    • @Taha-fw7ey
      @Taha-fw7ey 3 ปีที่แล้ว +1

      @@mCoding my friends and I are just getting into competitive programming
      So it would really nice to cover topic like ( Combinatorics problems, Prefix sum, Number of divisors, Graphs...) I will make sure that ur channel reach as much people as I know 😉

  • @user-ld8lc4ex4m
    @user-ld8lc4ex4m 11 หลายเดือนก่อน

    Thank you

  • @re.liable
    @re.liable 3 ปีที่แล้ว +3

    I suppose it doesn't really matter much but the function having two lists bugs me hahaha
    I see you made another video about this, I'll check it out

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

    I cant find this code in your github. Whats the name of it?

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

    What is the time complexity of the algorithm in here ?

  • @marcindj3411
    @marcindj3411 10 หลายเดือนก่อน

    How long it takes to find all primes in range (0 - 1 000 000 000)?

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

    Hi when I run the code the error: TypeError: 'type' object is not subscriptable occures. So python doesnt recognizes the list() function ???
    Any help? Thx

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

      when i run it on Replit website it gaves me the same error as you but on my local computer it worked well.

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

    Trying to remember Sieve_of_Eratosthenes as a reference name would cause explosion

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

    I am new and copied the same code but it is not working is_prime = [True] * n this portion is not working, it is showing "Unindent does not match any outer indentation level" Please help me with the following.. :(

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

      This means somewhere in you file your indentation is not correct, probably a stray tab or space somewhere.

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

    Well, i got the wrong output
    from math import isqrt
    def iss(n):
    if n

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

      if the num == 5 it returns 4 yet not a prime

    • @SAROJKUMAR-xe8vm
      @SAROJKUMAR-xe8vm 7 หลายเดือนก่อน

      USE
      for i in range(2,isqrt(n)+1):

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

    Sir, I have a problem which I want to solve with the help of python but I am not an expert in python please help me?

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

    but 2 isn't prime as u said...
    Is_prime[2]=False?

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

      2 is a prime number :)

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

      @@mCoding you should have the base case

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

      @@toomuchespresso13 This video was a long time ago but if I recall correctly the function returns the primes less than the input, not less than or equal to the input, so the base case should indeed be

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

    improve explaining the code line by line