C# dictionary size is a prime number [duplicate] - c#

This question already has answers here:
Why .Net dictionaries resize to prime numbers?
(3 answers)
.NET ConcurrentDictionary initial capacity set to arbitrary prime number rather than expected capacity in MSDN example documentation. Why?
(1 answer)
Closed 4 months ago.
I was reading the source code of the dictionary and came across the resize method. I saw the formula temp = (currentSize * 2), currentSize = GetNextPrimalNumber(temp). Why we must to set primal number as a dictionary size? https://referencesource.microsoft.com/#mscorlib/system/collections/generic/dictionary.cs,440
Can somebody explain why we should use primal numbers as a size of dictionary? Thanks a lot !

Related

I saw this in code to do with lists and I'm not sure what it does [duplicate]

This question already has answers here:
What does it mean when there is a comma inside an array declaration? e.g. float[,]
(4 answers)
Closed 2 years ago.
So I'm trying to learn from other peoples code and I saw this at the end of a list and could not find it anywhere I looked
List<IMyShipMergeBlock>[,] merges = new List<IMyShipMergeBlock>[3, 2];
What is [,] and [3,2] called and could you point me to where I could learn more about it thanks
its a multidimensional array of List<IMyShipMergeBlocks>
The [3,2] is the initialiser for the array
so imagine a 3x2 grid and in each square there is a space for a List<IMyShipMergeBlocks>
EDIT: And important to note: You would then need to initialise each list separately as with the above you have an array of nulls to start with

Generate Each number between 2 numbers [duplicate]

This question already has answers here:
How to create a sequence of integers in C#?
(9 answers)
Closed 2 years ago.
I am trying to figure out a way to generate each number between 2 other numbers. For example, if the first number is 7 and the last number is 12 I want to generate an array(?) of Int that would be Intarray {7,8,9,10,11,12}.
Is this possible?
Enumerable.Range(,) will be your friend.

Why do I get System.OverflowException when init array [duplicate]

This question already has answers here:
What is the Maximum Size that an Array can hold?
(6 answers)
Closed 4 years ago.
byte[] buffer2 = new byte[4294743227]; // string with System.OverflowException
The number 4294743227 is uint.
Why do i get exception?
According to this question, the maximum size of an array is System.Int32.MaxValue, which is 2,147,483,647.
See also the documentation on System.Array:
By default, the maximum size of an Array is 2 gigabytes (GB). In a
64-bit environment, you can avoid the size restriction by setting the
enabled attribute of the gcAllowVeryLargeObjects configuration element
to true in the run-time environment. However, the array will still be
limited to a total of 4 billion elements, and to a maximum index of
0X7FEFFFFF in any given dimension (0X7FFFFFC7 for byte arrays and
arrays of single-byte structures).

Permutations/Combinations of bounded numbers in C# [duplicate]

This question already has answers here:
Listing all permutations of a string/integer
(28 answers)
How can I create cartesian product of vector of vectors?
(9 answers)
Closed 8 years ago.
Here's something which has been bugging me for the past two days.
I need to populate an initial configuration(/state)-space for a fixpoint algorithm.
In this statespace, each transition weight has a vector of weights, and different bounds may apply to each of the weights in this vector.
This is currently defined as an example transition weight being for example (5,-1,-1)
The bounds for each weight correspond to the index of the weight vector itself, for example the upper bounds for these weights, assuming the lower bound is 0 for all is given by (5,3,3)
Now, to set up the initial configuration space, i need to have every combination of weights available in the beginning.
(0,0,0) (0,0,1) (0,1,0) (1,0,0)... and so on, each of them going to their max bounds.
Now, if i was dealing with a 3-weighted system this would be trivial, but i need to support n-dimensional vectors in my code.
So, any ideas as to how i would accomplish populating this configuration space? (I'm using C# currently)
Here's the code for generating all ntuples implemented in Javascript. It's self-explanatory, but if you need further explanation, I'd be glad to help (I actually tried to write the algorithm in pseudocode but what I wrote ended up looking like the comments)

Get all combinations of an array ( if i remove them 1 by 1 ) [duplicate]

This question already has answers here:
Algorithm to return all combinations of k elements from n
(77 answers)
What is the best way to find all combinations of items in an array?
(11 answers)
Closed 9 years ago.
Given:
{1,2,3}
Expected result:
{1,2,3},
{1,2},{1,3},{2,3},
{1},{2},{3}
So I want basically ALL possible combinations in a list ( but including all possible combinations of - when every element is removed ).
I hope you get what I mean ;)
Question: Which algorithm achieves this?
You want the power set algorithm.
There are some examples on Rosetta Code.

Categories