Quantcast

A community for students. Sign up today!

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

WebPillar

  • 2 years ago

hello, I'm stuck on the second problem of set 1. I am trying to write code to add the logs of each prime generated. I think it's close, but for some reason, it never exits the while loop when I want it to (when the number chosen is reached by the prime generator). No matter what, it seems to just give the sum of the first two primes. Any advice is much appreciated... the question box won't let me add my code... how do I share the full code?

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

    I attached the code to this answer... not sure how I was supposed to share it..

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

    Prime number: "The next prime number cannot be divided by other primes (except one)" like 2 only with 2 & 1, 3 only with 3&1, 4 (not a prime cuz 4%2(prime) = 0) and so on. Don't overthink just, pick a number "n" for how many primes, every time you found a new prime, add it into a variable (var = 0), var += log(newprime). and print outside the while loop .... now that I see, your "n" variable never decrease, your while loops is infinite, add the "n -= 1"

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

    I'm sorry, I can't really follow what you're saying... n isn't supposed to decrease. n is supposed to be the chosen number through which the primes are generated. I've already completed the first part of the problem set that would require picking a certain number of primes. This code is pick a number up through which all primes are generated, to then find their log, and then add those logs. Rather than n decreasing, the "number" variable is supposed to increase (as it does fine) and when it hits n, it is supposed to stop the loop. Per my understanding of loops, the first part of this is happening (the variable is increasing, as evidenced by it being printed properly) but the second part, the loop stopping, does not occur.

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

    Sorry I didn't see number either, i'm so into another thing, I just run it on my mind, I use n=5, and only gave me log(3) and finish the code. What I mean was: a prime number can only be divided by itself and 1, so a prime % anyOtherPrime != 0.

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

    recheck that your looping line very well

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

    I didn't really think through your code, but I see a couple of things: Python's null value is None, not none. Yes it does make a difference. If you print the value that your condition evaluates to on each iteration, I'll bet you'll be surprised.

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

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