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

  • Home
  • SEARCH
  • 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 4594430
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T22:49:37+00:00 2026-05-21T22:49:37+00:00

Basically, the problem simulates the following: There is an urn with 50 green balls

  • 0

Basically, the problem simulates the following:

There is an urn with 50 green balls and 50 red balls.

I am allowed to pick balls from the urn, without replacement, with the following rules: For every red ball picked, I lose a dollar, for every green ball picked, I gain a dollar.

I can stop picking whenever I want. Worst case scenario is I pick all 100, and net 0.

The question is to come up with an optimal stopping strategy, and create a program to compute the expected value of the strategy.

My strategy is to continue picking balls, while the expected value of picking another ball is positive.

That is, the stopping rule is DYNAMIC.

In Latex, here’s the recursive formula in an image:

https://i.stack.imgur.com/fnzYk.jpg

#include <stdio.h>
#include <math.h>
#include <stdlib.h>



double ExpectedValue(double, double);
double max(double, double);

main() {

double g = 50;
double r = 50;


double EV = ExpectedValue(g, r);

printf ("%f\n\n", EV);

system("PAUSE");

}


double ExpectedValue(double g, double r){

double p =  (g / (g + r));

double q = 1 - p;

if (g == 0)

return r;

if (r == 0)

return 0;

double E_gr = max ((p * ExpectedValue (g - 1, r)) + (q * ExpectedValue (g, r - 1)), (r - g));

return E_gr; 

}

double max(double a, double b){

if (a > b)
return a;

else return b;
}

I let it run for 30 minutes, and it was still working.
For small values of g and r, a solution is computed very quickly. What am I doing wrong?

Any help is much appreciated!

  • 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-05-21T22:49:37+00:00Added an answer on May 21, 2026 at 10:49 pm

    Your algorithm is fine, but you are wasting information. For a certain pair (g, r) you calculate it’s ExpectedValue and then you throw that information away. Often with recursion algorithms remembering previously calculated values can speed it up a LOT.

    The following code runs in the blink of an eye. For example for g = r = 5000 it calculates 36.900218 in 1 sec. It remembers previous calculations of ExpectedValue(g, r) to prevent unnecessary recursion and recalculation.

    #include <stdio.h>
    #include <stdlib.h>
    
    double ExpectedValue(int g, int r, double ***expectedvalues);
    inline double max(double, double);
    
    int main(int argc, char *argv[]) {
        int g = 50;
        int r = 50;
        int i, j;
    
        double **expectedvalues = malloc(sizeof(double*) * (g+1));
    
        // initialise
        for (i = 0; i < (g+1); i++) {
            expectedvalues[i] = malloc(sizeof(double) * (r+1));
            for (j = 0; j < (r+1); j++) {
                expectedvalues[i][j] = -1.0;
            }
        }
    
        double EV = ExpectedValue(g, r, &expectedvalues);
        printf("%f\n\n", EV);
    
        // free memory
        for (i = 0; i < (g+1); i++) free(expectedvalues[i]);
        free(expectedvalues);
    
        return 0;
    }
    
    double ExpectedValue(int g, int r, double ***expectedvalues) {
        if (g == 0) return r;
        if (r == 0) return 0;
    
        // did we calculate this before? If yes, then return that value
        if ((*expectedvalues)[g][r] != -1.0) return (*expectedvalues)[g][r];
    
        double p = (double) g / (g + r);
        double E_gr = max(p * ExpectedValue(g-1, r, expectedvalues) + (1.0-p) * ExpectedValue(g, r-1, expectedvalues), (double) (r-g));
    
        // store value for later lookup
        (*expectedvalues)[g][r] = E_gr;
    
        return E_gr;
    }
    
    double max(double a, double b) {
        if (a > b) return a;
        else return b;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically my problem stems from a desire to have the textbox portion be white,
Basically, here's my problem. I'm calling someone else's FORTRAN functions from my C++ code,
Basically my problem is splited into 2 parts. There are examples in the ExtJS
Basically my problem is with the page at http://wiki.diablocommunity.com/index.php?title=Tristram_Cathedral Please see the screen shots
Basically my problem is that i've adapted a piece of code found here http://social.msdn.microsoft.com/Forums/en-US/vemapcontroldev/thread/62e70670-f306-4bb7-8684-549979af91c1
I am creating a little testing component and am running into a problem Basically
The problem is basically this, in python's gobject and gtk bindings. Assume we have
This is basically a math problem, but very programing related: if I have 1
I have basically the same problem outlined in this question, however I am using
So basically here's my problem. I'm looking for a solution to allow us to

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.