Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PRIME NUMBERS
#1
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
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
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
Reply
#4
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
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?
Reply
#6
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.
Reply
#7
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. :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Random Generator: From Word to Numbers, from Numbers to n possibles Words Yamiyozx 2 1,407 Jan-02-2023, 05:08 PM
Last Post: deanhystad
  Convert list of numbers to string of numbers kam_uk 5 2,989 Nov-21-2020, 03:10 PM
Last Post: deanhystad
  Prime numbers janoshz 4 2,410 Oct-05-2019, 10:01 AM
Last Post: Preetimishra90
  Prime numbers calculations frequency 3 2,971 Nov-27-2018, 07:07 PM
Last Post: micseydel
  10x10 Array of Prime Numbers smfox2 1 2,526 Sep-03-2018, 12:36 AM
Last Post: ichabod801
  Regular Expressions in Files (find all phone numbers and credit card numbers) Amirsalar 2 4,091 Dec-05-2017, 09:48 AM
Last Post: DeaD_EyE
  prime factorization of two large numbers pyJim 2 3,676 Aug-24-2017, 03:19 PM
Last Post: pyJim

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020