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)
I've looked at other's answers and it all seems so complicated. Any additional reading to suggest?
one just simple hint: prime numbers are not dividable exactly except for only two numbers-prime 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
first suggestion is throw away your code entirely. write out what you are trying to do in pseudo-code. for example, if the assignment were to write a program that adds any two numbers provided as input, your pseudo-code 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 pseudo-code 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 pseudo-code as a series of steps, then translate it into code.
I 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
When 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 pseudo-code, 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.
I've googled it and this: for x in range(2,int(n**0.5)+1): Makes absolutely no sense to me.
This 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
This 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
Forget 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?
If it's divisible by 2 and/or 3 and/or 5 then it's not a prime.
Good. 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.
So can you try again? How would test to see if 17 is or is not a prime number? And why?
malpaso...you are trying really hard to give instruction...clap clap clap
I 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.
definition of prime number===can't be exactly dividable except 1and prime number itself
more precisely... if x is a prime number let's say y=x-1 then x%y is not 0 x%(y-1) is not 0 x%(y-2) is not 0 ... x%1 is only 0 (assumed y is bigger than 2) i.e) 3%2=1 but 3%1=0
I'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.
please! 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?
i hope you study mathematics
student47, that's the opposite of what i said. 11 would be prime since it isn't dividable by 2,3,5, or 7
i hope you could read more carefully henceforth.
i want to ask you ' how about 121, is it prime number?'
Heh- I was just looking for one myself.
oh wow, didn't think of that. thanks
sorry, just a little frustrated.
Does 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?
Put something on student47s post.
The 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 non-prime 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
ushwia, 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.
I 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..
Join our real-time social learning platform and learn together with your friends!