I've started assignment 2 by creating a test to see if a number is prime.
is_Prime = 4 #Declares a variable for the number to be tested
x=is_Prime #Sets the value x to the number being tested to act as the range
counter=0 #sets a variable named counter to zero
for i in range(1,x):
remainder = is_Prime%(x-1) #declares a variable named remainder and divides it by a value -1
if remainder == 0:
counter=counter+1 #if the remainder is = zero,then we add one to the counter
if counter>1:print "number not prime"
print "number is prime"
MIT 6.00 Intro Computer Science (OCW)
Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga.
Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus.
Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
does it work?
please use a code pasting site to post your code
.... there are others
@bwca My apologies. Newbie here. :)
Yes it does work. Now I have to figure out how to put it in a loop to get to 1000th prime.
Not the answer you are looking for? Search for more explanations.
sometimes to help visualize what you want to do, it helps to try to write it down in words - lets say the code you just wrote is primeTest:
start off with zero primes
while the number of primes is less than one thousand
perform a primeTest on successive integers
if you find a prime number, increment the number of primes
Here is my method of thinking.
Start at 2. Check to see if the number is prime. If it is prime, then add one to a counter. Do this until the counter reaches 1000, then print the number.
this is how I've written it out in code.
I may be way off though. I'll work with your suggestion.
Is it just me or is this assignment incredibly difficult for a beginning programming class? I simply cannot do this. I have tried different things and just can't get it to work.
The issue I have is once I come up with a function to determine if a number is prime, then how do I iterate through those values at the same time counting the number of primes?
@bwca How do I perform a primetest on successive integers? Are we talking about a loop within a loop?
you could use a loop within a for loop - you have to guess the limit of the range you are going to test then you have to break out of the loop when you have reached your goal. something like.
start with number of primes found = 0
for every number between 1 and 10000
perform a primeTest on number
if the number is prime increment number of primes found
if the number of primes found equals the number of primes you want to find
break (out of the for loop)
it is hard the first time you do it, you need to be systematic when trying to debug something. just keep at it - with practice you will learn some tricks.
one useful trick is putting print statements in your code to print variables and the results of tests so you can see how things are changing while it is running - compare what you are seeing with how you thought it was supposed to work.
I'm not sure your primeTest is working. it should be something like
assume theNumberBeingTested is prime
for every number between 1 and (theNumberBeingTested-1)
if theNumberBeingTested % number == 0 the number is not prime
Migpics, just keep reading the assignments and watching the lectures until it makes sense. Try the smaller exercises in the readings to make sure you understand each concept. The beauty of an on-line course is that you can go at your own pace.
Okay, I worked it out with a friend. The problem I was having was that I did not know how to iterate over the numbers. @bwCA I saw what you did about performing a test between 1 and 10000 and I was going to try that approach but instead I did an infinite loop and broke it when the counter hit 1000.
sometimes a while loop works better - you can loop while a condition meets your requirement - http://codepad.org/sxSePuqi