Man I actually did this back in school when I didn’t know about the % operator. That’s also exactly how I found out about % (through googling how to do it better) and I was amazed by my code being like 8000000 times faster lmao.
I know this looks bad, but I'm curious at the performance of it? Could the string conversion be more performant than your normal modulus method of even detection?
Edit: I just realized my "Bad Version" was using Go rune's instead of actual strings so was even a bit more optimized. When I did full raw strings it's even worse haha.
48
u/_u_whats_this Dec 04 '21
why is this nsfw
232
u/-MazeMaker- Dec 04 '21
This is downright elegant compared to some of the isEven posts on here.
2
u/Michax_Gaming Dec 05 '21
139
u/Masztufa Dec 04 '21
36
u/ChubbyPrincess87 Dec 04 '21
I can't even...
12
u/GreatBarrier86 Dec 04 '21
This is definitely not the worst I’ve seen. Primp it up with an if/else OR do an int[].Contains and it’s really not that bad.
19
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Dec 04 '21
Image Transcription: Code
I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!
9
u/Mteigers Dec 04 '21
I know this looks bad, but I'm curious at the performance of it? Could the string conversion be more performant than your normal modulus method of even detection?
Hmm. I kinda want to benchmark this.
5
u/SrineshNisala Dec 05 '21
Lucky we have is-even for JavaScript
2
u/toooof Dec 05 '21
why is this so adorable
2
u/TheRealSlimCoder Dec 05 '21
Can we just get a subreddit called ‘IsEven’ and be done with it?
These are getting annoying 😒
1
u/abrams666 Dec 05 '21
Would suggest to add an throw outofrangeexception
1
u/bistr-o-math Dec 05 '21
This is actually pretty neat. The author of the code is thinking in terms of patterns. Basically, what they did, is
(Not c# syntax, as I have no c# experience)
1
u/Sarimasak2000 Jan 18 '22
n%2==0 😭😭
0
u/[deleted] Dec 04 '21
[deleted] — view deleted comment