O(1) would mean it has the same running time for all values. This function is actually O(n/2). Calling isEven(1000) is going to take about 500 times longer than isEven(0).
It also makes calculating time complexity fuckin' impossible. n/2 isn't even correct. if you want a number that can be compared directly to other numbers, you'd have to figure out exactly what operations are necessary and how long each one takes, for each value of n. Those numbers change on different systems, so it wouldn't be a perfect comparison even then.
Mostly the reason was, that C or C++ wouldn't have offered a way to ridiculously abuse the DOM. I mean, probably yes, but I wouldn't have known the necessary APIs from memory.
It looks like this comment contains a code block delimited with triple backticks. Unfortunately reddit does not have universal support for this syntax and your comment will not render correctly on old reddit and most mobile apps.
For the benefit of people on old reddit, this link will take you to a correct rendering of the comment.
/u/jogrohh, it would be appreciated, but not required, if you could edit your comment to use the more compatible four space indention format. For single lines or inline code you can use single backticks.
If this is C/C++, then you'll get nasal demons. Signed integer overflow is undefined behavior. The compiler is unlikely to do anything that would break this particular function, but you can't be entirely sure it won't either.
Itâs probably Java right? Edit: as a couple people pointed out, actually probably csharp, cuz Java functions that return Booleans say âBooleanâ, and csharp functions that return booleans say âboolâ
You can drop the -dir at the and and it will sound more natural, this way it sounds like a newspaper headline or meme language equilavent of "when coffe is needed in the morning".
I'm Impressed that you are learning Turkish. Are you studying in Turkey? What languages do you speak?
constexpr auto is_even(const auto n) noexcept -> bool {
return not (n & 1);
}
(C++)<pedantic>This is the only right way of doing it. Stop using modulo for god sakes it can only ever be slower. (On arm and also on amd64) That's why clang does that optimization itself if you turn optimization onâŠ</pedantic>
Division/modulo by a constant power-of-two is such a common thing that pretty much all compilers optimize it automatically. If it compiles the same both ways, there's not much reason to not go with the more readable option. Generally, trying to out optimize the optimizer can easily go wrong unless you really know what you're doing and have tested it on all of the build systems you use.
I'm expecting everyone here is knowledgeable enough to understand what's going on. The problem that using the bitwise `and` solves is that when you ship a library you don't know what optimization the dev will use, or even the platform they'll run it on.
In any case, if it's in the "critical path", I won't guess, I'd just write a benchmark, then it's basically documenting itself because both the bitwise, and modulo idioms need to be benchmarked.
On x86-64, the few times it was in the middle of some very involved code and I benchmarked it the bitwise was faster. I'm not saying it's guaranteed to always be the case, just benchmark it.
Yeah, I've caught situations where the compiler very easily should've vectorized some code but because of another pointer dereference in there it didn't fit the "template" code the compiler looks for to optimize and therefore wasn't vectorizing the code at all. The compiler's ability to optimize code is limited to every permutation of an optimizable situation being foreseen by the coders responsible for the compiler's ability to optimize. It doesn't magically detect the optimal way to generate instructions for every single situation.
It's just like how spoken language translators still fail to properly conjugate or whatever else, even today, with teams of people working on the problem with machine learning for years and years. Compilers expect code to be situated a certain way in order for optimizations to kick in for some things.
It seems that your comment contains 1 or more links that are hard to tap for mobile users. I will extend those so they're easier for our sausage fingers to click!
It looks like this comment contains a code block delimited with triple backticks. Unfortunately reddit does not have universal support for this syntax and your comment will not render correctly on old reddit and most mobile apps.
For the benefit of people on old reddit, this link will take you to a correct rendering of the comment.
/u/Bit5keptical, it would be appreciated, but not required, if you could edit your comment to use the more compatible four space indention format. For single lines or inline code you can use single backticks.
7
u/Legendary-69420 Aug 23 '21
If you think that is bad, look at this: https://github.com/samuelmarina/is-even
286
u/Knuffya Aug 22 '21 edited Aug 22 '21
Ya'll are inefficient. Here's how the pros do it:
139
u/YourUsualSir Aug 22 '21
isEven(-1)
67
u/AdminYak846 Aug 22 '21
That code isn't event JavaScript like the post is tagged as, the dead give away being the fucking "int" type for the parameters.
24
u/[deleted] Aug 22 '21
Image Transcription: Twitter Post
Bora KAĆMER, @CoderBora
Bu nasıl çift sayı bulma Ćeklidir. Sabah sabah beynim kanadı :)
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!
22
u/GreyBeard4096 Aug 23 '21
Sabah kahve gereklidir. :-)
Still learning Turkish, but hopefully that says "in the morning coffee is a requirement". :-D
22
u/Mr2-1782Man Aug 23 '21
Why is this even posted here? This isn't bad code, its joke code. Hell this thing breaks at least two of the subs rules.
7
u/Melon_Chief Aug 23 '21
(C++)<pedantic>This is the only right way of doing it. Stop using modulo for god sakes it can only ever be slower. (On arm and also on amd64) That's why clang does that optimization itself if you turn optimization onâŠ</pedantic>
1
u/[deleted] Aug 23 '21
What about?
bool IsEven(int num) { return (num%2 == 0) }3
u/hroptatyr Aug 23 '21
I like this approach! This code can be elegantly adapted when They ever change the definition of even-ness.
Also I want to see this guy's take on
isPrime!2
u/chinpokomon Aug 23 '21
I prefer my method fragment from a few days ago in Javascript.
2
u/Voltra_Neo Aug 23 '21
I hate that in Java it works fine due to overflow
2
u/chestera321 Aug 23 '21
When you are programmer on a level that you act like remainder operator doesn't exist in nature :D
2
u/Bit5keptical Aug 23 '21
Well at least he used
even = !evenInstead of the good old
1
u/TheBenStA Aug 23 '21
Could you not just do number%2 and invert it?