I opened raw and waited for the whole thing to load. It literally goes to 375,000 and they've also added hard-coding to each number to recognize variants like "seven", "Seven", "SEVEN", etc.
Ok, but why not just loop through an array of all the even numbers you can think of then? Pretty easy to manually add another number to a static array if you ask me. Easier than adding a whole new line of code anyway.
That‘s an interesting point because addition/subtraction is O(log n) on modern hardware because it can be done in parallel whereas division is (presumably) also O(log n) since the number reduces by half every time.
However, I don’t think it would be faster, for a few reasons:
The wiki article on modulo mentions that n % 2 can actually be turned into a series of bitwise ands which makes it blazing fast compared to division. This implies that the compiler can perform even more tricks specific to the hardware if it knows you want even/odd, so using modulo signals your intention better.
branch prediction is also costly: modern hardware has sophisticated ways of predicting which branch to go down, but if it chooses the wrong path, it can cost a cycle or two. This improves over time once the pattern of the algorithm is “learned” by the hardware, but in general, more assembly instructions is worse performing.
it’s harder to write and debug so you waste human time!
In computing, the modulo operation returns the remainder or signed remainder of a division, after one number is divided by another (called the modulus of the operation). Given two positive numbers a and n, a modulo n (abbreviated as a mod n) is the remainder of the Euclidean division of a by n, where a is the dividend and n is the divisor. The modulo operation is to be distinguished from the symbol mod, which refers to the modulus (or divisor) one is operating from.
Never faster than modulo, but some compilers can compile it as to not blow up the stack. There's an optimization called Tail Recursion, mostly employed by functional languages.
You joke. But, the standard unit of measure for productivity for a lot of places is normally SLOC/hr. SLOC is used in costing basis of estimate and it's how development progress is measured. We've actually run into issues with writing effecient, well designed OO code, and had scrunity of "how is it possible you wrote X SLOC to complete all these requirements?" because we are like 1/2 the normal SLOC count.
What the fuck, where does this happen? That's like counting how many managers it takes to screw in a light bulb by how many cups of coffee was consumed that day.
poor productivity could correlate with poor morale or poor training or some other problem that is actionable and would make the whole environment healthier if solved.
That being said: people measuring productivity might be measuring the wrong thing, and it's (imo) only ethical if used as a diagnostic or a trailing indicator for some other problem. Knowing that there's a productivity problem means needing to investigate and fix something else.
I would not trust most managers to not abuse the metric. But I have trust issues.
It was downvoted because that’s the obvious answer, and one of the few actually senseful answers — while most users in these comments are just joking around with answers that would not make too much sense in reality. Like what OP posted.
What built-in method - can you elaborate? Of course, if you really don't want to do modulo, then you can use bit operations (~(x&1)) but I don't think will be any significant difference in performance either way.
Yeah and I wonder who wrote it, it might be from a student project in which case the educators failed the student or else someone fabricated it for karma
I remember that people from a group I was were running an isEven context where the winner was the one who could build it in the most creative/uneccessarily complex way lol
It also gets reposted here every two or three days
If I'm correct it's from a library of checking if a number us even. It was made as a joke but they have gotten so far with it. Google github is even or something like that
A function in Javascript to check if a number is even that's only...er...94.8MB. The guy who wrote it claims:
This is a 100% serious project, and it is made to help the community. There are a lot of cases where young developers face the issue of finding if a number is even, so I decided to help them by creating this beautiful package. Hopefully, someday it will be completed...
He naturally has another package that checks if a number is odd - that one relies on 10 JS files that total 761.5MB...but let's just hope you're not wanting to check if multiples of 2,000,000 are odd, because this monstrosity of a package doesn't account for that:
function isOdd(number){
if (number < 2000000){
return twoMil(number);
}
else if (number < 4000000 && number > 2000000){
return fourMil(number);
}
else if (number < 6000000 && number > 4000000){
return sixMil(number);
}
... (this goes on up to 20,000,000 but with the same fundamental error in every if block)
It is real this repository was made by a youtuber named smauel miller as joke but now it has more than 1 million lines of code.P.S. I contributed 100 numbers
Reminds me of the time my computer science teacher made an assignment where we had to write an is even function using recursion and banned the modulo operator
The compiler/interpreter is smart enough to convert between the two, so they are identical. In this case it's the JS engine, and I believe this optimization exists in all 3 major js engines.
This is actually really bad code! We should create a database with tables for even and non-even numbers. With this approach we can simply tell true if the num equals an entry from table even and false if num equals an entry from table non-even!
edit: and we don't have to change the script for new numbers!
if (num % 2 == 0 || num == 0) {
return true;
} else {
return false;
}
% is called the modulo operator, and x % y returns the remainder of dividing x by y.
|| is the logical or operator in c++, which means that the if argument will be true if either one of them are correct. Check this for your language, as some languages use different syntax such as ‘or’ in python.
I wrote explicit returns for quite a while until I realized what I was actually doing. It's probably more clear to a beginner, but ultimately takes more code for the same result.
760
u/Bodine12 Sep 04 '21 edited Sep 04 '21
What I like about this is its maintainability. As we discover new numbers, they can simply be tacked on at the end.
128
u/Cristichi wicked Sep 04 '21
Switch (num) { case 0: case 2: case 4: ... case 500: return true;
case 1: case 3 case 5: ... case 499: return false; }
112
u/FlamboyantApproval16 Sep 04 '21 edited Sep 04 '21
When you are paid for every line of code :
52
u/robin_888 Sep 04 '21
Filesize: ℵ₀ Bytes
88
u/bootje_wolf Sep 04 '21
They should just use a lookup table. Duhh... /s
74
u/FromWayDownUnder Sep 04 '21
Why not just return !isOdd(num)?
47
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Sep 04 '21 edited Sep 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!
15
u/Spynder shameless stealing content to r/YandereTechnique Sep 04 '21
This is r/YandereTechnique
26
u/[deleted] Sep 04 '21
This gets posted every week.
9
u/haltmich Sep 04 '21
All of isEven/isOdd functions are memes.
I remember that people from a group I was were running an isEven context where the winner was the one who could build it in the most creative/uneccessarily complex way lol
It also gets reposted here every two or three days
7
u/[deleted] Sep 04 '21
Dudes gonna flip when he hears about the modulo operator.
7
u/[deleted] Sep 04 '21
TODO: "IMPLEMENT 25 AND UP"
7
u/Johanno1 Sep 04 '21
10
u/Nisox64 Sep 04 '21
There is no way this is real
17
u/Akantor47 Sep 04 '21
Repost
https://www.reddit.com/r/programminghorror/comments/p1mxog/ever_heard_of_modulo/?utm_medium=android_app&utm_source=share
3
u/mwk0408 Sep 04 '21
https://github.com/samuelmarina/is-even
3
u/[deleted] Sep 04 '21
repost
3
u/horenso05 Sep 04 '21
My favourite right way is: return num & 1 == 0
3
u/Apprehensive_Pace775 Sep 04 '21
This is day 1 coding skills 😂
3
u/pixtxa Sep 04 '21
I cas do better!!! https://twitter.com/Pixtxa/status/1289746201395392515
2
u/syamgamelover Sep 04 '21
To infinity and beyond.
2
u/gvs311 Sep 04 '21
What's the end number?
2
u/Tornado547 Sep 04 '21
Reminds me of the time my computer science teacher made an assignment where we had to write an is even function using recursion and banned the modulo operator
2
u/Woah_Moses Sep 04 '21
2
u/[deleted] Sep 04 '21
what the frick atleast provide source
2
u/Chaoslab Sep 04 '21
Mmmmm mmmmm. Unroll that shit! /s
2
u/MinecrAftX0 Sep 04 '21
% operator please ffs
2
u/cat_hast Sep 04 '21
%
2
u/Super-administrator Sep 04 '21
Not only is the name descriptive. It also has a comment. Pfft.
2
u/BonerForest25 Sep 05 '21
Yeah i know right? Can just be a bunch of if’s without the “else” for uniformity. The “else” statements are totally useless.
2
u/jolharg Sep 05 '21
As well as %2 there's &1, wonder if it's faster
2
u/Bo_Jim Sep 05 '21
return(!(num & 1));
2
u/Ethanlac Sep 06 '21
POV: Your teacher hasn't taught you about the modulo operator yet.
4
u/[deleted] Sep 04 '21
2
u/[deleted] Sep 04 '21
Me with my code from last week:finals a worthy opponent.
2
u/propostor Sep 04 '21
Sorry but this is almost definitely faked just for upvotes.
1
u/johnathanesanders Sep 04 '21
Or you could…
const isEven = (n) => { return new RegExp('\d*[02468]{1}$').test(n); }
Yeah, I think that regex works… 🤷🏼♂️
1
u/[deleted] Sep 04 '21
It's hand unrolled for maximum speed.
1
u/nickwebha Sep 04 '21
At least they used the triple equals?
1
u/drinkmoredrano Sep 04 '21
Do you even modulus, bro?
1
u/NastroAzzurro Sep 04 '21
Man could’ve just done a for loop subtracting 2 every time until they get to 1 or 2. Smh
1
u/[deleted] Sep 04 '21
At what number does it end?
1
u/[deleted] Sep 04 '21
Joder
1
u/Deadly_chef Sep 04 '21
This must be satire
1
u/wackOverflow evil Sep 04 '21
Bonus points for the jsdoc comment above
1
u/matyklug Sep 04 '21
JavaScript, would be my guess.
1
u/LucaRicardo Sep 04 '21
Why is is always the isOdd or isEven functions?
Can they just do
for an is Even function
1
u/WHSolvation Sep 04 '21
It's called dedication and sheer will.
1
u/DotNetDeveloperDude Sep 05 '21
I wonder what % does
1
u/oneday111 Sep 05 '21
What's the maximum number they enumerated?
1
u/the_stooge_nugget Sep 05 '21
Public bool isEven(int num){
return num % 2 == 0;
}
1
u/Vethya Sep 05 '21
Optimized 100
1
u/Edited_by_god Sep 05 '21
Big brain 🧠
1
u/Legendary-69420 Sep 05 '21
Here is a year long trauma:
https://github.com/samuelmarina/is-even
1
u/InsertMyIGNHere Sep 05 '21
POV: first time trying to implement fizzbuzz
1
u/[deleted] Sep 05 '21
The yanderedev experience.
1
u/theromanempire1923 Sep 05 '21
I don’t like it here
1
u/TeenFlash Sep 06 '21
the classic
1
u/LordXaner beentheredonethat Sep 06 '21
This is actually really bad code! We should create a database with tables for even and non-even numbers. With this approach we can simply tell true if the num equals an entry from table even and false if num equals an entry from table non-even!
edit:
and we don't have to change the script for new numbers!
1
u/lofigamer2 Apr 09 '22
jeeez.. how long is it?
1
u/NANOwasFound Sep 04 '21
Why @param num and @return are highlighted in comments?
1
u/Lkj509 Sep 05 '21 edited Sep 05 '21
For beginners, a better way of doing this is:
% is called the modulo operator, and x % y returns the remainder of dividing x by y.
|| is the logical or operator in c++, which means that the if argument will be true if either one of them are correct. Check this for your language, as some languages use different syntax such as ‘or’ in python.
0
u/sigmaclientwastaken evil Sep 04 '21
Well, it works... maybe...
0
u/PuzzleheadedSector2 Sep 04 '21
Do people just make terrible snippets just to post here?
0
u/i-am-a-meme69 Sep 05 '21
what about
function isEven(num) {
return n % 2 == 0;
}
0
u/[deleted] Sep 05 '21
Its called precision...You should try it sometime...
-2
u/dfmaaa1 Sep 04 '21
What is wrong with it?