anonymous
  • anonymous
Man I'm pulling my hair out to solve this simple code. Can someone please help me. Write a program that computes and prints the 1000th prime number. (I'm doing this in Java btw.) This is beginning comp sci course so they I'm sticking with selection and repetitions structures. Nothing fancier than introductory comp sci code.
MIT 6.00 Intro Computer Science (OCW)
katieb
  • katieb
See more answers at brainly.com
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 this expert

answer on brainly

SEE EXPERT ANSWER

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

anonymous
  • anonymous
I have a solution to this that'a a little bit clunky. This is Python, I think I have basically the same inelegant solution for Java somewhere. Let me know if you come up with something that doesn't have to start counting at three. import math countPrime=1 testNumber=3 print '2 is prime' print '3 is prime' while countPrime < 999: testNumber=testNumber+1 divisor= int(math.sqrt(testNumber)) divisorCount=0 while divisor > 1: if (testNumber%divisor==0): divisorCount=divisorCount+1 divisor=divisor-1 if (divisorCount==0): print str(testNumber) +'is really prime' + str(countPrime) countPrime=countPrime+1 print 'end'
anonymous
  • anonymous
I've got a code with yield and next, it creates a list of the n first prime numbers, hope it hepls, python btw: def firstNPrimes(n): # n = how many primr numbers you want priL = [] aux = 0 i = 2 while len(priL) < n: if i == 2 or i == 3: priL.append(i) else: for j in priL: if i%j == 0: aux = 1 if aux == 0: priL.append(i) i += 1 aux = 0 return priL
TuringTest
  • TuringTest
just for posterity, I wrote one right now in python that does not use a function definition, and only gives the 1000th prime (sorry, nobody here seems to use JavaScript) primes = [] testPrime = 2 while len(primes) < 1000: isPrime = True for prime in primes: if testPrime%prime == 0: isPrime = False testPrime += 1 if isPrime == True: primes.append(testPrime) print primes.pop()

Looking for something else?

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

More answers

anonymous
  • anonymous
I forgot not to include definition xD!
KonradZuse
  • KonradZuse
Now what do you mean by the 1000th prime #? Do you mean the actual # that is the 1000th(whatever that may be) or do you mean a number that you enter? Please be specific.
anonymous
  • anonymous
Turing's gives the first 1000 primes, mine give you the # you want (n).
KonradZuse
  • KonradZuse
Idk why schools give such useless assignments such at this, they are NEVER used in the real world; however from what I'm posting it will help you think mathematically and help you with problem solving. Trivial Cases We learned numbers are prime if the only divisors they have are 1 and itself. Trivially, we can check every integer from 1 to itself (exclusive) and test whether it divides evenly. For example, one might be tempted to run this algorithm: //checks whether an int is prime or not. boolean isPrime(int n) { for(int i=2;i
anonymous
  • anonymous
Well that's a big argument this is how my face look like now :P .... |dw:1353791266808:dw|
KonradZuse
  • KonradZuse
Not at all, what they teach yo in school is mostly useless.
KonradZuse
  • KonradZuse
|dw:1353793840818:dw|
anonymous
  • anonymous
Thank guys, I really appreciate the help. Sorry I didn't acknowledge anybodies comments when when I initially made this.

Looking for something else?

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