anonymous
  • anonymous
Anyone want to check out my code for problem 1 (a)? It works, I am just looking for tips moving forwards. print 'Currently Calculating Primes...' primeCount=1 primeArray=[] a=0 while a < 1000: primeCount = primeCount + 1 if primeCount == 2: primeArray.append(primeCount) a=a+1 elif (primeCount/2)*2 != primeCount: false = 0 for j in primeArray: if primeCount%j == 0: false = 1 break if false == 0: primeArray.append(primeCount) a=a+1 print str(primeArray[999])
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 :)
SOLVED
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.
katieb
  • katieb
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
anonymous
  • anonymous
You know that the end number will be prime 999, but you could get 'the last thing in the list' by saying print primeArray[-1] Also, I'm not entirely sure what an array is in programming terms, but I know it's a specific thing and I'd personally be careful not to confuse it with a list, which is what the primeArray variable is.
anonymous
  • anonymous
That's a decent solution. I would make a couple of suggestions though. Firstly you can simply initialize your array with primeArray = [2] since you are adding the number 2 directly. This saves having to check each number to determine if it's a 2 when none of them will be. You can also consequently initialize a with a=3. My second suggestion would be to avoid using variables named 'true' and 'false'. It is not entirely clear what that data is intended to represent. Perhaps call it isFactorable. Since it is used to determine if can factor this value or not. Initialize it with isFactorable=False and then set it to True when you find a prime by which it is divisible. There is an error in your code in that you only increment a when the number is prime. The second a=a+1 line should be unindented to the same level as the while block. It should not be inside the if false==0: condition. Your final print statement will only print the last prime, not the whole list, and it's not necessary to convert it to a string since print will automatically convert the items you're printing into strings for you. Also, it doesn't appear that you're doing anything with the variable primeCount. Did you mean to print this out somewhere? Finally, your test for if a is even is already taken care of by your test to see if a is divisible by any already seen primes. While it may be handy as an optimization to avoid searching in half of the cases, it does add a bit of code that has to be maintained. So unless you notice that your function isn't operating "fast enough" it's best to hold off on trying to optimize it for efficiency. Readability matters. My changes would look something like this: primeArray=[2] i = 3 while i < 1000: isFactorable = False for prime in primeArray: if i%prime == 0: isFactorable = True if not isFactorable: primeArray.append(i) i+=1 print 'There are', len(primeArray), 'prime numbers between 1 and 1000' print 'They are', primeArray
anonymous
  • anonymous
I think we are not supposed to know what array is for this problem set. There was no mentioning about arrays in the lecture. I solved the first problem using two while cycles and few if statements.

Looking for something else?

Not the answer you are looking for? Search for more explanations.