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?
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?
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.
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.
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.
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.
Just tried that Daved, and the problem remains. My old code will work, the new code, not so much.
What's the new code? It's small enough that you can post it instead of attach it.
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; }
no such element - memory overrunCode:arraynums[n] = n;
this loop never runsCode:for (T i = 3; i < n; i++) { if (n%i == 0) arraynums[i-1] = i; }
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
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.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Thanks so much, laserlight! That works perfectly.