A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing


  • 5 years ago

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])

  • This Question is Closed
  1. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  2. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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

  3. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

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

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...


  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.