4

Possible Duplicate:
Enumerable.Range implementation

I ran into an issue where i need to add a very long value (Int64) into a list, The value is 600851475143, i want to create a List<Int64> that contains all the int upto this value, but Enumerable.Range has a limitation that in the count parameter, it accept only int values, as i am far from that value, i decided to loop through the list and and all those values, but my system soons run out of memory, what should i do?

List<int64> lst = new List<int64>();

for (Int64 i = 3; i < 600851475143; i=i+2)
{
    lst.Add(i);
}

Thanks

Community
  • 1
  • 1
manav inder
  • 3,531
  • 16
  • 45
  • 62
  • 7
    You should explain what you're really trying to do, because there's no way you really need a 5TB array of 600B numbers! – Gabe Jan 05 '12 at 06:56
  • 2
    Is that really a good idea? Do you realize that list would take [4.37 terabytes](https://www.google.com/search?hl=en&q=600851475143%20\*%2064%20bit) of memory? – Kobi Jan 05 '12 at 06:56
  • 2
    Does your system have 600,851,475,143 x 8 bytes = I believe ~6 terabytes of RAM? If not then why wouldn't you expect this to fill up your memory? – Jamie Treworgy Jan 05 '12 at 06:57
  • Ok, what i am trying to do is, getting the largest prime number of this number, my application is creating another list of all those prime numbers, To get the prime numbers i am using the algo of "The Sieve of Eratosthenes" http://mathforum.org/dr.math/faq/faq.prime.num.html – manav inder Jan 05 '12 at 07:01
  • My friend, be asured - all numbers between 2 and 600851475143 are available, but I can't see any good reason to keep all those numbers in memory! Round about 5 TB of RAM is needed to do that useless job ... – Fischermaen Jan 05 '12 at 07:02
  • Ok, to make it little better i just eliminate all the even numbers from the list, but it'll still kill the system, isn't it – manav inder Jan 05 '12 at 07:05
  • @MSingh: If you kill all even numbers, it will still need more than 2 TB of memory!!!! – Fischermaen Jan 05 '12 at 07:09
  • Exactly, so i should go with a yield return statement and when ever a number is gonna add to my array, i first check if it divisble to any of my previously filtered prime numbers, i believe that'll put the leash on the beast, What do you think about it? Kindly guide – manav inder Jan 05 '12 at 07:12
  • 1
    For a sieve, you only need 600,851,475,143 *bits*, or half that if you don't store the even numbers. Of course, you still end up needing something like 38GB of memory, so you need to pick a different algorithm! – Gabe Jan 05 '12 at 07:14
  • I should point out that if you're trying to store all the prime numbers less than 600,851,475,143, you will still run out of memory, because there are something like 23,038,900,221 of them! – Gabe Jan 05 '12 at 07:23
  • Not sure why this was closed -- it's a very interesting question that does not reduce to "_What is the precise implementation of Enumerable.Range in .Net?_" If you did have to pull back a range this long but could, as the question (almost by definition) implies, perform actions and **throw away the vast majority of the numbers**, how would you do that? We're on the right track with `yield`, but the specific use case here still holds as a unique question that isn't answered elsewhere. – ruffin Jul 27 '20 at 21:46

1 Answers1

0

As pointed out in the comments, such a large array would require an enormous amount of memory. A better option would for instance be to create yourself an enumerator to loop over the range. Start looking at this page for how to: http://msdn.microsoft.com/en-us/library/9k7k7cf0.aspx :-)

Julian
  • 20,008
  • 17
  • 77
  • 108
  • yeah, that sounds like a brilliant idea, i'll remove all those values that are divisible to my previous prime numbers, – manav inder Jan 05 '12 at 07:10