C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-07-2009, 07:09 AM   #1
Registered User
 
Join Date: Nov 2009
Posts: 13
[C] How to shorten this program?

Hi!

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);
    }
}
I think that
Code:
w=w*(n++-i)/i;
might be write shorter, but i can't figure it out.


P.s. Sorry for my English...
milky is offline   Reply With Quote
Old 11-07-2009, 07:28 AM   #2
+++ OK NO CARRIER
 
quzah's Avatar
 
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   Reply With Quote
Old 11-07-2009, 07:42 AM   #3
Registered User
 
Join Date: Nov 2009
Posts: 13
Ok, thanks, is there anything else to do?
milky is offline   Reply With Quote
Old 11-07-2009, 07:54 AM   #4
Registered User
 
Join Date: Feb 2009
Posts: 35
Code:
    while (scanf("%d%d",&n,&k)==2)
    {
goes to

Code:
    while (scanf("%d%d",&n,&k)==2) {
why do you want to make it shorter?

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   Reply With Quote
Old 11-07-2009, 07:56 AM   #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   Reply With Quote
Old 11-07-2009, 07:59 AM   #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   Reply With Quote
Old 11-07-2009, 08:15 AM   #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   Reply With Quote
Old 11-07-2009, 08:19 AM   #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   Reply With Quote
Old 11-07-2009, 08:24 AM   #9
Registered User
 
Join Date: Nov 2009
Posts: 13
Thanks @Dino, but it change my program, and doesn't work properly.
milky is offline   Reply With Quote
Old 11-07-2009, 08:25 AM   #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   Reply With Quote
Old 11-07-2009, 08:27 AM   #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   Reply With Quote
Old 11-07-2009, 08:30 AM   #12
Jack of many languages
 
Join Date: Nov 2007
Location: Katy, Texas
Posts: 1,929
Quote:
Originally Posted by milky View Post
Thanks @Dino, but it change my program, and doesn't work properly.
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   Reply With Quote
Old 11-07-2009, 08:32 AM   #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   Reply With Quote
Old 11-07-2009, 10:44 AM   #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   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 09:22 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22