Guest User

Raccoons!

a guest
Feb 11th, 2015
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.46 KB | None | 0 0
  1. namespace ManagedWorkspace
  2. {
  3.     using System;
  4.     using System.Diagnostics;
  5.  
  6.     internal static class Program
  7.     {
  8.         private static void Main(string[] args)
  9.         {
  10.             int total_number_of_raccoons = 61;
  11.             int total_number_of_tagged_raccoons = 8;
  12.             Debug.Assert(total_number_of_raccoons >= total_number_of_tagged_raccoons);
  13.             int total_number_of_untagged_raccoons = total_number_of_raccoons - total_number_of_tagged_raccoons;
  14.  
  15.             // p[n, k] = probability that k raccoons are tagged when chosen n raccoons.
  16.             double[,] p = new double[total_number_of_raccoons + 1, total_number_of_tagged_raccoons + 1];
  17.  
  18.             // Simple base cases.
  19.             p[1, 0] = ((double)total_number_of_untagged_raccoons) / total_number_of_raccoons;
  20.             p[1, 1] = ((double)total_number_of_tagged_raccoons) / total_number_of_raccoons;
  21.  
  22.             for (int n = 2; n <= total_number_of_raccoons; n++)
  23.             {
  24.                 for (int k = 0; k <= Math.Min(n, total_number_of_tagged_raccoons); k++)
  25.                 {
  26.                     if (k == 0)
  27.                     {
  28.                         if (p[n - 1, k] > 0)
  29.                         {
  30.                             int number_of_raccoons_chosen = n - 1;
  31.                             int number_of_tagged_raccoons_chosen = k;
  32.  
  33.                             int number_of_raccoons_remaining = total_number_of_raccoons - number_of_raccoons_chosen;
  34.                             int number_of_tagged_raccoons_remaining = total_number_of_tagged_raccoons - number_of_tagged_raccoons_chosen;
  35.                             int number_of_untagged_raccoons_chosen = number_of_raccoons_chosen - number_of_tagged_raccoons_chosen;
  36.                             int number_of_untagged_raccoons_remaining = total_number_of_untagged_raccoons - number_of_untagged_raccoons_chosen;
  37.  
  38.                             Debug.Assert(number_of_raccoons_remaining >= 0);
  39.                             Debug.Assert(number_of_tagged_raccoons_remaining >= 0);
  40.                             Debug.Assert(number_of_untagged_raccoons_chosen >= 0);
  41.                             Debug.Assert(number_of_untagged_raccoons_remaining >= 0);
  42.  
  43.                             p[n, k] = p[n - 1, k] * ((double)number_of_untagged_raccoons_remaining) / number_of_raccoons_remaining;
  44.                         }
  45.                     }
  46.                     else if (k < n)
  47.                     {
  48.                         double A = 0;
  49.                         double B = 0;
  50.                         if (p[n - 1, k - 1]> 0)
  51.                         {
  52.                             int number_of_raccoons_chosen = n - 1;
  53.                             int number_of_tagged_raccoons_chosen = k - 1;
  54.  
  55.                             int number_of_raccoons_remaining = total_number_of_raccoons - number_of_raccoons_chosen;
  56.                             int number_of_tagged_raccoons_remaining = total_number_of_tagged_raccoons - number_of_tagged_raccoons_chosen;
  57.                             int number_of_untagged_raccoons_chosen = number_of_raccoons_chosen - number_of_tagged_raccoons_chosen;
  58.                             int number_of_untagged_raccoons_remaining = total_number_of_untagged_raccoons - number_of_untagged_raccoons_chosen;
  59.  
  60.                             Debug.Assert(number_of_raccoons_remaining >= 0);
  61.                             Debug.Assert(number_of_tagged_raccoons_remaining >= 0);
  62.                             Debug.Assert(number_of_untagged_raccoons_chosen >= 0);
  63.                             Debug.Assert(number_of_untagged_raccoons_remaining >= 0);
  64.  
  65.                             A = ((double)number_of_tagged_raccoons_remaining) / number_of_raccoons_remaining;
  66.                         }
  67.                         if (p[n - 1, k] > 0)
  68.                         {
  69.                             int number_of_raccoons_chosen = n - 1;
  70.                             int number_of_tagged_raccoons_chosen = k;
  71.  
  72.                             int number_of_raccoons_remaining = total_number_of_raccoons - number_of_raccoons_chosen;
  73.                             int number_of_tagged_raccoons_remaining = total_number_of_tagged_raccoons - number_of_tagged_raccoons_chosen;
  74.                             int number_of_untagged_raccoons_chosen = number_of_raccoons_chosen - number_of_tagged_raccoons_chosen;
  75.                             int number_of_untagged_raccoons_remaining = total_number_of_untagged_raccoons - number_of_untagged_raccoons_chosen;
  76.  
  77.                             Debug.Assert(number_of_raccoons_remaining >= 0);
  78.                             Debug.Assert(number_of_tagged_raccoons_remaining >= 0);
  79.                             Debug.Assert(number_of_untagged_raccoons_chosen >= 0);
  80.                             Debug.Assert(number_of_untagged_raccoons_remaining >= 0);
  81.  
  82.                             B = ((double)number_of_untagged_raccoons_remaining) / number_of_raccoons_remaining;
  83.                         }
  84.                         p[n, k] = p[n - 1, k - 1] * A + p[n - 1, k] * B;
  85.                     }
  86.                     else
  87.                     {
  88.                         double A = 0;
  89.                         if (p[n - 1, k - 1] > 0)
  90.                         {
  91.                             int number_of_raccoons_chosen = n - 1;
  92.                             int number_of_tagged_raccoons_chosen = k - 1;
  93.  
  94.                             int number_of_raccoons_remaining = total_number_of_raccoons - number_of_raccoons_chosen;
  95.                             int number_of_tagged_raccoons_remaining = total_number_of_tagged_raccoons - number_of_tagged_raccoons_chosen;
  96.                             int number_of_untagged_raccoons_chosen = number_of_raccoons_chosen - number_of_tagged_raccoons_chosen;
  97.                             int number_of_untagged_raccoons_remaining = total_number_of_untagged_raccoons - number_of_untagged_raccoons_chosen;
  98.  
  99.                             Debug.Assert(number_of_raccoons_remaining >= 0);
  100.                             Debug.Assert(number_of_tagged_raccoons_remaining >= 0);
  101.                             Debug.Assert(number_of_untagged_raccoons_chosen >= 0);
  102.                             Debug.Assert(number_of_untagged_raccoons_remaining >= 0);
  103.  
  104.                             A = ((double)number_of_tagged_raccoons_remaining) / number_of_raccoons_remaining;
  105.                         }
  106.                         p[n, k] = p[n - 1, k - 1] * A;
  107.                     }
  108.                 }
  109.             }
  110.             Console.WriteLine(p[20, 4]);
  111.         }
  112.     }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment