import re
def is_even(i: int) -> bool:
return re.match(r"-?\d*[02468]$", str(i)) is not None
this post was submitted on 31 Oct 2024
385 points (99.0% liked)
196
16774 readers
2311 users here now
Be sure to follow the rule before you head out.
Rule: You must post before you leave.
founded 2 years ago
MODERATORS
load more comments
(2 replies)
Ask AI:
public static boolean isEven(int number) {
// Handle negative numbers
if (number < 0) {
number = -number; // Convert to positive
}
// Subtract 2 until we reach 0 or 1
while (number > 1) {
number -= 2;
}
// If we reach 0, it's even; if we reach 1, it's odd
return number == 0;
}
load more comments
(4 replies)
When you sacrifice memory for an O(1) algorithm.
In this case still O(n)
load more comments
(1 replies)
Just divide the number into its prime factors and then check if one of them is 2.
I remember coding actionscript in Flash and using modulo (%) to determine if a number was even or odd. It returns the remainder of the number divided by 2 and if it equals anything other than 0 then the number is odd.
I believe that's the proper way to do it.
load more comments
(1 replies)
or divide the number by two and if the remainder is greater than
-(4^34)
but less than
70 - (((23*3*4)/2)/2)
then
true
load more comments
(4 replies)
load more comments
(1 replies)