AceInfinity Emeritus, Contributor Joined Feb 21, 2012 Posts 1,728 Location Canada Dec 30, 2015 #1 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. Code: p(int n){int i=1;while(++i<n&&n%i);return!(n-i);} 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: Jan 1, 2016
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. Code: p(int n){int i=1;while(++i<n&&n%i);return!(n-i);} 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
niemiro Senior Administrator, Windows Update Expert Staff member Joined Mar 2, 2012 Posts 8,772 Location District 12 Jan 1, 2016 #2 Interesting code golf. You've done well. I think I've managed to shave one character off it though? In the return statement? Code: q(int n){int i=1;while(++i<n&&n%i);return n==i;}
Interesting code golf. You've done well. I think I've managed to shave one character off it though? In the return statement? Code: q(int n){int i=1;while(++i<n&&n%i);return n==i;}
AceInfinity Emeritus, Contributor Joined Feb 21, 2012 Posts 1,728 Location Canada Jan 1, 2016 #3 Nice, I worked on the while portion for the most part but I didn't even bother to check the return statement. assuming it was smaller because I could remove the space. :)
Nice, I worked on the while portion for the most part but I didn't even bother to check the return statement. assuming it was smaller because I could remove the space. :)
AceInfinity Emeritus, Contributor Joined Feb 21, 2012 Posts 1,728 Location Canada Jan 1, 2016 #4 I got it down even further: 46 chars Code: p(int n){int i=n;while(--i&&n%i);return i==1;} edit: 45 characters: Code: p(int n){int i=n;while(--i&&n%i);return!--i;} :grin1: Last edited: Jan 1, 2016
I got it down even further: 46 chars Code: p(int n){int i=n;while(--i&&n%i);return i==1;} edit: 45 characters: Code: p(int n){int i=n;while(--i&&n%i);return!--i;} :grin1:
niemiro Senior Administrator, Windows Update Expert Staff member Joined Mar 2, 2012 Posts 8,772 Location District 12 Jan 1, 2016 #5 Haha, damn, you beat me to it! I was just doing some maths revision for the last couple of hours and golfing it further in my head; I eventually got your 45 character version. Nicely done
Haha, damn, you beat me to it! I was just doing some maths revision for the last couple of hours and golfing it further in my head; I eventually got your 45 character version. Nicely done
AceInfinity Emeritus, Contributor Joined Feb 21, 2012 Posts 1,728 Location Canada Jan 2, 2016 #6 I was trying to think of some other smart ways to shrink '!--i' and '--i&&n%i' with better bitwise alternatives too, and ones that didn't invalidate any sequence point guidelines either. The problem is that if you get rid of the short-circuiting operation and do anything else with assigning to and checking i, the operation is probably undefined behavior.
I was trying to think of some other smart ways to shrink '!--i' and '--i&&n%i' with better bitwise alternatives too, and ones that didn't invalidate any sequence point guidelines either. The problem is that if you get rid of the short-circuiting operation and do anything else with assigning to and checking i, the operation is probably undefined behavior.
AceInfinity Emeritus, Contributor Joined Feb 21, 2012 Posts 1,728 Location Canada Jan 3, 2016 #7 39 characters: Code: [NO-PARSE]p(n,i){i=n;while(--i&&n%i);return!--i;}[/NO-PARSE] You don't need to actually pass the second parameter, and this saves you from declaring it in outer scope too saving the additional ';' required. Demo: (not "golf" optimized) Code: [NO-PARSE]int i = 0; for (i = 0; i < 10; ++i) { p(i)?printf("%d\n",i):0; }[/NO-PARSE] Last edited: Jan 3, 2016
39 characters: Code: [NO-PARSE]p(n,i){i=n;while(--i&&n%i);return!--i;}[/NO-PARSE] You don't need to actually pass the second parameter, and this saves you from declaring it in outer scope too saving the additional ';' required. Demo: (not "golf" optimized) Code: [NO-PARSE]int i = 0; for (i = 0; i < 10; ++i) { p(i)?printf("%d\n",i):0; }[/NO-PARSE]