Looking for something else?

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

## More answers

Looking for something else?

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

- anonymous

I'm having difficulty writing a program that calculates the first 1000 primes. After brooding for a while this is what I came up with:
a=0
while a<100:
a=a+1
r=2*a+1
f=1
while f(1/2)*int(r):
print r
I would prefer it if you did not tell me the answer but only point out the part of the script that is wrong

At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga.
Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus.
Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.

Get our expert's

answer on brainly

SEE EXPERT ANSWER

Get your **free** account and access **expert** answers to this

and **thousands** of other questions.

Get your **free** account and access **expert** answers to this and **thousands** of other questions

- anonymous

- katieb

I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!

Get this expert

answer on brainly

SEE EXPERT ANSWER

Get your **free** account and access **expert** answers to this

and **thousands** of other questions

- anonymous

First thing, use more descriptive variable names. Upon first glance, it's hard to tell what a, r, and f are all supposed to do. Secondly, can you describe in english what this block should do and why?
if r%f==0:
pass
else:
if int(f)>(1/2)*int(r):
print r

- anonymous

if r%f==0:
# to check if r divided by the number f gives remainder of 0
pass
#If true then do nothing
else:
if int(f)>(1/2)*int(r):
# if f exceeds half of the odd number r then it no longer needs to check
print r

- anonymous

So r is the candidate you're checking to see if it's a prime number. Stepping through the first few lines of your program, let's see where issues arise.
a=0
while a<100:
a=a+1
r=2*a+1
So initially a is 0, then you add 1 to a, so it's 1. Then you set r to (2*a + 1) which is 3. Next is:
f=1
while f(1/2)*int(r):
print r
3 % 2 is 1, so we go to the else section. Now we're hitting a problem. This test may not be testing what you want. Open up a python window and type:
>>> 1/2
Hit enter, what does it return?

Looking for something else?

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

- anonymous

I think we should describe the algorithm we gonna use before writing some code.

- anonymous

So I rewrote what I had. I think i made progress but I am unsure. To make it easier i am just testing the first 50 primes. Included are notes for which each line is intended to do.
odd=0
#state variable
while odd<50:
#ends variable at 50th odd number
odd=odd+1
#prevents infinite loop
sequence=2*odd+1
#creates sequence of off numbers excluding one
any_number=1
#divisor
while any_number=(1.0/2)*sequence:
#if there is no number that perfectly divides the sequence beyond half of the sequence then sequence is prime.
print sequence
#prints out that number

- anonymous

That's much easier to read now, but I think there's some other issues with your method of finding the prime numbers. Try to describe what you're doing in the while loop, and how that gets you the first prime, then the second and the third, and so on. What is the first prime your program finds? Can you add code to have it print something when it finds it?

- anonymous

I think iyasvami makes a good point. After two weeks of trial and error code writing, and some lecture re-watching, I started physically writing my algorithms down on paper. It helps tremendously. And not only from a code standpoint. There is a definite neurological bonus when writing the ideas down by hand, rather than typing them. It sounds so mundane and maybe irrelevant, but I promise you, it helps.

- anonymous

So I took your advice and wrote my algorithms down and its much better. though i still have an error. It's not spitting out the numbers I want it to. I don't see where exactly did I go wrong. Here is my Code with notes:
#to find the nth number of primes the user asks.
nth_prime=raw_input("nth prime you are looking for?")#defines the counter
number=0#sets variable for numbers to try
divisor=2#tells what to divide by starting with 2
list_of_primes=()#tuple used to collect all primes found
while nth_prime>0:#initiates loop
if number%divisor==0:#first test
number +=1#if evenly divisible, its not prime skip to next number
else:
divisor+=1# if not even with fist divisor try next divisor
if divisor >= number:#tells the divisor to stop if it reaches this point.
list_of_primes= list_of_primes +(number, )#adds number to list because it is prime
nth_prime= int(nth_prime)-1#tells program to find primes until counter reaches 0
else:
pass#to guard against possiblities i didnt think of
print list_of_primes#prints out my lst of primes

- anonymous

You are stuck in your second IF statement - IF divisor >= number:.
Well as you have it, your divisor will always be greater than your numerator, so will always return 1, because:
if 0%2 == 0
0 = 0 + 1 (1/2)
now - 1/2 has remainder - else:
divisor += 1 - so - 1/3.
if 3 greater than 1, print 1 in tuple.
Restart loop...
number = 1. so if 1/3 no remainder...and 3 greater than 1, add 1 to tuple...etc.
Until counter goes to zero, where you pass, and print list of all ones.

- anonymous

An if statement doesn't need an else clause. Just skip it if you don't need it.

- anonymous

sorry should be:
Restart loop...
number = 1. so if 1/3 HAS remainder...and 3 greater than 1, add 1 to tuple...etc.
Until counter goes to zero, where you pass, and print list of all ones.

- anonymous

You should be testing out any modifications you make with the same methodology Aslander just used. Run through step by step and jot down what the values of each variable are, and where the program is going as a result.
But I think the issue is that you're having trouble properly coding how to find a prime. Write down the properties of prime numbers. Sketch out ways you could test to see if a number was prime or not. Then test your method by feeding it prime and non-prime numbers. You need to nail down your algorithm.

- anonymous

Okay. I'll do it on paper now. I've been following them in my head and i didnt see the error. Thank you for the help.

- anonymous

I'm currently online, if you hop in chat, we can talk faster. :)

- anonymous

Here's my version of solution for Problem1 from ps1:
cand = 11
prime_count = 5
div = 3
isFound = True
while prime_count < 1000:
cand = cand + 2
div = 3
isFound = True
while div*div<=cand and isFound == True:
if(cand%div==0):
isFound = False
else:
div = div + 2
if isFound == True:
prime_count = prime_count + 1
if prime_count == 1000:
print 'The 1000th prime number is', cand

Looking for something else?

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