I want to open by saying that I am not very good at programming yet and there is probably a better way of doing what I want to. If looking at things that are so blatantly bad annoy you, then move on.
I’ve been giving a shot at the third question for Project Euler which is “Find the largest prime factor of 600851475143.” First I made something that calculates the factors of a number. (Excuse the names, I couldn’t think of anything.)
#include <iostream>
using namespace std;
void Factor (double dFactorer)
{
long dUpperlimit;
dUpperlimit = (int)dFactorer/2;
float fnumTouse;
fnumTouse = 1;
int nCounter;
nCounter = 0;
while (fnumTouse <= dUpperlimit)
{
if ((long)dFactorer % (long)fnumTouse == 0)
{
cout << fnumTouse << endl;
fnumTouse++;
nCounter++;
}
else
{
fnumTouse++;
}
}
cout << dFactorer << endl;
cout << "There are " << nCounter + 1 << " factors in this number";
}
int main()
{
double dNumtofac;
cout << "Enter a number to factor: ";
cin >> dNumtofac;
cout << endl;
Factor (dNumtofac);
return 0;
}
Alright, so, I know it’s a really shoddy job, what with all the casting I had to do to make certain things work. It works with smaller numbers but anything around the 100 millions makes it only output a certain number of factors before completely stopping. I tried the problem number, and it’s output was the number itself, saying that there is only one factor in this 600851475143. I’d like to know why it says this, is it something to do with the limits of the variables that I used? Something else? I don’t have enough knowledge to figure this out.