I need to use long numbers in my factor finding program. I am now aware of header that you can use called GMP that's very precise. So I downloaded it from this website:
http://gmplib.org/
In the folder that was downloaded there were lots of source files. They were all C source files though and I am working with C++? Also, which header files do I need to move to the folder of my application and how to I include the in my code?
Here's my code that I made if anyone is interested in finding factors of numbers:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
|
#include <iostream>
#include <cmath>
using namespace std;
int main(){
int TotalFactors = 0;
float TheNumber;
int Counter = 1;
cout.precision(16);
cout << " FACTOR FINDER " << endl;
cout << "***************" << endl;
cout << endl;
cout << "Enter Your Number: ";
while(!(cin >> TheNumber)){
cin.clear();
cin.ignore(100, '\n');
cout << "Please enter a valid 10 base whole number" << endl;
}
const int OriginalNumber = TheNumber;
const float SquareRoot = sqrt(TheNumber);
if (SquareRoot == int(SquareRoot)){
TotalFactors--;
}
while (TheNumber >= SquareRoot){
if (TheNumber == int(TheNumber)){
cout << Counter << " * " << TheNumber << endl;
TotalFactors+=2;
}
Counter++;
TheNumber -= TheNumber/Counter;
}
cout << "The number " << OriginalNumber << " has " << TotalFactors << " factors!" << endl;
cin.get();
return 0;
}
|
The main line is this one: TheNumber -= TheNumber/Counter;
What it does is as the program progress the number get's smaller. If the number is a whole number it's a factor. It's faster because diving by small numbers is going to be faster than dividing by big numbers.
Here's the pattern:
100 - 100/2 = 50;
50 - 50/3 = 33.33333;
33.33333 - 33.33333/4 = 25;
25 - 25/5 = 20;
20 - 20/6 = 16.66667;
16.66667 - 16.66667/7 = 14.28571;
14.28571 - 14.28571/8 = 12.5;
12.5 - 12.5/9 = 11.11111;
11.11111 - 11.11111/10 = 10;
And then it stops at 10 because that's the squareroot of 100.
Thanks.