Python Forum
PRIME NUMBERS - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: PRIME NUMBERS (/thread-5053.html)



PRIME NUMBERS - iamnotabot - Sep-16-2017

WE WERE ASKED TO PRINT THE PRIME NUMBERS USING WHILE, IF, ELSE, BREAK WITH TWO LOOPS.

MY CODE GOES LIKE THIS:

COUNTER = 2;
N = 2;

WHILE COUNTER < 100:
COUNTER += 1
IF COUNTER % 2 == 0:
BREAK
ELSE:
WHILE N< 100:
N += 1
IF N % 2 != 0:
PRINT (N)

---> instead of printing prime numbers my code prints ODD numbers, please tell me what am i doing wrong?? Thank you Smile Smile


RE: PRIME NUMBERS - ichabod801 - Sep-16-2017

It's printing odd numbers because you are only checking against 2. You need to check against all the prime numbers. So you have a list, it starts as just [2]. You check each number greater than two against everything in that list (with the same % check you are using). If it's not divisible by anything in the list, you add it to the list, and start checking against it as well.

For example, you start with [2] and check 3. 3 is not divisible by 2, so you add it to the list: [2, 3]. When you get to 9 you'll check it against 2, and it won't be divisible by 2. But then you'll check it against 3 and see that it is divisible by 3, and you won't add it to the list.

There are further refinements that can be done, but that should get you the required list of primes.


RE: PRIME NUMBERS - iamnotabot - Sep-16-2017

My instructor told me to do n-1 but it's giving me negative values of odd numbers since my while statement for my inner loop is n<100. I don't get what he's trying to say, does anybody know how to modify this? he doesn't want us to use "for" either


RE: PRIME NUMBERS - ichabod801 - Sep-16-2017

Wow, that's really screwy. I think what he wants you to do is two loops with two counters (call them counter_1 and counter_2). counter_1 goes from 2 to 100, and is the numbers you are checking for primality. counter_2 goes from 2 to (counter_1 - 1). That's the number you test against (counter_1 % counter_2).

That will work to give you all the primes. I say it's screwy because it will test a lot of numbers you don't need to test.


RE: PRIME NUMBERS - iamnotabot - Sep-17-2017

Thank you, I already tried to do this:

Counter = 2;
n = 2;

while Counter < 100:
Counter +=1
if Counter % 2 == 0:
break

else:
while n < 100:
n += 1
if n % Counter != 0:

print (n) 
this doesn't give me any prime numbers. did i do anything wrong here?


RE: PRIME NUMBERS - hbknjr - Sep-17-2017

With all those conditions #WHILE, IF, ELSE, BREAK WITH TWO LOOPS program is gonna be unnecessarily messed up.
I will give you hints and you write the code.
  1. declare variable n, assign value 2 and print it.
  2. start while loop until n < 100.
  3. increment n by 1.
  4. check if it's divisible by 2, use pass statement and do nothing
  5. in else statement, initialize a counter with 1, and a boolean variable isprime = True
  6. Now start a while loop while(counter <= n/2): # you can also check till sqroot of n
  7. increment counter by 1
  8. check if n modulus counter equals 0. within if statement change isprime = false and break out of the loop.
  9. Now within else statement from step 5 use if to check isprime and if it's true print n.

You can still optimize this code by checking counter value till square root of n and incrementing the counter by 2 instead of 1.


RE: PRIME NUMBERS - iamnotabot - Sep-17-2017

thank you, i will try this one but can't use the pass statement as we haven't covered that topic yet, will try to modify it. Thank you so much. :)