PS1, problem 2, Think I got it right, but my ratio gets further away (higher) from 1 as 'n' increases, so there's gotta be something wrong, anyone care to take a look and see if I'm missing something? http://codepad.org/F6nN5HrN
In line 30, why are you printing target as the # of primes? If I input 100 as the test, it returns 99 as the number of primes, which is inaccurate. Also, if you add "print num_of_primes" under line 22, you can see that your num_of_primes is increasing for every integer, not just when a prime should be found. I can't offer any concrete advice as to how to fix it (I am pretty new myself!), but I would take a look at how you are testing for primes.
Somehow a -1 got tacked onto my input expression, not sure where that came from. I'm also a little confused about what you said about line 22. If I put a print right below line 22 it would be put of that conditional expression, so of course it would go up each time, that conditional expression only goes true if the number was prime. I did a little more troubleshooting and it almost seems like I'm using the log function wrong somehow. When I run this program Python is outputting log(3) as 1.09.... that's way wrong.
The default base for the log() function is e, not 10. You can specify the base of the log as the (optional) second parameter, or you can call the log10() function if you want base 10 logs. But, you want the log base e (natural log), so just call log() with one parameter.
iyeager: Here is your code with an input of 7 (and some print lines I put in): http://codepad.org/GySd4Bqm By the solution of your code, it looks like the program thinks there are 8 prime number (when I am only testing up to the number seven). Again, sorry if this isn't helpful/I can't explain it better.
The main reason you're getting the wrong value for the ratio is that you're not exactly doing what the problem asked you to do. Reread the problem, and understand what their "n" is, and then see what your "target" is.
http://codepad.org/RrEva3I0 I also took the liberty of refactoring your code. I didn't change the behavior (except I initialized log_sum to log(2) instead of the literal value of log10(2), and I changed the condition in the first while loop), so it still produces your incorrect result. I did it incrementally, and included separate copies of the code for each step so you can look at the before and after versions for each step. There are 8 steps. On each step I put a comment about what code I changed and why. I think the end result is more readable and easier to follow. But it's the exact same code you wrote, just in a slightly different form. Hopefully you'll find it instructive. You can take the end result and take it out of the function, unindent it, and add the input() and print statements back in.
Thanks a ton dmancine, I appreciate you not just commenting but actually helping.
To be clear, I didn't change the behavior of your code, I just changed how it looked. It's still not doing what the problem asks. There's one big disconnect between your code and the problem statement. After that there might be some smaller details that need to be worked out.
Join our real-time social learning platform and learn together with your friends!