A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

anonymous

  • 4 years ago

I am having the same problem with PS!, here is how i figured out that a number is prime. x = 8 if x % 2 != 0 and x % 1 == 0: x% x print x, " is prime" else: print x, " is not prime" now that i can determine that the number is prime, How do i tell it find the 100th prime and print it, I know it involes using a loop.. thats where i am at..

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

    I am having the same problem, but I am not sure your code calculates the prime number correctly. Try putting in an odd number that is not prime, such as 27, for x and it will say that it is prime. I just don't know how to calculate if a number is prime.

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

    if you wanna the nth number you should try from 1 to \[\infty\]

  3. maitre_kaio
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    Your code is wrong. You are saying that a number is a prime if it is odd (x % 2 != 0) and a multiple of 1 (x % 1 == 0). All integers are multiples of 1. So your program will return true for all odd numbers. You should check the definition of a prime number http://en.wikipedia.org/wiki/Prime_number Make sure what a prime number is clear for you. After that, start coding. You will see that it's in fact easy to do. Once you have a code testing properly if a number is a prime, go to the next step and write a program which calculates the nth prime. To help you, I attached a little diagram.

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

    Ok wi ll try it. and see what happens.

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

    The way I (eventually) solved the issue of finding the 1000th prime is by creating a list of the known primes and having the program count them using the len() function. It's a concept that's not covered in the first two lectures really, but it's a neat trick to play with for a super n00b like me. I thought I'd at least share it since you may find it useful in the future. Every time the program successfully calculated a prime (if is_prime:), before going on to try the next number (num += 1) I would ask it to record the prime it just found and store it in a list using the append method. The code I wrote begins by asking whether or not I have reached the 1000th prime by counting the number of items in my primes list. As soon as it finds the 1000th, it prints it out. Here's what I'm talking about - hope it helps! For your reference: #Problem 1: Calculating the nth prime n = raw_input('Gimme a number. How many primes you want? ') #Enter 1000 to find the 1000th n = int(n) #Changing that input to an integer so I can use it for calculations number = 2 #No need to start at zero or it will tell me that 1 is a prime (it's not) primelist=[2] #2 is the first prime number while len(primelist) <= n: test = 2 is_prime = True while (test*test) <= number: #Looking for squares here for the square root test of finding primes if number%test == 0: is_prime = False test += 1 # Test out the next number if is_prime: primelist.append(number) #Just keeping track of the primes so I can count to 1000 number += 1 #Let's try the next number down the line print primelist[-1], "is the ", n, "th prime number." print "done"

  6. maitre_kaio
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    I have two things to say to improve this program: 1. when you have found a number is not a prime, why continue to test other divisors. You could end the inner looping and go to the next number. 2. Instead of keeping track of all the primes, why not count them (increase a counter each time you find one)? That way you will spare some memory, and you don't have to calculate the length of the list each time. I don't know how Python implements len(list), but just imagine that it has to walk through all the elements to count them....

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

    This is how I did it. I used len() as well. First I set up my primes list so I had something to measure. Then I devised a test if the number was prime or not. If the number was prime it was added to the primes list which was measured and set for the loop. Because I wanted to get only up to the 1000th prime I made my loop stop there. primes=[2,3] def prime_test(int_to_test): A= (int(int_to_test**.5)+1)### you only need to test up to the ### square root of the number if int_to_test%2==0 or int_to_test%3==0: return False elif int_to_test%A ==0: return False elif int_to_test%(A)!=0: while A>2: A=A-1 if int_to_test%A==0: return False elif int_to_test%A!=0: True if True: print int_to_test primes.append(int_to_test) k=5 while len(primes)<1000: prime_test(k) print len(primes) k=k+2 if True: print int_to_test primes.append(int_to_test) k=5 while len(primes)<1000: prime_test(k) print len(primes) k=k+2

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