Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Hydreigon_Lord
- //This program generates all the factors of a given number.
- #include <iostream>
- #include <vector>
- #define POSITION (factors.begin() + pos)
- using namespace std;
- int main()
- {
- unsigned long long num;
- cout << "Enter a number: ";
- cin >> num;
- vector<unsigned long long> factors;
- factors.push_back(1);
- if (num != 1)
- {
- factors.push_back(num);
- int pos = 1;
- for (unsigned long long i = 2; i * i <= num; i++)
- {
- if (i * i == num)
- factors.insert(POSITION, i);
- else
- {
- if (num % i == 0)
- {
- factors.insert(POSITION, num / i);
- factors.insert(POSITION, i);
- pos++;
- }
- }
- }
- }
- cout << num << " has " << factors.size() << " factors: ";
- unsigned long long sum = 0;
- for (unsigned long long f: factors)
- {
- cout << f << " ";
- sum += f;
- }
- cout << endl;
- cout << "The sum of the factors is: " << sum << endl;
- double ratio = (double)sum / num;
- cout << "The ratio of the factor sum to the number is: " << ratio << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement