Quantcast

A community for students. Sign up today!

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

Migpics

  • 2 years ago

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 x=x-1 if counter>1:print "number not prime" else: print "number is prime"

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

    does it work? please use a code pasting site to post your code http://dpaste.com/675763/ http://pastebin.com/qT1Hyd9K http://codepad.org/3lzUfYsa .... there are others

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

    @bwca My apologies. Newbie here. :)

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

    Yes it does work. Now I have to figure out how to put it in a loop to get to 1000th prime.

  4. bwCA
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    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

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

    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. http://codepad.org/zOOA8ys4 I may be way off though. I'll work with your suggestion.

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

    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?

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

    @bwca How do I perform a primetest on successive integers? Are we talking about a loop within a loop?

  8. bwCA
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    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 http://codepad.org/ecJFg686

  9. JulieNewbie
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    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.

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

    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.

  11. bwCA
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    sometimes a while loop works better - you can loop while a condition meets your requirement - http://codepad.org/sxSePuqi

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

    Search OpenStudy
    • Attachments:

Ask your own question

Ask a Question
Find more explanations on OpenStudy

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...

23

  • 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.