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)
November 13, 2018 - 00:05
bool iseven(int x) { return (x & 1); }
November 13, 2018 - 00:09
November 13, 2018 - 00:11
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"); }
November 13, 2018 - 00:12
I'll take your bitlogic one sillier! bool iseven(int x) { return(x == (x >> 1)
November 13, 2018 - 00:12
November 13, 2018 - 00:15
what about negative numbers?
November 13, 2018 - 00:15
isEven = (num) => { const n = num * 2;
if (n % 2 === 0) { return "odd"; }
return "even"; }
November 13, 2018 - 00:16
November 13, 2018 - 00:16
nevermind I've figured it out
November 13, 2018 - 00:17
November 13, 2018 - 00:17
bool iseven(int n) { if (n==0) return true; if (n==1) return false; return iseven(n-2); }
November 13, 2018 - 00:18
Congrats, every input returns true
November 13, 2018 - 00:20
November 13, 2018 - 00:20
Every input returns "odd" I should say.
November 13, 2018 - 00:22
bool isEven(int n) { return ((n
November 13, 2018 - 00:23
November 13, 2018 - 00:24
bool isEven(int n) { return ((n>sizeof(int) * 8 - 1) == 0; }
November 13, 2018 - 00:28
isEven = (num) => { var t = num /2 - floor(num /2); return !t; }
November 13, 2018 - 00:32
November 13, 2018 - 00:33
Shake things up with terrible Clojure code. (defn isodd [number] ((zipmap (map char (range 48 58)) (cycle [false true])) (last (str number))))
November 13, 2018 - 00:48
November 13, 2018 - 01:04
isEven = (num) => { var t = num /2 - (num /2 | 0); return !t; }
November 13, 2018 - 01:09
Oh I see. That's a nice one
November 13, 2018 - 01:11
js let isEven = x => x % 2 === 0
cl (defun is-even (num) (equal (mod num 5) 0))
November 13, 2018 - 01:13
November 13, 2018 - 01:14
you may have missunderstood the idea of the thread
November 13, 2018 - 01:15
November 13, 2018 - 01:17
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
November 13, 2018 - 01:18
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.
November 13, 2018 - 01:18
return ! ( ( char ( n > 7 );
November 13, 2018 - 01:19
int iseven(int in){ int i; for (i=0;2*i
November 13, 2018 - 01:21
hahah fuck i love this one
November 13, 2018 - 01:22
function pajeetIsNumberOdd(int $n) : bool { return stripos((string)$n / 2, ".") !== false; }
Attached: PooInTheLooException.png (182x183, 72K)
November 13, 2018 - 01:23
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')
November 13, 2018 - 01:25
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); }
November 13, 2018 - 01:30
zero one two three four five six seven eight nine
word = numberToAlpha(n) even = word.endsWith(o,r,x,t) return even
November 13, 2018 - 01:39
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; }
November 13, 2018 - 01:41
when i try to post my code it says my ip is banned from Jow Forums
November 13, 2018 - 01:43
we filter pajeet code rajeesh :^)
November 13, 2018 - 01:44
also had a issue like "connection refused" sometime, maybe try without the code-tag
November 13, 2018 - 01:44
I think my answer is too autistic and its triggering some automated flag
pastebin.com /LpF5wCJL
November 13, 2018 - 01:47
November 13, 2018 - 01:48
November 13, 2018 - 01:49
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?
November 13, 2018 - 01:52
November 13, 2018 - 01:54
public static isEven(int n){ int nHalf = n/2; int nHalfTimesTwo = nHalf*2; return n==nHalfTimesTwo; }
November 13, 2018 - 01:55
November 13, 2018 - 01:56
can also been done with (-1)^n, it's basically the same idea
November 13, 2018 - 02:00
Haskell: evenLength [] = True evenLength (h:t) = not $ evenLength t
isEven n = if n
November 13, 2018 - 02:07
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?
November 13, 2018 - 02:09
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))
November 13, 2018 - 02:18
Floor[e^(Log[|n|]-Log[2])]==e^(Log[|n|]-Log[2])
And yes it works for n = 0
November 13, 2018 - 02:55
November 13, 2018 - 03:12
Cos[n*π] == 1
And Sin[n*π] == 0
November 13, 2018 - 03:28
xor rdx, rdx mov rax, rcx mov rcx, 2 div rcx mov rax, rdx not rax ret
November 13, 2018 - 03:32
November 13, 2018 - 03:38
never mind i figured it out
November 13, 2018 - 03:42
November 13, 2018 - 03:47
not done yet so function isEven(n){ return [0, 2, 4, 6, 8].map(n=>n+'').includes((n+'')[(n+'').length - 1]) }
November 13, 2018 - 03:48