0
I need help writing a Python function for checking Even, Odd and Prime numbers
I could write 2 separate codes for even/odd and prime numbers but need some help with logic in integrating them into a single function. Please don't paste your own or complete code as I would like to use that little help from you to build the complete function.
4 Respostas
+ 1
Break it down..
declare a function..
def func():
inside your function, take user input and assign it to a variable. run the variable thru if/else statements, and print the appropriate output. now run the variable thru another set of if/else to figure out if it is a prime number and then give the appropriate printout.
I can give you an example code, but you asked not too.. if you want to see, I will gladly throw something together
0
you want to sort them into evens and Odds and then primes? like this?
input:
numbers = 12345678910
output:
evens = 246810
odds = 13579
primes = 2357
correct?
0
No, actually I would be asking the user to input a number and then would like to check against prime/odd/even and print an output saying 'the entered no. is prime/even/odd'.
0
Remember that 0 is even.
Remember that 2 is the only even prime.
If you have sorted that out, check if the number is even: number % 2 == 0
If it is not even, it is odd.
If it is odd, it might be prime.
Check by division, but do not waste time: Divide by odd numbers only.Check first if the input is a square number, like 25, 49, or 169. That saves looping. Don't divide by anything larger than the square root of the input.