Prime number finder
I have some python prime number code which finds out prime numbers. I was looking for a way to make it more efficient as this currently checks for all factors until it reachers the number, it possible could you make the explainations simple as I am 13. Here is the code:
import time print('Made by Yasas Kulatunga, Dhari Prashanth and Dhivy Prashanth') print(' ') number = int(input("Enter a Number: ")) print(' ') time.sleep(0.5) print('THINKING.') time.sleep(0.5) print('THINKING..') time.sleep(0.5) print('THINKING...') time.sleep(0.5) print(' ') c = 0 while number: for i in range(2,number): if number % i == 0: print(str(i),"times",str(number//i),"is",str(number)) time.sleep(0.3) c = c + 1 if c == 0: print(' ') print(str(number),"has no factors apart from 1 and itself, therefore it is a prime number") print(' ') time.sleep(0.3) else: print(' ') print(str(number),"is not a prime number, the number has factors more than 1 and the number itself.") print(' ') time.sleep(0.3) number = int(input('Enter a number: ')) print(' ') time.sleep(0.3) c = 0
The exact output is this, although it is from the original code I posted on top: Enter a Number: 25
5 times 5 is 25
25 is not a prime number, the number has factors more than 1 and the number itself.
Enter a number:
Sorry, I had tested the last code of @ramvee until √ - 1
But he was almost right, you have to check only until sqrt(number),
And, what I just added, by skipping the even divisors!
For instance, if number is 29, your code checks the division by
2 3 4 5 6 7 8 9 10 11 12 13 ...29
But "mine" (& @ramvee) only
2 3 5
@cvp, If you go the the three vertical dots at the upper right of your forum post, you can edit it to add new insights. This is probably a better approach than replying to your own posts.
@ccc, Thanks, I knew it but sometimes I forget it, and sometimes I'm not sure how other people see an update, while I'm sure they see a new response
Thank you for pointing the bug in my code. :)
I put in your line of code
for i in +list(range(3, maxfact+1,2)):
and it works fine,
Except it shows 2 is not a prime.. If we sort this we're good.. I think..
I think the consecutive 3 post's from @cvp might have been better done in edit. Maybe and maybe not. If the items inside a post themselves were also threaded , then it would be clear to do like that. But they are not. Just saying it's not a clear cut choice.
@cvp , lol, yes he is normally right. But need to keep him on his toes. 😱😈