Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
I'm interested to know if this is actually faster than calculating 'n'. Especially if the input is limited to < 5 anyway.
-
My God man, at least use a switch if you're gonna maintain some dignity. #elegantStupidity
-
Lagostra2958y@arcade-fryer Wouldn't it in both cases be an O(1)-operation? So it wouldn't yield much anyway...
-
@Lagostra thanks. I don't really understand that kind of stuff but always interested.
-
Lagostra2958y@SithLord I am aware of that - and using iteration, you can get any integer power. The point was to show the lazy/incompetent way of doing it ;)
-
If limit of 5 is needed... then:
return n <= 5 ? n*n : -1;
The above does 6 executions/checks regardless of n... pointless if n > 5
No one's ever gonna want the square of anything higher than 5, right?
undefined
codelikeapro