Thread: Windows error dealing with long

  1. #16
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    I know that, however one of my programming friends alleged that this (factoring) could be done in < 1 minute using inputs of 123456700 to 123456800. This was the best algorithm I could come up with, yet it comes nowhere close. Any better algorithms?
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  2. #17
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    More craziness: When I put in 1000 to 10000, it gets all the way to 1521 and then gives me a windows error message. Any ideas?
    *slams head against desk* Now when I put in, say, 12 to 22, it suddenly quits, no prompt, nothing. What is wrong?
    *shoots self* My old code works with 100 to 1000, yet the "new, improved heap code" will just close. Help!
    Last edited by manutd; 11-18-2006 at 06:50 PM.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  3. #18
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Does it help if I tell you I had to restart my machine?
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  4. #19
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    No, I'm wondering why the "new (no pun intended)" code won't work with relatively small values, yet the "old" code will? This is really starting to get to me.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  5. #20
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You're going past the array bounds in your for loops. You should be using < instead of <=, or making your array one size bigger.

    The output to the console will really slow your program down. Just run it without that to see how fast or slow it is.

    I believe there are non-brute force methods of finding factors, you should do some research to see if you can find them.

  6. #21
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Just tried that Daved, and the problem remains. My old code will work, the new code, not so much.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  7. #22
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    What's the new code? It's small enough that you can post it instead of attach it.

  8. #23
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Code:
    template <typename T>
    void findfactors (T n)
    {
        T* arraynums = new T[n];
        for (T a = 0; a < n; a++)
        {
            arraynums[a] = 0;
        }
        arraynums[0] = 1;
        arraynums[n] = n;
        if (n == 2)
            for (T i = 3; i < n; i++)
            {
                if (n%i == 0)
                    arraynums[i-1] = i;
            }
        else
            for (T i = 2; i < n; i++)
            {
                if (n%i == 0)
                    arraynums[i] = i;
            }
        for (T j = 0; j < n; j++)
        {
            if (arraynums[j] != 0)
                cout << arraynums[j] << " * " << n/arraynums[j] << endl;
        }
        delete [] arraynums;
        arraynums = 0;
    }
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  9. #24
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    arraynums[n] = n;
    no such element - memory overrun

    Code:
            for (T i = 3; i < n; i++)
            {
                if (n%i == 0)
                    arraynums[i-1] = i;
            }
    this loop never runs

    3. no need to check value more than sqrt(n)
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  10. #25
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Getting rusty on C++ (Java is in syllabus at my university), so I wrote a brute force version for practice, but I have no guarantee that it is correct. It prints the factors of each integer in the range [123456700,123456800] in about a second or less, on my computer.

    I don't buy your argument about keeping your code relatively interchangeable when any C compiler will choke on your use of templates, iostreams and new[]/delete[], so I went ahead and used a std::vector.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #26
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Thanks so much, laserlight! That works perfectly.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Peculiar Problem with Printf
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-02-2002, 12:34 AM
  2. Peculiar Problem with Printf
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 07-02-2002, 12:03 AM
  3. Background-process app
    By philip in forum C++ Programming
    Replies: 14
    Last Post: 03-26-2002, 12:22 PM
  4. need help
    By emperor in forum C Programming
    Replies: 1
    Last Post: 03-04-2002, 12:26 PM
  5. can someone check this out and let me know ?
    By javaz in forum C Programming
    Replies: 5
    Last Post: 01-21-2002, 02:13 PM