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 7853937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:51:39+00:00 2026-06-02T19:51:39+00:00

As a beginning programmer, I recently bought the book ‘Algorithms – Forth Edition’ by

  • 0

As a beginning programmer, I recently bought the book ‘Algorithms – Forth Edition’ by Robert Sedgewick/Kevin Wayne and I really appreciate the exerices at the end of each chapter. However, there is one exercise (who looks quite simple) that is driving me crazy since I can’t find a solution for it.

You have to take this recursive algorithm that finds the probability of getting exactly k successes in n trials where p is the probabily of success for one event. The algorithm given is based on the recursive binomial distribution forumula.

public static double binomial(int n, int k, double p) {
    if (n == 0 && k == 0)
        return 1.0;
    else if (n < 0 || k < 0)
        return 0.0;
    return (1 - p) * binomial(n - 1, k, p) + p * binomial(n - 1, k - 1, p);
}

The goal of this exercise is to make this algorithm faster by saving computed values in an array. I already made this algorithm considerably faster by using another way of getting the binomial distribution [p(x) = nCr * p^k * (1 – p)^(n – k)] that uses an iterative method to find factorials. However, I don’t understand how an array could be used to improve execution time in this context.

Any help would be greatly appreciated!

… and before someone asks, this is not homeworks!

  • 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-02T19:51:40+00:00Added an answer on June 2, 2026 at 7:51 pm

    The book is trying to teach you a particular programming technique called memoization, a kind of broader technique known as dynamic programming. Of course in real life knowing a closed-form solution is much better, but not in the context of solving this exercise.

    Anyway, the idea is to pass a 2D array as your fourth parameter, fill it with NaNs initially, and check if there’s a solution for the given combination of n and k in the array before computing anything. If there is, return it; if there isn’t, compute it recursively, store in the array, and only then return it.

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

Sidebar

Related Questions

I am a beginning C programmer (though not a beginning programmer) looking to dive
How would you approach this exercise, as a beginning programmer? It is a classroom
So, Im a beginning C# programmer. I know basic syntax and simple things like
I am a beginning programmer and came across this in my textbook: public boolean
I'm a beginning python programmer, and I'd like someone to clarify the following behavior.
As a beginning programmer, I'm trying to settle on a standard naming convention for
As a beginning iPhone programmer, what is the best practice for writing apps to
I am beginning to believe I am a grossly incompetent programmer. After a lot
I am primarily a c# programmer. I have recently been getting into some jquery
I am a beginning programmer but I was wondering how I could keep track

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.