A community for students.
Here's the question you clicked on:
 0 viewing
tiggertiffin87
 4 years ago
ps1: I am absolutely, utterly, totally lost... Please just provide hints and not answers. I had trouble learning Java too.
import math
x = raw_input("Please enter a number ")
def prime(x):
if x < 2:
return False
if x%2 == 0:
return False
if x%3 == 0:
return False
if x%5 == 0:
return False
else:
return True
prime (x+2)
tiggertiffin87
 4 years ago
ps1: I am absolutely, utterly, totally lost... Please just provide hints and not answers. I had trouble learning Java too. import math x = raw_input("Please enter a number ") def prime(x): if x < 2: return False if x%2 == 0: return False if x%3 == 0: return False if x%5 == 0: return False else: return True prime (x+2)

This Question is Closed

tiggertiffin87
 4 years ago
Best ResponseYou've already chosen the best response.0I've looked at other's answers and it all seems so complicated. Any additional reading to suggest?

student47
 4 years ago
Best ResponseYou've already chosen the best response.0one just simple hint: prime numbers are not dividable exactly except for only two numbersprime number itself and 1. it means it is not exactly dividable with any number lower than it but for 1.i recommend [while loop] with several [if] condition

malpaso
 4 years ago
Best ResponseYou've already chosen the best response.2first suggestion is throw away your code entirely. write out what you are trying to do in pseudocode. for example, if the assignment were to write a program that adds any two numbers provided as input, your pseudocode would look like this: retrieve as input two numbers add the two numbers print the result Suppose the example were slightly more complicated. If the problem was accept two integers as input and determine if the sum is even or odd. The pseudocode might look like this: retrieve as input two integers add the two numbers if the sum is even, print "even" else print "odd" I encourage you to first write out the program in pseudocode as a series of steps, then translate it into code.

tiggertiffin87
 4 years ago
Best ResponseYou've already chosen the best response.0I found an incredibly helpful post by Peter Otten here: http://groups.google.com/group/comp.lang.python/browse_thread/thread/f6f44b646af5b09e?pli=1 He actually explained each item you needed to figure out. Now my only question, how do I alter the code so I can enter a number and it will give me that prime. Like if I wanted to know the 30th prime or the 1000th, rather than have the code just print out the 1000th. import math def isprime(candidate): return candidate != 42 def all_natural_numbers(): i = 1 while True: yield i i = i + 1 def all_prime_numbers(): for i in all_natural_numbers(): if isprime(i): yield i i = 1 for p in all_prime_numbers(): if i == 1000: print p break i = i + 1

malpaso
 4 years ago
Best ResponseYou've already chosen the best response.2When I look at the code above it appears convoluted. Some of it makes no sense whatsoever. If you are not going to write out what you are trying to achieve in pseudocode, then work on a simple problem first and post the code for that. See if you can write the following program: The program accept as input some integer, and determines whether the number is prime or not. The program output should something like this:  What number would you like me to test for primality? 5 True  What number would you like me to test for primality? 121 False etc. I wouldn't tackle the 1000th prime problem until you are able to complete this program first.

tiggertiffin87
 4 years ago
Best ResponseYou've already chosen the best response.0I've googled it and this: for x in range(2,int(n**0.5)+1): Makes absolutely no sense to me.

tiggertiffin87
 4 years ago
Best ResponseYou've already chosen the best response.0This whas the code it came from: def isPrime(n): if n == 1: return 0 else: for x in range(2,int(n**0.5)+1): if n%x == 0: return 0 return 1

tiggertiffin87
 4 years ago
Best ResponseYou've already chosen the best response.0This doesn't work and it's all I can figure out: defPrime(x): if x%2 == 0: return False if x%3 == 0: return False if x%5 == 0: return False else: return True

malpaso
 4 years ago
Best ResponseYou've already chosen the best response.2Forget Google. Forget Coding. Think. If I give you a number, how would determine whether it's prime or composite? What are the steps? Write that down. For example, if I give you the number '17', how would YOU determine whether it's prime or not?

tiggertiffin87
 4 years ago
Best ResponseYou've already chosen the best response.0If it's divisible by 2 and/or 3 and/or 5 then it's not a prime.

malpaso
 4 years ago
Best ResponseYou've already chosen the best response.2Good. Your answer is *not* correct. But this is a step forward. What you say is partially true. Indeed, if a number is divisible by 2 or 3 or 5 then it is not prime. But a number is not prime if it is divisible by *any* number other than itself. So, for example the number 49 is not prime. Why? Because it is divisible by 7. If we were to use your test (is it divisible by 2,3, or 5) then we would conclude that 49 is prime, which it is not.

malpaso
 4 years ago
Best ResponseYou've already chosen the best response.2So can you try again? How would test to see if 17 is or is not a prime number? And why?

student47
 4 years ago
Best ResponseYou've already chosen the best response.0malpaso...you are trying really hard to give instruction...clap clap clap

claiminglight
 4 years ago
Best ResponseYou've already chosen the best response.0I recommend you check out lecture 4, near the end. The professor's discussion on the Fibonacci sequence is of particular use in this area. You'll want a 'recursive' answer. As a side note, trying to solve for big numbers might crash your program you'll actually see an example of that when the Prof. tries to enter '36' into his code. But, in theory, it's sound.

student47
 4 years ago
Best ResponseYou've already chosen the best response.0definition of prime number===can't be exactly dividable except 1and prime number itself

student47
 4 years ago
Best ResponseYou've already chosen the best response.0more precisely... if x is a prime number let's say y=x1 then x%y is not 0 x%(y1) is not 0 x%(y2) is not 0 ... x%1 is only 0 (assumed y is bigger than 2) i.e) 3%2=1 but 3%1=0

andidious
 4 years ago
Best ResponseYou've already chosen the best response.0I've been stuck on this problem for a while too. Please check my logic: I started out with the numbers 2, 3, 5, 7. If the candidate_value was any of those, the script should instantly return as a prime number, and move onto the next. I then tried to check if any numbers divided by 2, 3, 5, 7 left any remainders. If so, it is also a prime number.

student47
 4 years ago
Best ResponseYou've already chosen the best response.0please! prime number definition is not "is it dividable by 2,3,5,7 or not?"! prime number asks you is it exactly dividable with 1 and prime number itself! don't you think 11 is also prime number?

student47
 4 years ago
Best ResponseYou've already chosen the best response.0i hope you study mathematics

andidious
 4 years ago
Best ResponseYou've already chosen the best response.0student47, that's the opposite of what i said. 11 would be prime since it isn't dividable by 2,3,5, or 7

andidious
 4 years ago
Best ResponseYou've already chosen the best response.0i hope you could read more carefully henceforth.

student47
 4 years ago
Best ResponseYou've already chosen the best response.0i want to ask you ' how about 121, is it prime number?'

claiminglight
 4 years ago
Best ResponseYou've already chosen the best response.0Heh I was just looking for one myself.

andidious
 4 years ago
Best ResponseYou've already chosen the best response.0oh wow, didn't think of that. thanks

andidious
 4 years ago
Best ResponseYou've already chosen the best response.0sorry, just a little frustrated.

claiminglight
 4 years ago
Best ResponseYou've already chosen the best response.0Does anyone know of a way I can send andi a hint about what he needs to be looking for without giving it away to tigger? Like a pm system?

claiminglight
 4 years ago
Best ResponseYou've already chosen the best response.0Put something on student47s post.

ushwia
 4 years ago
Best ResponseYou've already chosen the best response.0The question the helped me was, “what "happens" when a number that is prime is divided by a number that is not itself.” and “how is this different than what happen when a nonprime is divided by itself?” Merely saying it is/is not divisible by x doesn't help with the process. I'm stuck on the second part, though. i don't understant the log e**n question

malpaso
 4 years ago
Best ResponseYou've already chosen the best response.2ushwia, can you post your log question as a separate question? that way more people will see it. otherwise it will stay buried in this thread. thanks.

tony1404
 4 years ago
Best ResponseYou've already chosen the best response.0I am having the same problem with this one, 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..
Ask your own question
Sign UpFind 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
 Engagement 19 Mad Hatter
 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.