Hello There, Guest!
View New Posts  |  View Today's Posts
[C] IsPrime Function (Code Golfing Style)

  • 0 Vote(s) - 0 Average


12-29-2015, 11:41 PM #1
AceInfinity
Developer
*******
Administrators
Posts: 9,733 Threads:1,026 Joined: Jun 2011 Reputation: 76

IsPrime Function (Code Golfing Style)
After some time on a few challenge sites, I decided to gather some very minimized functions. Here is a prime checking function I devised and shrunk down to 49 characters for size:
Code:
p(int n){int i=1;while(++i<n&&n%i);return!(n-i);}

Not efficient as it's basically just a loop that checks for divisibility, but that's not the point. :) I was also able to omit the return type since the majority of the compilers used default to int. This is all bad practice in the real world, but it helps when code golfing. C is not a great language for it, but this was fairly decent for a function.

cheers
This post was last modified: 12-29-2015, 11:42 PM by AceInfinity.


Microsoft MVP .NET Programming - (2012 - Present)
®Crestron DMC-T Certified Automation Programmer

Development Site: aceinfinity.net

 ▲
 ▲ ▲

01-01-2016, 06:52 PM #2
AceInfinity
Developer
*******
Administrators
Posts: 9,733 Threads:1,026 Joined: Jun 2011 Reputation: 76

RE: IsPrime Function (Code Golfing Style)
46 characters:
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;}
This post was last modified: 01-01-2016, 07:03 PM by AceInfinity.


Microsoft MVP .NET Programming - (2012 - Present)
®Crestron DMC-T Certified Automation Programmer

Development Site: aceinfinity.net

 ▲
 ▲ ▲




Forum Jump:


Possibly Related Threads...
Thread Author Replies Views Last Post
  Code Challenge [Help Needed] Zultralord 3 1,479 01-09-2016, 03:20 AM
Last Post: AceInfinity
   Calling A Private Member Function (For Fun) AceInfinity 2 1,165 07-03-2014, 07:38 PM
Last Post: AceInfinity
   Fun With Function Pointers AceInfinity 0 687 03-04-2014, 09:29 PM
Last Post: AceInfinity
   C Style String Encoding & Decoding AceInfinity 0 2,009 05-07-2013, 12:10 AM
Last Post: AceInfinity
   Calculate Distance via Zip Code project KoBE 16 8,108 04-06-2013, 07:21 PM
Last Post: AceInfinity


Users browsing this thread: 1 Guest(s)