yes, im still stuck on that primes problem. my code is this: http://dpaste.com/532787/ my bug begins after the 24th prime. for the 25th i get 95 and not 97. since my tuple contains all the #'s from 2 up to but not including the value being tested, and 95%5==0 is true, it should add 2 to the number again, but it doesn't. when i try running the for loop 999 time it gives me the 800 something th prime. i just can't seem to figure our the bug. my (tiny) brain has ben struggling with this problem for a tremendous amount of time. help.....

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.

Get our expert's

answer on brainly

SEE EXPERT ANSWER

Get your free account and access expert answers to this and thousands of other questions.

A community for students.

yes, im still stuck on that primes problem. my code is this: http://dpaste.com/532787/ my bug begins after the 24th prime. for the 25th i get 95 and not 97. since my tuple contains all the #'s from 2 up to but not including the value being tested, and 95%5==0 is true, it should add 2 to the number again, but it doesn't. when i try running the for loop 999 time it gives me the 800 something th prime. i just can't seem to figure our the bug. my (tiny) brain has ben struggling with this problem for a tremendous amount of time. help.....

MIT 6.00 Intro Computer Science (OCW)
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
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.

Get this expert

answer on brainly

SEE EXPERT ANSWER

Get your free account and access expert answers to this and thousands of other questions

I couldn't really just find a single bug or an easy fix for your code so I kind of just rewrote it for you if you have any questions just ask. http://dpaste.com/hold/532804/
Double_o, your outer loop is only iterating 49 times, and your inner most one is making your divisor search skip a lot of possible divisors for some of the prime numbers. I think inserting a few print statements would make it pretty obvious where things are going wrong. Just put a line in a few different places to show where you are in the code and what the values of prime, div, and i are.
sry i was using 49 to see if it would give me the 50th prime and forgot to fix it. but what do you mean that its skipping possible divisors? doesn't the range give the values from 2 up to but not including the upper value in range? sry im really slow sometimes

Not the answer you are looking for?

Search for more explanations.

Ask your own question

Other answers:

Yes, the range(2,prime) will give a list of numbers from 2 to prime, but inside that you're incrementing your prime variable and continuing to look for divisors from the list for the original prime, not the newly incremented one.
ok, so your saying that the prime value in range(2, prime) is 3 throughout the entire loop?
polpak had a good idea: try this out, http://dpaste.com/533072/ i put a few print statements in there - makes for a long listing but you can see that is actually "going wrong" a lot earlier even though it provides correct results - stop it from running with ctrl-c then scroll back to examine the output Heres a good example i=8 and prime = 25. http://dpaste.com/533073/ sometimes inside your while loop you change the number you are testing for primeness but div just keeps incrementing - it never goes back to check the 'new prime' for the lesser values of div. later on that might lead to problems. print statements rule!! was that while loop an attempt to optimize? optimization early to the dark side leads
AH! the light just turned! on now i see what it is doing. thank you all for your help. now that i understand what it is doing wrong i might be able to fix it (hopefully)
AH! the light just turned! on now i see what it is doing. thank you all for your help. now that i understand what it is doing wrong i might be able to fix it (hopefully)
here's my updated code: http://dpaste.com/533273/ i added to more for loops so that it would update the upper end of the range. since it was able to give me the ten-thousandth prime correctly, im thinking that its ok for the purposes of this lesson. any comments? thanks polpak and bwCA
me personally, i would not change two things inside of loops. because you are doing this you have to have multiple nested loops to go back and account for those changes which complicates the program and makes it hard to understand what's going on. check out the zen of python: http://www.python.org/dev/peps/pep-0020/ bottom line - if you get the right answers, you get the right answers. the purpose of the course is to learn how to think like a computer scientist and learning python is a side benefit (big one imo). the way you implemented this may work for a simple/small problem like primes but if you use that approach for larger/complex problems it might get real ugly. the caveat here is that i really don't have a lot of experience but i have written some ugly programs - that's why i'm taking the course. as the code gets more complicated, you should add more comments to explain what it is doing so that when you look at it next week you can figure out what it's doing. if You(!) think your code is starting to get ugly it might be time to regroup and rethink the solution. I can tell when this is happening to me when i keep having to go back and add all kinds of stuff to just MAKE IT grind away at a problem till it works. in this case, i would have an outer loop that changes prime and an inner loop that changes div. just my take tho - maybe someone else will jump in..
thanks for the advice
One of the things to start thinking about is Given some large problem, how can I decompose into a set of smaller problems. For this case, you need to find a bunch of prime numbers. To do that, you have to move through the set of potential primes, and test for primeness. So it breaks down into two smaller problems. Problem (1) move through the set of potential primes until we have found 1000 primes. Problem (2) test a given number to determine whether or not it is a prime. The solution to the first problem relies on the solution to the second, so start with the second problem first. If I give you a number testNum = 53982717637291, can you write a small bit of code that will determine if testNum is a prime number?
well, you could take one of two approaches 1. divide by all numbers except 1 and the number u mentioned is prime. if there's no remainder for any of the numbers, then it is prime 2. check to see if any of the previous primes divide evenly into the number. if they do not, then the number is prime thats what immediately comes to mind
But for the example I gave, you don't have any previous primes.
then the only option is 1, but i don't think its a good method. honestly though, i can't see another way
it would be like i did in my program where: for i in range(2, #): if # % i == 0: print "# is prime" else: print " # is not prime"
opps i got that backwards for i in range (2, #): if # % i == 0: print "# is not prime" else: print "# is prime"
That won't quite work right. First use testNum instead of # and imagine that testNum = 15. What will this code output?
oh i didn't think that that would happen.......
Can you correct the problem?
i tried this: testnum = 15 for i in range(2, testnum): if testnum % i == 0: testnum = 2 else: testnum = 0 if testnum <3 and testnum >1: print "not prime" else: print "prime" but its not correct because it testnum = 100 it still says prime... :(
Ok, lets take a step away from the code. Describe in english what you would do to determine if 15 was a prime number?
wait, i just had a thought
you could find the divisors to testnum and see if any of them are prime
Does it matter? If you find a divisor for testNum it is definitely not prime unless it's 1 or testNum.
Let me describe a process to you, and you tell me what you think. Loop for every number from 2 to testNum-1. If that number divides testnum, testnum is not prime If we get to the end before we found a divisor testnum is prime
Actually I don't like the word loop in that explanation, but oh well.
Does that process seem as though it does what we want?
sounds right to me.....
#Ok so you have the outer part down for i in range(2, testNum): #And also the part where we check if i divides testNum if testNum % i == 0: # What should we do here??
i'd like to say that it's not prime, but i did that i nmy last cade and it didn't work so i'm kinda lost
Well you're right. We can say it's not prime. But what else do we need to do?
Should we continue searching?
we need to kill the program
We need to end the loop. Do you know of a way to end a loop early?
sadly, no
Ok then. Do you know how to write a while loop?
since ive only watched up to lesson 3 i only know about for loops
Ok then. Lets try this. Can we safely assume that a number is prime unless we find something showing it is not prime?
oh wait u mean the "while something is true, do something" loop?
Yes. That is a while loop.
ooooooooooh ok then i guess ive seen it before
Can you write a while loop that is the equivalent to for i in range(2, testNum):
div = 2 while div < testnum and div >= 2: testnum % div # im still not quite whether to bind it to something, set it equal to something or something else so for the time being, i'll leave this portion incomplete div = div + 1
do i need an if clause in there?
No, that's great.
One minor point (doesn't matter much) since div = 2 and you're adding to it inside the loop it will never be < 2. So the div >= 2 bit isn't needed.
So for div in range(2, testNum): is the same as div = 2 while div < testNum: div = div+1
never thought until today
never thought about that until today*
wait, since all the numbers being tested are odd, couldn't we set div = 1 in the beginning then increment it by 2 to get it to divide by all odd numbers, since no odd number is evenly divisible by an even one?
Sure, that'd save us some bad tests certainly.
no that won't work we have to set it to 3 since it would evenly divide by 1
Yeah good point.
But still, that's a decent optimization.
Ok, so now did you decide if it would be ok to assume that until we find a number which divides testnum, we can assume it is prime.
yes i would think so
We are trying to tell if testNum is prime because we plan to do something with that piece of information. So perhaps we should have a variable to keep track of that info. Something to indicate the 'primeness' of testNum so once we determine it, we don't have to test it again.
What should we call that?
are you suggesting to store the prime values in a tuple?
No. I'm saying have a variable 'testNum_is_prime' or something that is True if testNum is prime, and False if it is not prime.
im not sure i understand what you mean by storing and not testing again.
Well, we are looping over all the divisors right? And when we find one that divides testNum we know at that moment testNum is not prime. But after that moment is passed, how will we know? Once we've finished the loop, what can we do to see if we made it through successfully without finding a divisor or not?
If we store that knowledge somewhere we can retrieve it later
we could say something like prime = testnum if testnum is prime and not_prime if testnum is not prime
Sure. But we only know testnum is prime if we make it through EVERY divisor and don't find one that works. We know it is not prime if we find just 1 that works.
So really we just need to know if it's not prime. Perhaps we can call the variable notPrime. if testNum % div == 0: notPrime = True
but we can't assign to literal right?
notPrime is just a variable name, like testNum or div
i meant we can't assign a variable to something literal, but i found out we can call it true so never mind
True is the universal truth object in python. Just like 1 is the universal unary object.
So it's ok. If you prefer though you can do something like notPrime = 1
sry i didn't know that
So now we have our loop: div = 2 notPrime = 0 # We don't know so we assume it's prime while div < testNum: if testNum % div: notPrime = 1 if notPrime == 0: print testNum, "is prime" else: print testNum, "is not prime"
Whoops, I'm missing a few things.
I forgot to increment div, and you wanted to start div at 3 instead of 2. Can you fix it?
Did I lose ya?
i meant we can't assign a variable to something literal, but i found out we can call it true so never mind
not yet :) http://dpaste.com/533358/
Great except that the div = div+2 bit needs to not be part of the if block right? We always want to increment div
http://dpaste.com/533360/
right
Ok, now that should work well. Can you see any way we can optimize it further?
Oh, actually your version has a minor bug.
if testNum % div:
testNum % div will be True when?
testNum % div == 0
right. Otherwise it would do the opposite of what we want since 0 evaluates to False in a boolean context.
oh ok sry about that
but now i need to modify the program so that it will generate numbers, test to see if they are prime, then spit out the 1000th prime number right?
Certainly! But we already have some code to test for primeness, so all we have to do is take this code we have, and nest it inside some other loop that changes the value of testNum.
so we could basically set testNum = 1 in the begnning , loop it to the desired prime-1, then keep testing the odd numbers?
Well you want the 1000th prime, so you need to keep a count of how many primes you find.
oh yeah....
do we use a tuple then?
or range (1000)
No, just a variable. testNum=3 primeCounter = 1 # Since we're skipping 2 while primeCounter < 1000: ... testNum = testNum + 2
oh duh
Then when you find a prime (in addition to printing it) you can increment primeCounter.
the ... is where our original code will go.
with nearly no modification
so if i basically stick all this together i can get any prime i need so long as i make primeCounter < anyVariable and have anyVariable = input("some sort of blah")?
Yes essentially.
It will take longer, the farther you go because of the increased sparsity of primes
But it will work
thanks for all your help. now i think i see what i was supposed to. your a lifesaver
of course!
i'll be back when i need more help but thanks in advance :D

Not the answer you are looking for?

Search for more explanations.

Ask your own question