Thread: [C] How to shorten this program?

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    40

    [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...

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Swap your if for this:
    Code:
    k=k>n/2?n-k:k;

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    40
    Ok, thanks, is there anything else to do?

  4. #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?

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,325
    w=w*(n++-i)/i;

    can go to

    w*=(n++-i)/i;
    Mainframe assembler programmer by trade. C coder when I can.

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,325
    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++) ;
    }
    Last edited by Dino; 11-07-2009 at 08:12 AM. Reason: moved i++ to end
    Mainframe assembler programmer by trade. C coder when I can.

  7. #7
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,325
    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;
    }
    Mainframe assembler programmer by trade. C coder when I can.

  8. #8
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    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;}

  9. #9
    Registered User
    Join Date
    Nov 2009
    Posts
    40
    Thanks @Dino, but it change my program, and doesn't work properly.

  10. #10
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    But what is the purpose of making the program cryptic and hard to read?

  11. #11
    Registered User
    Join Date
    Nov 2009
    Posts
    40
    I have to do this program using as less as i can chars.

  12. #12
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,325
    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.)
    Mainframe assembler programmer by trade. C coder when I can.

  13. #13
    Registered User
    Join Date
    Nov 2009
    Posts
    40
    I run your latest version and it also change my program:/ Maybe there is another, shorter algorithm to count binomial coefficient?

  14. #14
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,325
    It might be good to start with the proper formula in the first place: Binomial coefficient - Wikipedia, the free encyclopedia
    Mainframe assembler programmer by trade. C coder when I can.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM