Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23160

Which method to use when filter odd and even numbers? [closed]

$
0
0

Apparently we may have two methods to filter odd an even numbers.
The distinction between them is how they consider number zero 0 case.

Method 1:

n/2 = (q, r) => 2 * q + r = n

For n >= 0:
n is odd, if r > 0
n is even, if r = 0

This method will consider zero 0 to be an even number.

Method 2:

n/2 = (q, r) => 2 * q + r = n

For n >= 0:
n is odd, if q >= 0 and r > 0
n is even, if q > 0 and r = 0
n = 0 is neutral, if q = 0 and r = 0

This method will consider zero 0 to have neutral parity, meaning counting neither an odd nor an even number of units 1 corresponding to counted objects present.

Programming Method 1:

while True:    a = int(input("a = "))    Aa = abs(a)    r = Aa % 2    if r > 0: print(a, "odd")    elif r == 0: print(a, "even")    else: pass

Output:

me@amadeus:~$ python3 Templates/A0a = 33 odda = 22 evena = 11 odda = 00 evena = 

Programming Method 2:

while not 0:    a = int(input("a = "))    Aa = abs(a)    q = Aa // 2    r = Aa % 2    # q, r = divmod(Aa, 2)    if q >= 0 and r > 0: print(a, "odd")    elif q > 0 and r == 0: print(a, "even")    elif q == 0 and r == 0: print(a, "neutral")    else: pass

Output:

me@amadeus:~$ python3 Templates/A1a = 33 odda = 22 evena = 11 odda = 00 neutrala =

Addendum:

Therefore, resulting such ironic dialog example.

- Do You have a wife?
- I have an even number of them.
- It is illegal to have an even number of wives.
- I have no choice, because somebody consider "0 is even".
- "I have to live eternal life, and after die, to find, what's love".
I composed this simple poem, because I want to share with You,
🍏☕ and say "Apple and Tea make a Tea+m". 🍒🙃


Viewing all articles
Browse latest Browse all 23160

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>