- Jan 12, 2005
- 4,793
- 1
- 0
I am trying to make a code to find the prime factors of a number in C++, here is what i have so far,
#include <iostream>
#include <stdlib.h>
#include <math.h>
using namespace std;
double Abs(double Nbr)
{
if( Nbr >= 0 )
return Nbr;
else
return -Nbr;
}
double SquareRoot(double Nbr)
{
double Number = Nbr / 2;
const double Tolerance = 1.0e-7;
do Number = (Number + Nbr / Number) / 2;
while( Abs(Number * Number - Nbr) > Tolerance);
return Number;
}
int main(int argc, char *argv[])
{
double Number;
cin >> Number;
cout.precision(1);
double Nbr = SquareRoot(Number);
double x;
for (x = Nbr; x <= Nbr && x > 2; x--) {
cout << x << endl;
}
system ("PAUSE");
return 0;
}
I cannot take credit for the algorithim that finds the square root, I got that off the internet. I have done the rest. So far I can only find the numbers that are less than the square root, and I want to find the numbers less than the square root that have no remainders. I think that should get me the prime factors, Any suggestions?
#include <iostream>
#include <stdlib.h>
#include <math.h>
using namespace std;
double Abs(double Nbr)
{
if( Nbr >= 0 )
return Nbr;
else
return -Nbr;
}
double SquareRoot(double Nbr)
{
double Number = Nbr / 2;
const double Tolerance = 1.0e-7;
do Number = (Number + Nbr / Number) / 2;
while( Abs(Number * Number - Nbr) > Tolerance);
return Number;
}
int main(int argc, char *argv[])
{
double Number;
cin >> Number;
cout.precision(1);
double Nbr = SquareRoot(Number);
double x;
for (x = Nbr; x <= Nbr && x > 2; x--) {
cout << x << endl;
}
system ("PAUSE");
return 0;
}
I cannot take credit for the algorithim that finds the square root, I got that off the internet. I have done the rest. So far I can only find the numbers that are less than the square root, and I want to find the numbers less than the square root that have no remainders. I think that should get me the prime factors, Any suggestions?
