A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

anonymous

  • 5 years ago

what's wrong with this (trying to find a prime number) import math candidate=29 if (candidate/2)*2 == candidate: print candidate, 'is even' else: print candidate, 'is odd' for i in range(2,candidate): if candidate % i == 0: print candidate, 'is not a prime' else: print candidate, 'is a primo'

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

    Why not use the following as the first line? if (candidate%2) != 0:

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

    Oops, that should be the reverse: if (candidate%2) == 0:

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

    That's better than expanding to if candidate % i == 0?

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

    You don't need to import math, but, more importantly, you must recognise what `for` does. It takes each item in a sequence, in order, the first, the second and so on, and assigns them, one at a time to a variable which you name anything you like. You called it `i`, but you can call it anything, `each` is common. `in` is obvious and then, on the end, you need a sequence. The range() function returns a sequence, a list of integer numbers in the range. So for each in 'abc': print each print '...' would output a ... b ... c ... because the for loop assigns the first item in the sequence, the 'a', to the variable `each`, so `each` now evaluates to a single character string, 'a'. Then it goes through the indented block, print each, so it prints the letter a, then print three dots, then the interpreter reaches the end of the block so it goes back to the top and assigns the next thing in the sequence to `each`, the character 'b' and so on until it has done this once for each item in the sequence. Because range returns a sequence, a list of numbers, you can iterate across them one by one, which you are doing with for i in range(2, candidate): This enables you to test whether candidate % i == 0 over and over again. Once with `i` evaluating to 2, then with it evaluating to 3 and so on. It is only once you have tried to divide `candidate` by every possible value for `i` and still have got no result that has a remainder of 0, that you know that `candidate` is prime. You must exhaust every possible value for `i` before you can know that `candidate` is prime. This is the bit you are struggling with. Try here for an example to work from http://pastebin.com/KU0CXFx3

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

    Your program is horribly in-efficient all you have to check are primes from 2 to the sqrt of the candidate. No reason to check all numbers.

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

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.