115
Rule (lemmy.blahaj.zone)
submitted 3 hours ago by Persona3Reload to c/196
top 19 comments
sorted by: hot top controversial new old
[-] Euphoma@lemmy.ml 21 points 2 hours ago

return true

is correct around half of the time

[-] ImplyingImplications@lemmy.ca 2 points 6 minutes ago
assert IsEven(2) == True
assert IsEven(4) == True
assert IsEven(6) == True

All checks pass. LGTM

[-] ashestoashes 3 points 1 hour ago

just check the least significant bit smh my head

[-] superkret@feddit.org 26 points 3 hours ago

Just divide the number into its prime factors and then check if one of them is 2.

[-] fartripper@lemmy.ml 10 points 2 hours ago* (last edited 2 hours ago)

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
[-] superkret@feddit.org 2 points 2 hours ago

What if the remainder is greater than the first, but not less than the latter?

Like, for example, 1?

[-] prime_number_314159@lemmy.world 1 points 52 minutes ago

Then you should return false, unless the remainder is also greater than or equal to the twenty second root of 4194304. Note, that I've only checked up to 4194304 to make sure this works, so if you need bigger numbers, you'll have to validate on your own.

[-] fartripper@lemmy.ml 1 points 10 minutes ago

i hate to bring this up, but we also need a separate function for negative numbers

[-] tipicaldik@lemmy.world 8 points 2 hours ago

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.

[-] Korne127@lemmy.world 7 points 2 hours ago

Yeah. The joke is that this is the obvious solution always used in practise, but the programmer is that bad that they don't know it and use some ridiculous alternative solutions instead.

[-] superkret@feddit.org 11 points 2 hours ago

I believe that's the proper way to do it.

[-] bob_lemon@feddit.org 9 points 2 hours ago* (last edited 2 hours ago)
import re

def is_even(i: int) -> bool:
    return re.match(r"-?\d*[02468]$", str(i)) is not None
[-] dadarobot@lemmy.sdf.org 7 points 2 hours ago
If number%2 == 0: return("Even")
Else: return("odd") 
[-] istdaslol@feddit.org 8 points 2 hours ago

When you sacrifice memory for an O(1) algorithm.

In this case still O(n)

[-] lnxtx@feddit.nl 7 points 2 hours ago

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;
}
[-] Sanctus@lemmy.world 9 points 2 hours ago

Anything but using modulo I guess

This makes me happy that I don’t use genai

this post was submitted on 31 Oct 2024
115 points (100.0% liked)

196

16409 readers
1835 users here now

Be sure to follow the rule before you head out.

Rule: You must post before you leave.

^other^ ^rules^

founded 1 year ago
MODERATORS