Efficient Prime Numbers in Python

แชร์
ฝัง
  • เผยแพร่เมื่อ 28 ก.ย. 2024
  • This video shows how to write an efficient isPrime function in Python for determining if a number is prime or not.

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

  • @marcoshenriques3118
    @marcoshenriques3118 ปีที่แล้ว +10

    All prime numbers > 3 have either the form 6k+5 (k>=0) or 6k+1 (k>=1). Therefore you can filter the "numbers beyond all primes list", and you get rid of 2/3 of occurrences. I tested many prime programs in python and i have many ideas. I can generate all primes up to 100000 in 0.022 secs and detect primes of 17 digits in only 7.23 secs.

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

      please share your code, I am exciting to try

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

      I learned something new today. Thank you.

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

      Could you mention your machine

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

      I suggest doing something slightly different, too. Instead of keeping a list of old primes, use sets instead. You can keep track of your largest and most recent prime as a separate variable. Using sets may speed things up a bit more since the access time is faster. But then again, if your primes are out of order this could hurt your time as well. Im not sure; havent tested this.
      There are more efficient ways of generating a list of primes, still. The Sieve of Eratosthenes can be coded in Python fairly efficiently and is much faster than these brute force generators. Even though I guess it is a kind of brute force generator in its own right, it takes a slightly different approach.
      As for checking for the primality of a specific integer, there are much more efficient algorithms such as the Miller-Rabin test, and others.
      If you really want to have a coding adventure in python to learn a lot about math and primes and efficient coding, try building integer factorization algorithms. Talk about a challenge! The theory and the practice are lightyears above.

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

    Dude that's better than expected

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

    nice work ... primes are underrated lol

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

    damn, pretty nice

  • @yoni1991
    @yoni1991 8 หลายเดือนก่อน

    Confused 😂😂😂 me.wow

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

    efficient?

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

    you only have to loop through the range 2,x//2+1

  • @Demha96
    @Demha96 9 หลายเดือนก่อน

    but 2 is a prime number

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

    Good but speaking so fast

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

      Sorry about that. You can slow down the video under settings through the gear icon on youtube.

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

      def sieve_of_atkin(limit):
      P = [2, 3]
      sieve = [False] * (limit + 1)
      for x in range(1, int(limit**0.5) + 1):
      for y in range(1, int(limit**0.5) + 1):
      n = 4 * x**2 + y**2
      if n

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

      Fastest algorithm

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

    wonderful !! :)