28 thoughts on “Python: Calculating Prime Numbers

  1. Why do we set a isPrime boolean to True at the beginning? Completely new to programming so would like all things that probably seem 'obvious' spelt out to me in the most simple way possible please. Can anyone help?

  2. I find the below code efficient(written by me). Can it be improved in any way?…Any help is greatly appreciated
    a=[2,3]
    n=int(input('Enter limit:'))
    for i in range(5,n,2): #not checking primeness in even numbers
    flag=True
    for j in a: #applying mod only by a prime number
    if(i%j==0): #(composite numbers smaller than i are already divisible by numbers in list a)
    flag=False
    break
    if(flag):
    a.append(i)
    print(a)

    edit: Of course using ur ideas.Great video
    edit 2:added my comments.

  3. Good one. Could speed it up a little more by checking only odds after 2, using range(3, n+1, 2) for both for loops (n is max or sqrt expression). Preload primeList with 2 for completeness.

  4. I found another way to optimize it. Instead of checking every single number, you can check only the odd numbers. In this case:

    for x in range(3, 10000, 2):
    isPrime = True
    for y in range(2, int(potential_prime**0.5)+1):
    if potential_prime % div == 0:
    isPrime = False
    break
    if isPrime:
    primeList.append(x)

    print(primeList

    Any thoughts?

  5. If you start with 2 and explain the complete process, it would be helpful. How do you explain it?
    if 2 % y == 0 for y = 2 (Refer run time video : 2:31) Is not 2 evenly divided by 2 here?
    by your statement, it is not a prime. Right? Or is there any other logic here.

  6. +Olie to answer your question, I think Joe didn't get it quite right. You should have

    if isPrime: primeList.append('%d prime'%x)

    else: primeList.append('%d not prime'%x)

    Joe's answer puts the number and the string as separate elements which is not what you want. Btw one is NOT prime, please try to not make this common mistake.

  7. Why wont this work for me?

    primeList =[]
    compositeList = []

    max= int(input("Find prime number up to what number? : "))

    for x in range (2, max+1):
    isPrime=True
    for y in range (2,x):
    if x % y == 0:
    isPrime =False

    if isPrime:
    primeList.append(x)

    else:
    compositeList(x)

  8. Joe, if I only needed to find the first 1000 prime numbers, how should I do that? I tried using while len(primeList <1000) under the primeList variable but I get a type error saying that field is a boolean. I like your approach to this problem. Thanks for the video!

  9. Thanks for the video professor. It was very helpful. But I am stuck at a question in this video.

    Why are we using 'if isPrime' before the print statement and the append statement?
    Also when I remove the 'if isPrime' ,before the print as well as append statements, then there are repeated list of prime numbers in the console after running the module.

    Can you please explain why is it happening and what role and importance of the 'is isPrime' statement before these statements?

    Thanks

  10. Hi Prof. James, Fantastic vid! I learned valuable information watching that. Thank you!I also noticed that the same result can be achieved without doing Boolean comparison. I removed all isPrime lines and replaced the if isPrime line with else:, and it works just fine. It saves me 1 less total line in the code.But regardless, thank you very much! I gained deeper understanding about nested for loops from this vid. I also learned a little bit on timing optimization.

Leave a Reply

Your email address will not be published. Required fields are marked *