Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
prime numbers
#21
(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
Reply
#22
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.
Reply
#23
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
Reply
#24
You've been here two years or so and haven't written a function?
Reply
#25
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...
Reply
#26
(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 ...
Reply
#27
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.
Reply
#28
okay, i understand, there's truth to what you say,
i'll try harder...
Reply
#29
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...
Yoriz likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  prime numbers with iterator and generator cametan_001 8 1,922 Dec-17-2022, 02:41 PM
Last Post: cametan_001
  Return prime numbers from range krzyfigh 2 1,943 Apr-20-2020, 08:08 PM
Last Post: krzyfigh
  Prime numbers Anderi02 1 1,989 Oct-13-2019, 04:49 PM
Last Post: ichabod801
  first k non-prime numbers arycloud 11 7,358 Jul-09-2019, 02:19 PM
Last Post: abhi19935
  first k non prime numbers print bsrohith 7 7,585 Jun-20-2019, 10:48 AM
Last Post: arycloud
  Print Numbers starting at 1 vertically with separator for output numbers Pleiades 3 3,763 May-09-2019, 12:19 PM
Last Post: Pleiades
  Finding prime numbers creslin_black 7 4,406 Jul-20-2018, 02:28 PM
Last Post: grjmmr
  Prime Numbers OmarSinno 1 4,390 Sep-23-2017, 05:29 PM
Last Post: ichabod801
  prime numbers generator is generating non prime numbers? ixaM 2 4,487 Dec-18-2016, 01:35 PM
Last Post: ixaM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020