There was thread a few nights ago where someone was asking how to check if a number was odd or not...

There was thread a few nights ago where someone was asking how to check if a number was odd or not. I thought it was kind of interesting because people started giving some wild answers that weren't necessarily optimal, but were certainly an interesting way of a approaching the problem. So here's my challenge Jow Forums:

Come up with as many unique ways of determining if a given number (we will assume it's an integer) is divisible by 2.

Rules:
>time complexity doesn't matter
>the solution must be complete (work for all valid inputs)

I'll start with the obvious answer:


def is_even(n):
return not(n % 2)

Attached: modulus.png (225x225, 4K)

Other urls found in this thread:

pastebin.com/LpF5wCJL
twitter.com/NSFWRedditGif

bool iseven(int x)
{
return (x & 1);
}

That would be isodd

public static boolean isEven(Integer i) {
if (i.equals(new Integer(0)) return true;
else if (i.equals(new Integer(1)) return false;
// ...
else if(i.equals(new Integer(2147483647)) return false;
else throw new IllegalArgumentException("I don't know negative numbers");
}

I'll take your bitlogic one sillier!
bool iseven(int x)
{
return(x == (x >> 1)

>what is completeness

what about negative numbers?

isEven = (num) => {
const n = num * 2;

if (n % 2 === 0) {
return "odd";
}

return "even";
}

return (x/2)!=((x-1)/2)

nevermind I've figured it out

really makes me think

bool iseven(int n) {
if (n==0) return true;
if (n==1) return false;
return iseven(n-2);
}

Congrats, every input returns true

iSeven

Every input returns "odd" I should say.

bool isEven(int n) {
return ((n

??

this is always true

bool isEven(int n) {
return ((n>sizeof(int) * 8 - 1) == 0;
}

isEven = (num) => {
var t = num /2 - floor(num /2);
return !t;
}

sorry Math.floor();

Shake things up with terrible Clojure code.
(defn isodd [number]
((zipmap (map char (range 48 58)) (cycle [false true]))
(last (str number))))

assume integer math

isEven = (num) => {
var t = num /2 - (num /2 | 0);
return !t;
}

Oh I see. That's a nice one

js
let isEven = x => x % 2 === 0


cl
(defun is-even (num) (equal (mod num 5) 0))

Five should be a two.

you may have missunderstood the idea of the thread

lel

with accumulators
def iseven(n):
n = abs(n)
accum1 = 0
accum2 = 0
parity_flag = True
while n != 0:
if parity_flag:
n = n - 1
accum1 = accum1 + 1
parity_flag = False
else:
n = n - 1
accum2 = accum2 + 1
parity_flag = True
return accum1 == accum2

z = i^n
Im[z] == 0

Where i is the square root of -1 and n is an integer. Returns true for even n and false for odd n.

return ! ( ( char ( n > 7 );

int iseven(int in){
int i;
for (i=0;2*i

hahah fuck i love this one

function pajeetIsNumberOdd(int $n) : bool {
return stripos((string)$n / 2, ".") !== false;
}

Attached: PooInTheLooException.png (182x183, 72K)

def pajeet_iseven(n):
s = str(n)
return (s[-1] == '0') or (s[-1] == '2') or (s[-1] == '4') or (s[-1] == '6') or (s[-1] == '8')

fun isEven(n: Int): Boolean {
var string = n.toString();
var list = charArrayOf('0','2','4','6','8');
var lastDigit = string.last();
return list.contains(lastDigit);
}

zero
one
two
three
four
five
six
seven
eight
nine

word = numberToAlpha(n)
even = word.endsWith(o,r,x,t)
return even

Oh I have to run with this...
#include
#include

bool iseven(int number) {
sqrt(pow(-1, number));
if (errno != EDOM)
return true;
errno = 0;
return false;
}

when i try to post my code it says my ip is banned from Jow Forums

we filter pajeet code rajeesh :^)

also had a issue like "connection refused" sometime, maybe try without the code-tag

I think my answer is too autistic and its triggering some automated flag

pastebin.com/LpF5wCJL

probably the eval

yeah thought so

function pooIsNumberOdd(int $n) : bool
{
$skip = true;
for ($i = PHP_INT_MIN; $i < PHP_INT_MAX; $i++) {
if ($skip) {
$skip = false;
continue;
}

if ($n === $i)
return true;

$skip = true;
}

return false;
}


does it count if it runs in theory but requires ridiculous amounts of ram?

what did he mean by this

public static isEven(int n){
int nHalf = n/2;
int nHalfTimesTwo = nHalf*2;
return n==nHalfTimesTwo;
}

great one

can also been done with (-1)^n, it's basically the same idea

Haskell:
evenLength [] = True
evenLength (h:t) = not $ evenLength t

isEven n = if n

npm install --save is-odd


const isOdd = require('is-odd');

console.log(isOdd('1')); //=> true
console.log(isOdd('3')); //=> true


What's the matter user? Too smart for web programming?

recursive definition based on the fact that

Even + Even = Even, Odd + Odd = Even, and Odd + Even = Odd


def iseven(n):
if n == 1:
return False
if n == 2:
return True
return iseven(math.floor(n/2.0)) == iseven(math.ceil(n/2.0))

Floor[e^(Log[|n|]-Log[2])]==e^(Log[|n|]-Log[2])

And yes it works for n = 0

f(x)= x^|n|
f(x)==f(-x)

Cos[n*π] == 1

And
Sin[n*π] == 0

xor rdx, rdx
mov rax, rcx
mov rcx, 2
div rcx
mov rax, rdx
not rax
ret

e^(i*n*pi) == 1

never mind i figured it out

nice

not done yet so
function isEven(n){
return [0, 2, 4, 6, 8].map(n=>n+'').includes((n+'')[(n+'').length - 1])
}