Is This Number Even?
Level 1: Simple, elegant, O(1)
x % 2 === 0
Level 2: Still optimal but now you're showing off
x & 1 === 0
Level 3: Enterprise-ready™
class NumberChecker {
constructor(value) { this.value = value; }
isEven() { return this.value % 2 === 0; }
}
new NumberChecker(x).isEven()
Level 4: When all you have is regex, everything looks like a pattern
/[02468]$/.test(x.toString())
Level 5: Hope x < 20000
const evens = Array.from({length: 10000}, (_, i) => i * 2);
evens.includes(x)
Level 6: Stack overflow for large numbers is a feature
function isEven(n) {
if (n === 0) return true;
if (n === 1) return false;
return isEven(Math.abs(n) - 2);
}
Level 7: Visual counting via regex
Array(x).fill('|').join('').match(/||/g).length === x/2
Level 8: Why calculate when you can SELECT?
-- First, insert all numbers up to 1 million into a table
SELECT is_even FROM number_properties WHERE value = ?
Level 9: Network latency adds gravitas
fetch('https://is-even-api.herokuapp.com/check/' + x)
.then(res => res.json())
.then(data => data.even)
Level 10: Immutable truth on the blockchain
// Deploy smart contract first
await evenChecker.methods.isEven(x).call()
// Gas fees may apply
Level 11: The wisdom of crowds
<iframe src="https://strawpoll.me/is-x-even"></iframe>
Level 12: 98.2% accuracy!
// After training on 10 million examples
model.predict([[x]])[0] > 0.5
Level 13: What did it cost?