Hello all, first of all great site I've been lurking ever since I had an interest in C++ (not very long) but never posted here before so I'm not sure if this should really go in beginners or just general C++ programming. So forgive me if this is in the wrong section please.
Anyways my problem today is with: http://projecteuler.net/problem=3
If you know the answer already please don't spoil it that's not what I'm asking for.
#include <iostream>
#include <fstream>
usingnamespace std;
int main()
{
longlong z; /*each time there is a prime factor,
z is set to it so when there's no prime factors left this variable will be the last highest prime factor. */
ofstream myfile("output.txt", ios::trunc);
for(longlong i=2;i<=(600851475143/2);i++)
{
if(600851475143%i == 0)
{
for(longlong f=2;f<=i/2;f++)
if (i%f == 0) myfile << i << " is a factor but not prime." << endl;
elseif (i%f != 0)
{
myfile << i << " is a prime factor." << endl;
z = i;
}
}
elseif(i == (600851475143/2)) { cout << z << " is the highest prime factor." << endl; }
}
myfile << endl;
myfile.close();
return 0;
}
First of all I'm thinking it would be better if I started the first for loop at half of 600851475143 and decreasing it till it finds the first and highest prime factor.
But the problem I'm having with this code is that it outputs the same numbers lots of times. For instance here is the first couple of prime numbers in output.txt repeated a lot of times by the program: http://pastebin.com/t62J0rGr
It also happens with non-prime factors but it seems to only happen 2 times for each one of those as apposed to the tons of repeats for prime factors.
Thank you for taking the time to read my post and also any hints on the actual problem are welcome as well.
Have you tested your code with the initial information you're given?
"The prime factors of 13195 are 5, 7, 13 and 29."
Once your program gives you an output of 5, 7, 13, and 29, you should then move on to the next step of the program, which is getting the largest prime factor for 600851475143 in under a minute of compiler run time (which should be easy, as you've already figured out the trick to dealing with big numbers in loops)
Check out line 21. Every time that i%f isn't zero you get a print out. Just remove that cout and I think your program will end up with the correct number.
Note that this algorithm is going to take a long time to compute. 30-60 min on my oldish (2005) computer.
Thanks for the responses everyone, yeah I realized that it's going to take a really long time when I got it working once. I was trying to figure it out without googling it but my code wasn't a very good idea for this as I found out.
But when I tried to implement it in my program I keep getting segment faults, like in a couple of the other programs I've tried. I could never figure out why either so I'm thinking it's just a limitation on arrays or something, since the only thing in common with the 2 programs that give them is the fact that they use arrays. So I was planning on reading up about some better container classes, I've read that I can just use vectors for everything I can use arrays for and that they have a lot of extra features that take extra steps to do with arrays.
In reply to Phil123:
Yes that explains why I'm getting lots of output, but not why I'm getting lots of the same output (unless I'm not understanding what you mean.) I know that it will eventually get to the highest prime factor, and that for the most part this functions properly, except the problem I described. Also just the fact that it isn't really optimal since this is the best I could come up with without asking my good friend Google.