Programming humor on reddit used to be excellent bits like this but then it devolved into new learners jumping straight to the irony they didn't understand and flooded the sub with nonsense.
I miss these bits.
btw it does get easier
import math def is_even(num): if num in [i for i in range(1000) if float(i)/2.0 == math.floor(float(i)/2.0)]: print("true") else: print("false")
Obviously one would need to increase the range for bigger numbers but this code is optimized.
def is_even(n): match n: case 1: return False case 0: return True # fix No1 case n < 0: return is_even(-1*n) case _: return is_even(n-2)
modulo
pseudocode:
if number % 2 == 0 return "number is even" (is_num_even = 1 or true) else return "number is odd" (is_num_even = 0 or false)
plus you'd want an input validation beforehand
who needs modulo when you can get less characters out of
while (number > 1) { number -= 2; } return number;
very efficient
edit: or theres the trusty iseven api
just check the last bit jesus christ, what is it with these expensive modulo operations?!
return !(n&1);
This code is terrible. If you input 10.66 it returns "number is odd
It should be:
if number % 2 == 0 return "number is even" (is_num_even = 1 or true) else return "number is not even" (is_num_even = 0 or false)
You joke, but I've seen a programming language that didn't have a loop, and if you copied a line of text and pasted it in a text editor, JSON would come out...
The editor could barely handle 400+ lines because it probably converted the text to JSON, added a letter and converted it back to JSON... Per inserted symbol...
My solution in perl back in the day when I was a teenage hobbyist who didn't know about the modulus operator: Divide by 2 and use regex to check for a decimal point.
if ($num / 2 =~ /\./) { return "odd" }
else { return "even" }