Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8083761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T17:34:40+00:00 2026-06-05T17:34:40+00:00

I am attempting to do some statistics-related functions so I can carry out a

  • 0

I am attempting to do some statistics-related functions so I can carry out a few related procedures (ie: statistics calculations for probabilities, generate Pascal’s triangle for an arbitrary depth, etc).

I have encountered an issue where I am likely dealing with overflow. For example, if I want to calculate nPr for (n=30,p=1), I know that I can reduce it to:

30P1 = 30! / (30 - 1)!
     = 30! / (29)!
     = 30! / 29!
     = 30

However, when calculating using the functions below, it looks like I will always get invalid values due to integer overflow. Are there any workarounds that don’t require the use of a library to support arbitrarily large numbers? I’ve read up a bit in other posts on the gamma functions, but couldn’t find concrete examples.

int factorial(int n) {
   return (n == 1 || n == 0) ? 1 : factorial(n - 1) * n;
}


int nCr(int n, int r) {
   return (nPr(n,r) / factorial(r));
   //return factorial(n) / factorial(r) / factorial(n-r));
}


int nPr(int n, int r) {
   return (factorial(n) / factorial(n-r));
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T17:34:43+00:00Added an answer on June 5, 2026 at 5:34 pm

    You look like you are on the right track, so here you go:

    #include <math.h>
    #include <stdio.h>
    
    int nCr(int n, int r) {
       if(r>n) {
          printf("FATAL ERROR"); return 0;
         }       
       if(n==0 || r==0 || n==r) {
          return 1;
       } else {
          return (int)lround( ((double)n/(double)(n-r)/(double)r) * exp(lgamma(n) - lgamma(n-r) - lgamma(r)));
       }
    }
    
    
    int nPr(int n, int r) {
       if(r>n) {printf("FATAL ERROR"; return 0;}
       if(n==0 || r==0) {
          return 1;
       } else {
          if (n==r) {
             r = n - 1;
          }
          return (int)lround( ((double)n/(double)(n-r)) * exp(lgamma(n) - lgamma(n-r)));
       }
    }
    

    To compile, do: gcc -lm myFile.c && ./a.out

    Note that the accuracy of your results is limited by the bit-depth of the double data type. You should be able to get good results with this, but be warned: replacing all the ints above with long long unsigned may not necessarily guarantee accurate results for larger values of n,r. At some point, you will still need some math library to handle arbitrarily large values, but this should help you avoid that for smaller input values.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am attempting to add some functions to backbone so that I can communicate
I am attempting to pivot some data out of the Vendor column in this
I'm attempting to pull some data from a SQLite database so that I can
I am attempting to do some calculations on a simple ruby screen. Lets say
I am attempting to write some tests using webtest to test out my python
I am attempting to implement some additional statistics gathering in a C# server application
I've been reading through a few asp.net articles, and attempting some code, but I
I am attempting to create some custom map routes but can't get it working
I am attempting add some tests to an existing QT GUI application using QTest.
I am attempting to modify some existing C++ code to work with my needs,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.