0

I am trying to create a program that will give me every single digit between 1 and 55 but 6 times in a row.

That is;

1-17-25-45-49-51

55-54-53-52-51-50

Note, two numbers can never repeat in a row.

I've tested this program on a smaller scale and so far it does as intended. However, if I run it full scale, as the code posted bellow shows, it runs into an "out of memory exception". My guess is, its because of the memory having to hold such big numbers while building the ArrayList and file consecutively.

How do I go about fixing this?

class MainClass

{

    public static void Main (string[] args)

    {

        ArrayList newWords= new ArrayList();
        System.Console.WriteLine ("Please enter word to prepend ");
        int wordCount = 0;
        string numbers;
        string word = Console.ReadLine ();
        string word2 = word;
        string separator = "-";

        System.Console.WriteLine ("Please enter location of where you would like the new text file to be saved: ");
        string newFileDestination = Console.ReadLine ();

        TextWriter writeToNewFile = new StreamWriter (newFileDestination);

        for(int a = 1; a < 56; a++){
            for(int b = 1; b < 56; b++){
                for(int c = 1;  c < 56; c++){
                    for(int d = 1; d < 56; d++){
                        for(int e = 1;  e < 56; e++){
                            for(int f = 1; f < 56; f++){
                                if(a != b && a != c && a != c && a != e && a != f && b != c && b != d && b != e && b != f && c != d && c != e && c != f && d != e && d != f && e != f){
                                    word = word2;
                                    numbers = string.Concat(a, separator,  b, separator, c, separator, d, separator, e, separator, f);
                                    word += numbers;
                                    newWords.Add (word);
                                }
                            }
                        }
                    }
                }
            }
        }

        foreach(string line in newWords){
            writeToNewFile.WriteLine(line);
            Console.WriteLine (line);
            wordCount++;
        }

        writeToNewFile.Close ();
        Console.WriteLine (wordCount);
        Console.WriteLine ("Press any key to exit.");
        System.Console.ReadKey ();
    }
}
}
3
  • Don't use ArrayList. Use a List<T>. Commented Jun 12, 2015 at 2:09
  • 2
    It is 20 872 566 000 rows (55*54*53*52*51*50). If you encode each element as byte this is 120 gigs. Do you really need that gigantic list? I doubt that you have that much RAM anyway. Commented Jun 12, 2015 at 2:11
  • Looks like a "Variations without Repetition" candidate for the Combinatorics Library. I've used it before with great success... Commented Jun 12, 2015 at 4:35

1 Answer 1

2

Without trying to fix the rest of your code, the basic principle you need to follow is to write out the numbers as you get them, instead of cumulating them in a list. You can get rid of the ArrayList altogether, and the accompanying foreach loop.

The relevant part would look like this:

    TextWriter writeToNewFile = new StreamWriter (newFileDestination);

    for(int a = 1; a < 56; a++){
        for(int b = 1; b < 56; b++){
            for(int c = 1;  c < 56; c++){
                for(int d = 1; d < 56; d++){
                    for(int e = 1;  e < 56; e++){
                        for(int f = 1; f < 56; f++){
                            if(a != b && a != c && a != c && a != e && a != f && b != c && b != d && b != e && b != f && c != d && c != e && c != f && d != e && d != f && e != f){
                                word = word2;
                                numbers = string.Concat(a, separator,  b, separator, c, separator, d, separator, e, separator, f);
                                word += numbers;
                                writeToNewFile.WriteLine(word);
                                Console.WriteLine (word);
                                wordCount++;
                            }
                        }
                    }
                }
            }
        }
    }

But, be warned. Your code will take a long time to complete. You just won't get the out of memory error. You may run out of hard disk space though :)

Sign up to request clarification or add additional context in comments.

3 Comments

How is it that the rest of my code is faulty. You said "without trying to fix the rest of your code". What did you mean? I want to improve my code.
@Kitoliwa: Commendable attitude on your part! Though, I should have said "improve" instead of "fix". A few improvements that are just good practices in general: 1) Use List<string> instead of ArrayList. Strongly typed collections are preferable. 2) It's better to wrap the TextWriter inside a using block, rather than performing a .Close() yourself. This ensures that the file handle gets released, even in the event of an exception. (no big deal in this case, because you're inside the main()) 3) Your use of word, word2, numbers can be simplified. But these are all minor details.
Thank you so much for the advice, I appreciate it. Programming has become addicting for me, I just want to do it in a more efficient manner. Happy coding!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.