2
Sep 02 '19
square(int n) { return n << 1; }
Bitshift x one to the left equals x to the power of two, if you think about how binary works... And ignore overflow.
1
70
u/PyroBurnem greek comma Aug 10 '19
when you get paid per line of code
59
u/shizzy0 Aug 10 '19
When you get paid in time complexity.
PROGRAMMER 0: How much did you get?
PROGRAMMER 1: Just O(1). How much did you get, Fred?
PROGRAMMER 10X: O(n2), baby! Woohoo!
11
25
u/calio Aug 10 '19
I want that comment as a license I can license my stuff under when I release them.
44
u/protosquirrel Aug 10 '19
Image Transcription: Code
// I don't know what I did but it works
// Please don't modify it
private int square(int n)
{
int k = 0;
while (true)
{
if (k == n*n)
{
return k;
}
k++;
}
}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!
7
7
u/tcpukl Aug 10 '19
It's only a line change from a square root!
1
u/fb39ca4 depraved Oct 29 '19
And it will go into an infinite loop if the square root is irrational!
5
5
4
3
2
4
u/DavidWigh Aug 10 '19
You could use a for-loop instead of a while-loop. Then you could define k, have your condition and k++ in one line
7
1
1
0
Aug 10 '19
[deleted]
7
5
Aug 10 '19
why tf are the methods private though, are we not even going to invoke this shit?
12
u/c3534l Aug 10 '19 edited Aug 10 '19
Of course not, the performance is terrible.
7
70
u/pierovera Aug 10 '19
I get this is shitty code, but this wouldn't even compile.
19
u/fond42518 Aug 10 '19
Can't even try compiling bc I'm on mobile rn. Is it because the redeclaration of
kin the second method?14
u/Dr-RobertFord Aug 10 '19
No that wouldn't matter. In fact I'm pretty sure this would compile... It would run forever, however, unless n = 0
7
u/Dmium Aug 10 '19 edited Aug 10 '19
Edit: missread but I'm pretty sure it runs fine as long as you call square(n)
The code may have been edited since this discussion
17
u/BigMintyMitch Aug 10 '19 edited Aug 10 '19
It does matter in C#. You can’t redeclare a parameter in a C# function like the example above. If that line was removed, yes, it would run forever unless n = 0.
Edit: The comment above originally said
int k = 0notk = kwhen I commented this. I’m not an idiot, my comment was true at the time.7
u/tcpukl Aug 10 '19
It's not redecorated though. It's just assigned to itself
7
u/BigMintyMitch Aug 10 '19
Ok, they edited the post since I left this comment. Now it’s just assigned to itself, before it was not.
1
1
Aug 10 '19 edited Aug 10 '19
I’m not sure but I thought in most languages you can only overload the constructor like that?
Edit: Sounds like I was right not to be sure
3
u/fond42518 Aug 10 '19
I'm assuming this code is Java, in which a class's methods can be overloaded as long as their input parameters are different.
Edit: nvm, just saw the C# flair
2
u/dbgprint Aug 10 '19
What languages can’t do this? C++ can do it too, as long as the function signature is different
8
u/Doriphor Aug 10 '19
int k = 0;
This line needs to be deleted in private int square(int n, int k)
6
Aug 10 '19
Shit, I missed that. Fixed it so that k is now set to k.
22
u/Doriphor Aug 10 '19
I like your solution, but you forgot about cosmic rays. Here I fixed it :)
int square(int n, int k) { k = k; while (true) { if (k = n*n) { return k; } else { return square(n, k+1); } } } int square(int n) { int k = 0; while (true) { if (k = n*n) { return k; } else { return square(n, k+1); } } } int cosmicrayinterferenceprevention(int z) { const int RUN_NUM = 100; int answers_arr[RUN_NUM]; for (int i = 0; i < RUN_NUM; i++) { answers_arr[i] = square(z); for (int j = 0; j <= i-1; j++) { while(answers_arr[i] != answers_arr[i+1]) { answers_arr[i] = square(z); answers_arr[i+1] = square(z); } } } return answers_arr[0]; }7
3
5
3
Aug 10 '19
k will always be 0 in the recursive case when it's compared to n2 so you have an infinite loop unless n = 0.
2
2
1
0
0
Aug 10 '19
[deleted]
10
u/wind-raven Aug 10 '19
Nope, after seeing some of the code in the legacy app I'm working on I would love to come across code like this. It may make you go wtf but at least you can follow it.
18
Aug 10 '19
no this is fake. This guy multiplied a number by itself so I'm sure that he knows how to return a square (return x**2)
0
0
4
u/[deleted] Aug 10 '19
I can't even