Posts: 7,313
Threads: 123
Joined: Sep 2016
Nov-06-2022, 11:17 AM
(This post was last modified: Nov-06-2022, 11:17 AM by snippsat.)
(Nov-06-2022, 02:45 AM)deanhystad Wrote: I would write like this: It's ok,but in first post he try to print all number given from user input.
Think is cleaner to have a is_prime function,
then last part of code is same as his first post with added is_prime .
def is_prime(p: int) -> bool:
for n in range(2, p):
#for n in range(2, int(p ** 0.5) + 1): # Faster
if p % n == 0:
return False
return True
if __name__ == '__main__':
user_input = int(input("choose a number: "))
for x in range(2, user_input+1):
if is_prime(x):
print(f"{x} A prime,divide only by 1 and {x}")
else:
print(f"{x} Not prime") Output: λ python prime.py
choose a number: 20
2 A prime,divide only by 1 and 2
3 A prime,divide only by 1 and 3
4 Not prime
5 A prime,divide only by 1 and 5
6 Not prime
7 A prime,divide only by 1 and 7
8 Not prime
9 Not prime
10 Not prime
11 A prime,divide only by 1 and 11
12 Not prime
13 A prime,divide only by 1 and 13
14 Not prime
15 Not prime
16 Not prime
17 A prime,divide only by 1 and 17
18 Not prime
19 A prime,divide only by 1 and 19
20 Not prime
Posts: 6,779
Threads: 20
Joined: Feb 2020
Nov-06-2022, 12:23 PM
(This post was last modified: Nov-06-2022, 12:23 PM by deanhystad.)
I disagree with your interpretation of what the first post was meant to accomplish, I think it was meant to test if the input number is prime, but heartily agree that this should be a function.
Posts: 230
Threads: 39
Joined: Mar 2020
okay guys, thank you,
what can i say - i don't think i would've been able to come up with this kinda complexity of code, i really am just beginning with coding...
but your examples are very helpful,
for example - i didn't know that taking a number to the power of **0.5 would bring it to it's square root...
and besides - i never wrote a function,
but in anyway - i will read more about the for loop (any recommendation for material ?)
thank you very much
Posts: 1,838
Threads: 2
Joined: Apr 2017
You've been here two years or so and haven't written a function?
Posts: 230
Threads: 39
Joined: Mar 2020
hehe....i haven't been here the whole time, i touched on it a little back then (2 years ago) and then continued with my business and got back to it recently....so all in all i learned python for very little time...
Posts: 230
Threads: 39
Joined: Mar 2020
(Nov-06-2022, 02:45 AM)deanhystad Wrote: Sorry, I gave bad advice. The test for == 2 should be done first. Then I can add a test for even that makes it easier to only test odd factors.
I would write like this:
while True:
user_input = int(input("Enter Number "))
if user_input == 2: # Test first
print("is prime")
# No number < 2 is prime. No even number, other than 2, is prime.
# Doing this test 2nd lets me report that 2 is prime.
elif user_input < 2 or user_input & 1 == 0:
print("is not prime")
else:
# Test all odd numbers between 3 and square_root(user_input)
for x in range(3, int(user_input**0.5)+1, 2):
if user_input % x == 0:
print('is not prime')
break # Found a factor. No reason to test any more factors
else:
# If no factor found, must be prime
print("is prime")
i didn't get why in the line for x in range(3, int(user_input**0.5)+1, 2): there is +1 ...
Posts: 6,779
Threads: 20
Joined: Feb 2020
Nov-07-2022, 08:35 PM
(This post was last modified: Nov-07-2022, 08:35 PM by deanhystad.)
It has to do with how range() works. When you call range(start, end, step) the results includes start and numbers up to, but not including end.
The square root of 25 is 5. If I do "for x in range(3, 5, 2):" the only factor tested is 3. If I change that to "for x in range(3, 6, 2):" the code tests 3 and 5.
This is not a question you should be asking on this forum. This is a question you should be trying to figure out yourself. If I didn't know the answer I would write a program like this:
number = 25
for i in range(3, int(number**0.5), 2):
print(i, end=", ")
print()
for i in range(3, int(number**0.5)+1, 2):
print(i, end=", ")
print() Output: 3,
3, 5,
If you do research and perform experiments to answer these kinds of questions your Python knowledge will grow much faster than if you ask questions on the forum. I don't think you are learning much here. I think most of what we say is in one ear and out the other because you don't really understand much of what is said. That won't be the case when you are the one providing the answer. So feel free to ask questions, but only after you've made a significant effort to answer the question yourself. And when you do post, make sure you reference the work you've done.
Posts: 230
Threads: 39
Joined: Mar 2020
okay, i understand, there's truth to what you say,
i'll try harder...
Posts: 230
Threads: 39
Joined: Mar 2020
btw, i have to say, one of the reasons i ask questions on the forum is because it gives me a sense of being helped by a community,
i don't have anyone else in my life who wants to learn python with me (i suggested it to a few friends but they were not up to it),
so communicating here in the forum gives me a sense of company, and not having to deal with everything solely on my own...
|