Find the sum of all prime factors of a number (1 is not considered a prime number).
For example:
- 6 = 2 * 3, so the output is 2 + 3 = 5
- 8 = 2 * 2 * 2, so the output is 2 + 2 + 2 = 6
Sample Inputs/Outputs
Sample Input(s) | Sample Output(s) |
---|---|
One positive integer per line, with x < 2000000000, until EOF. | The corresponding sum of prime factors. |
19 32 | 19 10 |
Thought Process
Using a For loop to determine how many prime numbers there are in the input integers (only need to check up to the square root of the input data), and it is recommended to use cin for optimization. According to the test data, 1 is not considered a prime number, so if the input data is a prime number, simply output that prime number.
Sample Code-ZeroJudge A740: Sum of Prime Factors
#include <iostream>
#include <math.h>
using namespace std;
int main() {
cin.sync_with_stdio(0);
cin.tie(nullptr);
int num;
while (cin >> num)
{
int ans = 0, OG = num;
for (int i = 2; i <= (sqrt(OG))+1; i++)
{
if (num % i == 0)
{
while (num % i == 0 && num > 1)
{
ans += i;
num /= i;
}
}
}
if (num != 1) ans += num;
cout << ans << endl;
}
}
//ZeroJudge A740
//Dr. SeanXD