Ah that's great thanks, didn't think of that :)
I'll add that :D
K0ns3rv
Junior Poster in Training
92 posts since Oct 2009
Reputation Points: 11
Solved Threads: 8
On another note I am confused by the deprecated headers.
I can compile the program with the following includes
#include <iostream>
#include <fstream.h>
#include <vector.h>
#include <cmath>
But I of course still get the warning/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the header for the header for C++ includes, or instead of the deprecated header . To disable this warning use -Wno-deprecated.
I checked 17.4.1.2 of the C++ standard and found that the following include should be correct
#include <iostream>
#include <fstream>
#include <vector>
#include <cmath>
It isn't, the compiler is not able to locate the ofstream object an a few other thigs.
If i include like this
#include <iostream>
#include <fstream.h>
#include <vector>
#include <cmath>
It doesn't complain about the vectors but, for some reason ios and app on the following line are undeclared
file.open ( outputPath, ios::app );
Just update GCC.
I am quite confused by this :)
K0ns3rv
Junior Poster in Training
92 posts since Oct 2009
Reputation Points: 11
Solved Threads: 8
You can delete line 158 and 193. Also quoted strings i.e "quotedStrings" are automatically null terminated via compiler, so you don't have to manually add null character like you did here "-c\0"
firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
You can delete line 158 and 193. Also quoted strings i.e "quotedStrings" are automatically null terminated via compiler, so you don't have to manually add null character like you did here "-c\0"
I also thought so, but I didn't work. I tried checking it and the input argument and the compared "-X" weren't the same size. I guess something is wrong with my compiler settings. Thanks anyway :)
K0ns3rv
Junior Poster in Training
92 posts since Oct 2009
Reputation Points: 11
Solved Threads: 8
The same thing I mentioned before, about cutting your search
off after reaching sqrt(n) instead of n, in isPrime(), applies
equally well to your function find(); That is, your call to
find
std::vector primes = find ( value, false, NULL, true );
should be
std::vector primes = find ( sqrt(value), false, NULL, true );
since you don't need primes beyond sqrt(value) to do the prime factorization
of value.
However, you can do even better by NOT getting all the primes first.
If the number is large, and has some factors, you will do better by
just finding the next prime up, and testing it, then dividing down the
number by that prime. Further, if your test for divisibility of the
number succeeds, after you should divide down the number, you should test again,
until you use up this factor entirely. In this way, you can avoid
a large prime search entirely for a number which is a high power
of some small primes, like 2^10.
Don't know if I agree with the call find( sqrt( value ) , false, NULL , true ) because then isPime will be working with the second root since find calls isPrim on line 73.
The other point you made is great, I'll be sure to implement that. It's gonna save up a lot of memory and CPU power, weird how I didn't do it that way in the first place since that's how you would do it on paper.
K0ns3rv
Junior Poster in Training
92 posts since Oct 2009
Reputation Points: 11
Solved Threads: 8
Glad the suggestion helped. About your first comment on the sqrt(value) suggestion:
It looks to me like you would never need primes larger than sqrt(value), in the call to find() on line 104. Why do you want them?
I don't see why that would be, but in any case it doesn't matter since as you already pointed out getting all primes less than the value is redundant anyways.
EDIT: Just realized why you are correct about that.
K0ns3rv
Junior Poster in Training
92 posts since Oct 2009
Reputation Points: 11
Solved Threads: 8
Don't know if I agree with the call find( sqrt( value ) , false, NULL , true ) because then isPime will be working with the second root since find calls isPrim on line 73.
The other point you made is great, I'll be sure to implement that. It's gonna save up a lot of memory and CPU power, weird how I didn't do it that way in the first place since that's how you would do it on paper.
If you want to save CPU power, then consider looking at faster algorithms. The prim tester you have runs in n*sqrt(n). To start, have a look at http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes for finding primes.
firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608