Python and Prime Numbers || Python Tutorial || Learn Python Programming

///Python and Prime Numbers || Python Tutorial || Learn Python Programming

Python and Prime Numbers || Python Tutorial || Learn Python Programming

FavoriteLoadingAdd to favorites

Today we use Python to identify prime numbers. This is important in cryptography and number theory. While it’s simple to make the function work, it’s hard to make it FAST. We’ll profile the function and look at several ways to improve the speed of our algorithm.

Our Patrons on Patreon help us make our videos! We’d like to recognize our VIP Patron Vishal Shah whose generosity has made such a big difference for our channel. Thank you, Vishal!

➢➢➢➢➢➢➢➢➢➢
To learn Python, you can watch our playlist from the beginning:

➢➢➢➢➢➢➢➢➢➢
If you’d like a reference book, we recommend “Python Cookbook, 3rd Edition” from O’Reilly:

The Mythical Man Month – Essays on Software Engineering & Project Management

➢➢➢➢➢➢➢➢➢➢
To​ ​help​ ​us​ ​make​ ​videos more quickly,​ ​you​ ​can​ ​support​ Socratica at:

​Patreon​:

Socratica Paypal:

We also accept Bitcoin! 🙂
Our​ ​address​ ​is: 1EttYyGwJmpy9bLY2UcmEqMJuBfaZ1HdG9

Thank​ ​you!!

➢➢➢➢➢➢➢➢➢➢
You​ ​can​ ​also​ ​follow​ ​Socratica​ ​on:
-​ ​Twitter:​ ​@socratica
-​ ​Instagram:​ ​@SocraticaStudios
-​ ​Facebook:​ ​@SocraticaStudios

➢➢➢➢➢➢➢➢➢➢
Python instructor: Ulka Simone Mohanty (@ulkam on Twitter)
Written & Produced by Michael Harrison (@mlh496 on Twitter)

source

By |2019-10-18T23:26:37+00:00October 18th, 2019|Python Video Tutorials|41 Comments

41 Comments

  1. Socratica October 18, 2019 at 11:26 pm - Reply

    It's official! The Socratica Python Kickstarter was a success! Thank you to all of our supporters. Because of you, many more Python videos coming soon!! 💜🦉

  2. utpal dutta October 18, 2019 at 11:26 pm - Reply

    I am learning python for the last few months and your videos are one of the best among all the available videos on internet.

    Thank you very much and wish you guys all the very best.

  3. Socratica October 18, 2019 at 11:26 pm - Reply

    HEEELLLLLLP! http://bit.ly/PythonKickstarter

    DEADLINE SUNDAY OCT 20th

  4. Socratica October 18, 2019 at 11:26 pm - Reply

    We're halfway there. We still need your help! Support Socratica Python Kickstarter: http://bit.ly/PythonKickstarter

  5. Bauyrzhan Kadauov October 18, 2019 at 11:26 pm - Reply

    thanks for such a great explanation!

  6. Nico Robin October 18, 2019 at 11:26 pm - Reply

    omg, I like her presentation, not only cleared the vague of the subject but also presented it in a sie-fi way, lol
    good job <3

  7. Aleš Pecka October 18, 2019 at 11:26 pm - Reply

    Does anyone have an idea why using filter in this case is slower than a for-loop? If you try the following function, which replaces the last for-loop with a filter, you will find out that it performs considerably worse. Also, why don't filter use multiple threads by default? It feels like it is build for it.
    def isPrime(num):
    if num == 1:
    return False

    if num % 2 == 0:
    if num == 2:
    return True
    return False

    end = int(math.sqrt(num))
    factors = filter(lambda i: num % i == 0, range(3, end+1, 2))

    return not bool(len(list(factors)))

  8. Bhagya Laxmi Sahoo October 18, 2019 at 11:26 pm - Reply

    I want to get answer to this question
    A program to take a number as input then display the sum of its factors

  9. Mustafa Kalafat October 18, 2019 at 11:26 pm - Reply

    will next step be to try only the prime numbers as divisors?

  10. Rohit Dhage October 18, 2019 at 11:26 pm - Reply

    why not use break statement and make it much faster? As soon as you find a number which it divides successfully apart from 1, then its not a prime.

  11. Socratica October 18, 2019 at 11:26 pm - Reply

    Support what you love! Socratica has a Kickstarter to make more Python: http://bit.ly/PythonKickstarter

  12. Frank Willeke October 18, 2019 at 11:26 pm - Reply

    Sieve of Eratosthenes is still faster than v3 if properly implemented 😉

  13. calvin atuhaire October 18, 2019 at 11:26 pm - Reply

    i hope you are ready, cause its PRIMETIME

  14. Robert Alaverdyan October 18, 2019 at 11:26 pm - Reply

    Excellent. I'd highly appreciate to see more of the kind of videos

  15. mugaya maddox October 18, 2019 at 11:26 pm - Reply

    Not working for 2

  16. skilz8098 October 18, 2019 at 11:26 pm - Reply

    Next Up for Socratica: 2 New Series, Building a Full 3D Graphics Engine from Sratching using Vulkan and Creating a Full NES Emulator that can run Crysis!

  17. skilz8098 October 18, 2019 at 11:26 pm - Reply

    When running the speed test of the is_prime() function that is based on the version in the video at 2:12 it took about .5 seconds for 10,000 and about 45 seconds for 100k trials. I would still say this is decent, but this algorithm can be significantly improved; continue watching Sacratica to find out how!

  18. Chris LAM October 18, 2019 at 11:26 pm - Reply

    why do we need round down and add 1 in the range?.. i cant understand

  19. Kimmi G October 18, 2019 at 11:26 pm - Reply

    What if n = 120, the output of the code is:
    2, 11, 13, 17, 19, 23, 25, 29, 31, 35, 37, 41, 43, 47, 49, 53, 55, 59, 61, 65, 67, 71, 73, 77, 79, 83, 85, 89, 91, 95, 97, 101, 103, 107, 109, 113, 115, 119,
    But in fact the real list of prime numbers within 120 is:
    2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113

    So, though your code seems cool, it doesn't really work.

  20. Graham Macleod October 18, 2019 at 11:26 pm - Reply

    That's numberwang.

  21. Kaarthigeyan Rajendran October 18, 2019 at 11:26 pm - Reply

    do some videos on computer vision using python.

  22. Rajat Mehta October 18, 2019 at 11:26 pm - Reply

    your explanation is awsome

  23. Ikk0 M October 18, 2019 at 11:26 pm - Reply

    Can she crash Mr.python by
    🤪😱
    python prime number pendulum?
    like
    bigbanG?

  24. Asante Theophilus October 18, 2019 at 11:26 pm - Reply

    I like the way the woman introduces the topic

  25. Daniel von Bose October 18, 2019 at 11:26 pm - Reply

    only necessary to check divisors whose square is less than the test number.

  26. Warriorx1 Nino October 18, 2019 at 11:26 pm - Reply

    not a good explanation at all. It's hard for beginners to understand it.

  27. Daniel 91953574 October 18, 2019 at 11:26 pm - Reply

    I made a code to print prime numbers in sequence:

    ————————————————————-

    def tp(n):

    for x in lx:

    if(n%x==0):

    return False

    """for i in range(n,1,-1):

    if(1!=i!=n)and(n%i==0):

    return False"""

    return True

    lx=[]

    for i in range(2,10**7):

    if(tp(i)==True):

    lx+=[i]

    print(end="%i, "%i)

    ————————————————————-

    Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:06:47) [MSC v.1914 32 bit (Intel)] on win32

    Type "copyright", "credits" or "license()" for more information.

    >>>

    ===================== RESTART: H:PythonMathprimos.py =====================

    2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997, 1009, 1013, 1019, 1021, 1031, 1033, 1039, 1049, 1051, 1061, 1063, 1069, 1087, 1091, 1093, 1097, 1103, 1109, 1117, 1123, 1129, 1151, 1153, 1163, 1171, 1181, 1187, 1193, 1201, 1213, 1217, 1223, 1229, 1231, 1237, 1249, 1259, 1277, 1279, 1283, 1289, 1291, 1297, 1301, 1303, 1307, 1319, 1321, 1327, 1361, 1367, 1373, 1381, 1399, 1409, 1423, 1427, 1429, 1433, 1439, 1447, 1451, 1453, 1459, 1471, 1481, 1483, 1487, 1489, 1493, 1499, 1511, 1523, 1531, 1543, 1549, 1553, 1559, 1567, 1571, 1579, 1583, 1597, 1601, 1607, 1609, 1613, 1619, 1621, 1627, 1637, 1657, 1663, 1667, 1669, 1693, 1697, 1699, 1709, 1721, 1723, 1733, 1741, 1747, 1753, 1759, 1777, 1783, 1787, 1789, 1801, 1811, 1823, 1831, 1847, 1861, 1867, 1871, 1873, 1877, 1879, 1889, 1901, 1907, 1913, 1931, 1933, 1949, 1951, 1973, 1979, 1987, 1993, 1997, 1999, 2003, 2011, 2017, 2027, 2029, 2039, 2053, 2063, 2069, 2081, 2083, 2087, 2089, 2099, 2111, 2113, 2129, 2131, 2137, 2141, 2143, 2153, 2161, 2179, 2203, 2207, 2213, 2221, 2237, 2239, 2243, 2251, 2267, 2269, 2273, 2281, 2287, 2293, 2297, 2309, 2311, 2333, 2339, 2341, 2347, 2351, 2357, 2371, 2377, 2381, 2383, 2389, 2393, 2399, 2411, 2417, 2423, 2437, 2441, 2447, 2459, 2467, 2473, 2477, 2503, 2521, 2531, 2539, 2543, 2549, 2551, 2557, 2579, 2591, 2593, 2609, 2617, 2621, 2633, 2647, 2657, 2659, 2663, 2671, 2677, 2683, 2687, 2689, 2693, 2699, 2707, 2711, 2713, 2719, 2729, 2731, 2741, 2749, 2753, 2767, 2777, 2789, 2791, 2797, 2801, 2803, 2819, 2833, 2837, 2843, 2851, 2857, 2861, 2879, 2887, 2897, 2903, 2909, 2917, 2927, 2939, 2953, 2957, 2963, 2969, 2971, 2999, 3001, 3011, 3019, 3023, 3037, 3041, 3049, 3061, 3067, 3079, 3083 …

  28. Urmila Gundeli October 18, 2019 at 11:26 pm - Reply

    I really love the way of explaining concept n you made my concepts more clear. This video really helped me a lot.
    Thank you n plz continue making such videos on programming problem solving approaches in efficient way

  29. Luis Valdez October 18, 2019 at 11:26 pm - Reply

    I liked the intro , really futuristic

  30. MachiToons - Machimations October 18, 2019 at 11:26 pm - Reply

    I see many people import math jsut for math.sqrt() which is a bit weird to me.
    Is there a flaw with n**0.5 ?

  31. miad abdi October 18, 2019 at 11:26 pm - Reply

    you can delete statement below
    if n > 2 and n == 2:
    return False
    and instead of that use:
    "for n in range(3, 100000, 2):"
    range(start, stop, step)

    it produced a range of odd numbers ( like this 3, 5, 7, 9, 11, …..)

  32. Mohamed .Bakr October 18, 2019 at 11:26 pm - Reply

    Thank you

  33. Arif Zuhairi October 18, 2019 at 11:26 pm - Reply

    Pretty creepy and serious but entertaining… Great video… Thank you

  34. Alexey Lagunov October 18, 2019 at 11:26 pm - Reply

    math.floor(math.sqrt(n)) + 1 is the same as math.ceil(math.sqrt(n))
    Am I right?

  35. Winict Maximus Cosmo October 18, 2019 at 11:26 pm - Reply

    i feel like i am watching Person of Interest …damn..this is gold

  36. Ghost Hunter October 18, 2019 at 11:26 pm - Reply

    😂😂😂😂

  37. Abhiraj Kale October 18, 2019 at 11:26 pm - Reply

    why does this sound like the lady in hitman?

  38. zola mo October 18, 2019 at 11:26 pm - Reply

    Here is a better way to find primes: The Prime Locomotive.
    The locomotive runs down the number line to the first number/tie. Finds no number there but does nothing because the number one is not a prime number. Continues on to the second number location which is the number 2. Finds nothing there, places the #2 into the prime file, places the #2 on the number line and multiplies it foreword (2,4,6,etc).
    Locomotive continues to the third number location, finds nothing there, places number 3 in the prime folder, places the number three on the number line and multiplies it foreword (3,6,9,). Locomotive continues to the next number location, #4. Finds a number there ( the product of #2 being multiplied foreword) which means that number is not a prime, so it continues on past #4. Continues to the next number location, #5, finds no number there, places #5 in the prime folder, places #5 on the number line and multiplies it foreword (5,10,15,20,etc…). From then on the locomotive continues down the number line and wherever it come across an empty number location it files that number in the prime folder, places it in the number line, and multiplies it foreword. This will find every prime on the number line, however far down the number line you wish to travel.
    Regard,
    Russell Lee

  39. jawad mansoor October 18, 2019 at 11:26 pm - Reply

    make the processor faster
    if chipers of a number are all combined equal to a number divisible by three then it is not a prime, but that will also cost time.
    all numbers that have 5 in end are not prime either (not including 0, since it is divisible by 2 and already incorporated).

  40. S M Fahim October 18, 2019 at 11:26 pm - Reply

    It took 0.528 seconds to calculate 10,000 times. But 74.35 seconds to calculate 100,000 time. Why? Shouldn't have it taken 5.28 seconds? Why did it took almost 100 times extra seconds instead of 10 times?

  41. Mahendra Prasad Dimri October 18, 2019 at 11:26 pm - Reply

    Can you plz tell me which software is this ?!!!

Leave A Comment

*