Skip to content

Instantly share code, notes, and snippets.

@ddjerqq
Created August 4, 2024 21:38
Show Gist options
  • Save ddjerqq/eb487d3f8157c4362d9f106de622cb65 to your computer and use it in GitHub Desktop.
Save ddjerqq/eb487d3f8157c4362d9f106de622cb65 to your computer and use it in GitHub Desktop.
var size = Random.Shared.Next(0, 1_000_000);
var numbers = Enumerable.Range(0, size).ToArray();
Random.Shared.Shuffle(numbers);
// we are allowed a total of 5 observations, we must use
// the average gap method to approximate the number of elements
var observationCount = 100;
var observations = numbers.Take(observationCount).ToArray();
var maxObserved = observations.Max();
// k is sample count
// x is the max number observed
// x + (x - k) / k
var total = maxObserved + (maxObserved - observationCount) / observationCount;
Console.WriteLine($"Sample count: {observationCount}");
Console.WriteLine($"Observations: {string.Join(", ", observations)}");
Console.WriteLine($"Estimated total: {total}");
Console.WriteLine($"Actual total: {size}");
Console.WriteLine($"Error: {Math.Abs(size - total)}");
Console.WriteLine($"Error %: {MathF.Abs(size - total) / size:P2}");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment