AceInfinity
Emeritus, Contributor
I've been looking for programming challenges lately to exercise my learning. I ended up trying some speed and golfing challenges, and one part of a particular problem was to test for if N was prime, then do something with it. I ended up writing a minimal prime checking function which is only 49 characters.
And it works really well for a small code size. The return type was omitted to save a few more characters since most compilers will allow a return type to default to 'int'. Additionally, this is not a very efficient solution, but the idea was for code size, rather than performance. The only other way I can think of reducing this would be to have the code inline to save the characters required to type out a function, but it really depends on what the rest of your code looks like. In some cases, integrating direct code increased my overall code size, and thus I would resort to functions instead.
Just thought I'd share this. :) I don't think it can get much smaller than this without changing the approach. Who knows if it's even possible to get it smaller in size haha.
45 characters edit: https://www.sysnative.com/forums/pr...ction-code-golfing-post142073.html#post142073
Code:
[plain]p(int n){int i=1;while(++i<n&&n%i);return!(n-i);}[/plain]
And it works really well for a small code size. The return type was omitted to save a few more characters since most compilers will allow a return type to default to 'int'. Additionally, this is not a very efficient solution, but the idea was for code size, rather than performance. The only other way I can think of reducing this would be to have the code inline to save the characters required to type out a function, but it really depends on what the rest of your code looks like. In some cases, integrating direct code increased my overall code size, and thus I would resort to functions instead.
Just thought I'd share this. :) I don't think it can get much smaller than this without changing the approach. Who knows if it's even possible to get it smaller in size haha.
45 characters edit: https://www.sysnative.com/forums/pr...ction-code-golfing-post142073.html#post142073
Last edited: