42
u/Kaasplankie Oct 27 '17
How long does it take it to check is_even(2)? I know about 231 operations but is it like unbearably slow or more “that’s a difficult computation” slow?
32
6
Oct 28 '17
I'm getting ~3.97 seconds at best to ~3.94 at worst in C#.
better than I expected.
I wanted to do it in C as well, but I have no idea how to accurately measure time, and my compiler is on a virtual desktop I'm too lazy to log into, though knowing how crazy amazing C compilers are, it would probably get optimized into something usable.
3
20
u/_youlikeicecream_ Oct 27 '17 edited Nov 07 '17
This is nothing, I sat a programming exam back in 2000 for object orientated pascal, the question was something like "using the mod function determine whether a given value is odd or even". I had forgotten how to use mod (because it was rare for me to use it back then) ....
So, my novel method was to take the given integer, divide it by two then typecast it to a string, strip off everything before and including the decimal point, then compared the stripped value to 0. Anything greater than 0 was an odd integer.
Edit: the full horror of what I actually did has come back to me: Instead of comparing the stripped value to 0 I actually compared the length in chars of the starting number (typecasted to a string) to the length in chars of the result of the div by 2 (also typecasted to a string) if the result was longer than the starting number then the number was odd.
16
u/EarthC-137 Oct 27 '17
Just read this over here... https://www.reddit.com/r/ProgrammerHumor/comments/791y38/what_is_modulo/?st=J9A8ORE3&sh=332a112c
Was that you? :-D
1
2
Oct 28 '17
I procrastinated on a C project in university where I had to read pseudo random characters from a text file and then do stuff with them.
An hour before the due time, I completely forgot the fscanf() syntax and usage, so I bullshat and made a function that generates a (pre-populated, constant) array of the entire alphabet and returns the value at a random index.
I got a 93% for it, too...
1
15
u/lurking_bishop Oct 27 '17
I just googled it, apparently the overflow of an unsigned int is defined behaviour. So it's not an architecture quirk that this code works.
8
u/Lightfire228 Oct 27 '17
Well, technically it is an architecture quirk, but it would require rewriting the laws of the universe to fix, so now it's 'defined behavior'
9
u/lurking_bishop Oct 27 '17
Well, yes and no. For example, the overflow of a signed int is not defined -> you cannot rely on the result of the computation but on some machines it will still work. The reason for that is that there's a couple of common possibilities to represent signed integers although the 2's complement is the most common. So the C design committee left that implementation detail open which requires making the overflow undefined.
While it's possible to represent unsigned integers in a couple of different ways*, there isn't really a reason to do it differently than the naive way. So the C committee forced the language to do exactly that.
* for example, you could gray-encode the binary value, an overflow would't produce a zero in that case I believe
1
u/Tweenk Oct 27 '17
It would only require making a non-binary machine.
1
u/Lightfire228 Oct 28 '17
Even still, the machine would have a finite storage capacity, so overflow would still occur
1
u/Vitztlampaehecatl Oct 28 '17
<insert trans joke>
1
Oct 28 '17 edited Oct 28 '17
class boolPC { private byte value; public void Set (bool bin) { if (bin) value = 1; else value = 0; } public void Set (byte nonBin) { value = nonBin; } public bool ToBinary (void) { if (value == 1) return true; else if (value == 0) return false; else return null; } }
3
u/poolp34 Oct 27 '17
Needs more jpeg
12
3
Oct 27 '17
I was reading as signed Int, and was wondering how that ever worked. Then I went, but if it were unsigned... looks at post oh.
2
2
u/msg45f Oct 28 '17
public static boolean isEven(int i) {
i = Math.abs(i);
boolean even = false;
while(--i > 0) {
even = !even;
}
return even;
}1
-14
u/digizen Oct 27 '17
Amusing, yet hideously slow method of finding odd/even.
"The x+= 2" is a bug, it results in the function hanging if anything over 1 is passed in. Don't know if that's part of the joke.
28
u/Benjamin-FL Oct 27 '17
Try it: it wraps back aroung to zero at 232.
2
u/KVYNgaming Oct 27 '17
In the real world yes this may happen but just looking at it from an algorithm standpoint the function makes no sense
6
u/Benjamin-FL Oct 27 '17
I've been trying to find a way to abuse stack overflows to determine if something is even or odd, so if you think this is bad...
10
4
1
0
Oct 27 '17
[deleted]
4
u/Benjamin-FL Oct 27 '17
It's this font. It's reasonably readable, while still taking up almost no space, allowing me to do things like fit up to 3 panes of code side by side (breaking at 80 characters). My screens are 1280x1024, which makes it about the right size for me. I've tried it on higher density displays though, and it's definitely way to small to read comfortably.
1
0
Oct 28 '17
[deleted]
4
u/Benjamin-FL Oct 28 '17
It all depends on your input. If you are inputting numbers smaller than 2147483648, using
x -= 2is faster, while numbers larger than 2147483648 are faster using my algorithm. The worst case runtime of either algorithm is almost a second on my machine, so there are literal seconds at stake per call. That doesn't sound like much, but keep in mind that you might want to know whether 100,000 different numbers were even or odd. In that case, you could literally waste days of runtime by picking the wrong algorithm. I recommend conducting extensive testing and analysis beforehand to determine which algorithm is best for your unique situation.
0
Oct 27 '17
[deleted]
16
u/Benjamin-FL Oct 27 '17
Integer overflow means that once it reaches 232 it will wrap back around to zero. It takes a second or two to execute on my machine though.
1
Oct 27 '17
[removed] — view removed comment
3
u/Doophie Oct 27 '17
Eventually the number gets so big it overflows and becomes small, since he's adding by two it'll be 0 or 1 eventually
57
u/[deleted] Oct 27 '17 edited Oct 27 '17
For those of you who have a hard time reading it: