Windows error dealing with long

This is a discussion on Windows error dealing with long within the C++ Programming forums, part of the General Programming Boards category; I know that, however one of my programming friends alleged that this (factoring) could be done in < 1 minute ...

  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 05:50 PM.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  3. #18
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,412
    Does it help if I tell you I had to restart my machine?
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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,317
    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,317
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    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)
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  10. #25
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,450
    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.
    Attached Files Attached Files
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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

Page 2 of 2 FirstFirst 12
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, 11:22 AM
  4. need help
    By emperor in forum C Programming
    Replies: 1
    Last Post: 03-04-2002, 11:26 AM
  5. can someone check this out and let me know ?
    By javaz in forum C Programming
    Replies: 5
    Last Post: 01-21-2002, 01:13 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21