SHARE
TWEET

Untitled

a guest Sep 22nd, 2018 67 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. #define NUM_N 960
  5.  
  6. double num_rolls_needed[NUM_N + 1];
  7.  
  8. int main(void)
  9. {
  10.         num_rolls_needed[0] = 0.0;
  11.         num_rolls_needed[1] = 0.0;
  12.         for (int i = 2; i <= 6; ++i) {
  13.                 double p = (6 - i) / 6.0;
  14.                 num_rolls_needed[i] = 1 + p / (1.0 - p);
  15.                 printf("%3d %5.3f\n", i, num_rolls_needed[i]);
  16.         }
  17.  
  18.         for (int i = 7; i <= NUM_N; ++i) {
  19.                 double best_needed = HUGE_VAL;
  20.                 int best_d = -1;
  21.  
  22.                 for (int d = 2; d <= 6; ++d) {  // Try throwing d2..d6.
  23.                         // Sum over all possible outcomes for that die.
  24.                         double sum_needed = 0.0;
  25.                         for (int outcome = 1; outcome <= d; ++outcome) {
  26.                                 int range_start = lrint(i * (outcome - 1) / (double)(d));
  27.                                 int range_end_exclusive = lrint(i * outcome / (double)(d));
  28.                                 sum_needed += num_rolls_needed[range_end_exclusive - range_start];
  29.                         }
  30.  
  31.                         double needed = num_rolls_needed[d] + sum_needed / d;
  32.                         if (needed <= best_needed) {
  33.                                 best_needed = needed;
  34.                                 best_d = d;
  35.                         }
  36.                 }
  37.  
  38.                 num_rolls_needed[i] = best_needed;
  39.  
  40.                 printf("%3d %5.3f  [best = %d]\n", i, num_rolls_needed[i], best_d);
  41.         }
  42. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top