andioop@programming.dev to Programming Horror@programming.dev • edit-29 months agoGod I wish there was an easier way to do thisimagemessage-square23 fedilinkarrow-up194
arrow-up194imageGod I wish there was an easier way to do thisandioop@programming.dev to Programming Horror@programming.dev • edit-29 months agomessage-square23 Commentsfedilink
minus-squarerecursive_recursion [they/them]@programming.devhexbear5·edit-29 months agomodulo 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 linkfedilink
minus-squaremac@programming.devhexbear9·edit-29 months agowho 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 linkfedilink
minus-squareNullPointer@programming.devhexbear5·9 months agohere is somewhat less: return (number % 2) == 0; linkfedilink
minus-squarehuf [he/him]hexbear4·9 months agojust check the last bit jesus christ, what is it with these expensive modulo operations?! return !(n&1); link
minus-squareperviouslyiner@lemm.eehexbear1·9 months agoare the negative numbers all even? linkfedilink
minus-squareRandomVideos@programming.devhexbear1·9 months agoThis 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) linkfedilink
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
here is somewhat less:
return (number % 2) == 0;
just check the last bit jesus christ, what is it with these expensive modulo operations?!
return !(n&1);
are the negative numbers all even?
🤦
are u a wizard?
John carmak posting
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)