Like many other programmers, I went into primes, and as many of them, what I like is the challenge, so I'm not looking for comment like Atkin did this faster than you dude, but just a solution - or at least an hint - to my issue.
I need to create big arrays (like size > int.MaxValue
). So I went to a lot of web pages and found the gcAllowVeryLargeObjects Element one. I thought I was saved, add the following magic to my App.config
:
<configuration>
<runtime>
<gcAllowVeryLargeObjects enabled="true" />
</runtime>
</configuration>
But it didn't worked. Here's the code I use :
void go(object sender, EventArgs eventArgs)
{
t.Stop();
ulong maxprime = 10;
Stopwatch stopwatch = new Stopwatch();
string s = String.Empty;
while (maxprime < ulong.MaxValue)
{
stopwatch.Restart();
richTextBox2.Text += Environment.NewLine + ("Max = " + maxprime.ToString("N0"));
try
{
richTextBox2.Text += Environment.NewLine + ("Count = " + GetAllPrimesLessThan(maxprime).Count);
richTextBox2.Text += Environment.NewLine + ("Time = " + stopwatch.Elapsed);
richTextBox2.Text += Environment.NewLine + ("--------------------------------");
maxprime *= 10;
richTextBox2.Refresh();
}
catch (Exception exception)
{
s = exception.Message + "; Allocation size: " + (maxprime + 1).ToString("N0");
break;
}
}
if (!string.IsNullOrEmpty(s))
{
richTextBox2.Text += Environment.NewLine + s;
}
richTextBox2.Text += Environment.NewLine + ("Done.");
}
private static List<ulong> GetAllPrimesLessThan(ulong maxPrime)
{
var primes = new List<ulong>() { 2 };
var maxSquareRoot = Math.Sqrt(maxPrime);
var eliminated = new bool[maxPrime + 1];
for (ulong i = 3; i <= maxPrime; i += 2)
{
if (!eliminated[i])
{
primes.Add(i);
if (i < maxSquareRoot)
{
for (ulong j = i * i; j <= maxPrime; j += 2 * i)
{
eliminated[j] = true;
}
}
}
}
return primes;
}
Which output this:
[...]
Max = 1?000?000?000
Count = 50847534
Time = 00:00:15.3355367
--------------------------------
Max = 10?000?000?000
Array dimensions exceeded supported range.; Allocation size: 10?000?000?001
Done.
How can I get rid of this error?
FYI: I've got
- 16GB ram;
- 32GB memory mapped(/paged?) on SSD;
- 64bits enabled
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…