![]() |
| | #1 |
| Registered User Join Date: Nov 2009
Posts: 13
| [C] How to shorten this program? I'd like you to take a look at this code and tell me what should i do to make this shorter. This program count binomial coefficient. Thanks for all answers. Code: #include "stdio.h"
int main()
{
int k,n,i,w;
while (scanf("%d%d",&n,&k)==2)
{
if(k>n/2)
k=n-k;
for(w=i=1;i<=k;i++)
w=w*(n++-i)/i;
printf("%d\n",w);
}
}
Code: w=w*(n++-i)/i; P.s. Sorry for my English... |
| milky is offline | |
| | #2 |
| +++ OK NO CARRIER Join Date: Oct 2001
Posts: 10,258
| Swap your if for this: Code: k=k>n/2?n-k:k; Quzah.
__________________ Hundreds of thousands of dipshits can't be wrong. Are you up for the suck? |
| quzah is offline | |
| | #3 |
| Registered User Join Date: Nov 2009
Posts: 13
| Ok, thanks, is there anything else to do? |
| milky is offline | |
| | #4 |
| Registered User Join Date: Feb 2009
Posts: 35
| Code: while (scanf("%d%d",&n,&k)==2)
{
Code: while (scanf("%d%d",&n,&k)==2) {
it already looks cryptic with its 1 letter variable names and complete lack of spacing. I compiled it and I dont think it works 5C3 = 5!/3!2! = 5*4/2 = 10 but your program returns 8 what algorithm are you using to calculate the coefficient? |
| Brain_Child is offline | |
| | #5 |
| Jack of many languages Join Date: Nov 2007 Location: Katy, Texas
Posts: 1,929
| w=w*(n++-i)/i; can go to w*=(n++-i)/i;
__________________ Mac and Windows cross platform programmer. Ruby lover. Memorable Quotes From Recent Posts: I can't remember. |
| Dino is offline | |
| | #6 |
| Jack of many languages Join Date: Nov 2007 Location: Katy, Texas
Posts: 1,929
| Code: #include "stdio.h"
int main()
{
int k,n,i,w;
while (scanf("%d%d",&n,&k)==2) for(k=k>n/2?n-k:k,w=i=1;i<=k;w*=(n++-i)/i,printf("%d\n",w),i++) ;
}
__________________ Mac and Windows cross platform programmer. Ruby lover. Memorable Quotes From Recent Posts: I can't remember. Last edited by Dino; 11-07-2009 at 08:12 AM. Reason: moved i++ to end |
| Dino is offline | |
| | #7 |
| Jack of many languages Join Date: Nov 2007 Location: Katy, Texas
Posts: 1,929
| Here's my final version. Code: #include <stdio.h>
int main()
{
int k,n,i,w;
while (scanf("%d%d",&n,&k)==2) for(k=k>n/2?n-k:k,w=i=1;i<=k;w*=(n++-i)/i++,printf("%d\n",w)) ;
return 0;
}
__________________ Mac and Windows cross platform programmer. Ruby lover. Memorable Quotes From Recent Posts: I can't remember. |
| Dino is offline | |
| | #8 |
| Registered User Join Date: Jan 2009
Posts: 151
| Code: #include <stdio.h>
int main(){int k,n,i,w;while(scanf("%d%d",&n,&k)==2)for(k=k>n/2?n-k:k,w=i=1;i<=k;i++,w*=(n++-i)/i,printf("%d\n",w));return 0;}
|
| Subsonics is offline | |
| | #9 |
| Registered User Join Date: Nov 2009
Posts: 13
| Thanks @Dino, but it change my program, and doesn't work properly. |
| milky is offline | |
| | #10 |
| Registered User Join Date: Jan 2009
Posts: 151
| But what is the purpose of making the program cryptic and hard to read? |
| Subsonics is offline | |
| | #11 |
| Registered User Join Date: Nov 2009
Posts: 13
| I have to do this program using as less as i can chars. |
| milky is offline | |
| | #12 |
| Jack of many languages Join Date: Nov 2007 Location: Katy, Texas
Posts: 1,929
| Then it's worthless. (Did you run the version before or after my edit? Before the edit, I was incrementing i in the wrong spot.)
__________________ Mac and Windows cross platform programmer. Ruby lover. Memorable Quotes From Recent Posts: I can't remember. |
| Dino is offline | |
| | #13 |
| Registered User Join Date: Nov 2009
Posts: 13
| I run your latest version and it also change my program:/ Maybe there is another, shorter algorithm to count binomial coefficient? |
| milky is offline | |
| | #14 |
| Jack of many languages Join Date: Nov 2007 Location: Katy, Texas
Posts: 1,929
| It might be good to start with the proper formula in the first place: Binomial coefficient - Wikipedia, the free encyclopedia
__________________ Mac and Windows cross platform programmer. Ruby lover. Memorable Quotes From Recent Posts: I can't remember. |
| Dino is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Issue with program that's calling a function and has a loop | tigerfansince84 | C++ Programming | 9 | 11-12-2008 01:38 PM |
| Need help with a program, theres something in it for you | engstudent363 | C Programming | 1 | 02-29-2008 01:41 PM |
| This is a simple program.. Help me please I think it has an error.. | lesrhac03 | C Programming | 4 | 02-21-2008 10:39 AM |
| My program, anyhelp | @licomb | C Programming | 14 | 08-14-2001 10:04 PM |