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?

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.

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.

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)

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.

+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.

I am not sure if the second "while" is considered a nested loop inside the first while. Can you take a look at the code and let me know what you think I am doing wrong. Thanks in advance!

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!

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?

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.

Then how to count it

Is sieve Erathosthenes best solution for that

It is based on elimination and has linear space complexity

i want to count the output

That's so helpful! Thank you for posting that!!

thank you so much, the explaining was in very simple way that anyone could understand

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?

that was dope man!

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.

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.

Thank you

nice effort, but it wont work for the number 2

Thanks for the performance comparisons. đź™‚

any1 else tpnig wif lef hand?

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?

this helped me w my test, thanks

can you explain on how x**0.5 is sqrt of x and why are we adding + 1 to it? I am not able to grasp that particular part

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.

Very Well explained. Thanks!

+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.

ParabĂ©ns , obrigado…

Nice tut. Thanx!

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)

Ok I got it. Thank for all your help!

Thanks for the example. I tried to use that and I think I have the indents all wrong. This is what I have: http://app3.vocusgr.com/ViewAttachment.aspx?EID=vZR5Y4hXkVWu946S007TjFMGHODpqU6WQ6y%2f7WKEsdY%3d

I am not sure if the second "while" is considered a nested loop inside the first while. Can you take a look at the code and let me know what you think I am doing wrong. Thanks in advance!

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!

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

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.

The explanation about why only going up to the square of the number was nice. đź™‚