#include<iostream>
#include<vector>
using namespace std;
bool prime[20000000];
int main(){
int x , total = 0;
vector<int> array;
for(int i = 0;i < 20000000; i++){
prime[i] = true;
}
for(int i = 2; i < 20000000; i++){
if(prime[i]){
for(int j = i * 2; j < 20000000; j += i){
prime[j] = false;
}
array.push_back(i);
}
}
prime[0] = false;
prime[1] = false;
while(cin >> x){
for(int i = 0 ;array[i] <= x ; i++){ //條件如果改成 i < array.size() 就AC
while(x % array[i] == 0){
total += array[i];
x /= array[i];
if(x == 1){
break;
}
}
}
if(total == 0){
cout << x << endl;
}
else{
cout << total << endl;
total = 0;
}
}
return 0;
}
#include
#include
using namespace std;
bool prime[20000000];
int main(){
int x , total = 0;
vector array;
for(int i = 0;i < 20000000; i++){
prime[i] = true;
}
for(int i = 2; i < 20000000; i++){
if(prime[i]){
for(int j = i * 2; j < 20000000; j += i){
prime[j] = false;
}
array.push_back(i);
}
}
prime[0] = false;
prime[1] = false;
while(cin >> x){
for(int i = 0 ;array[i] <= x ; i++){ //條件如果改成 i < array.size() 就AC
while(x % array[i] == 0){
total += array[i];
x /= array[i];
if(x == 1){
break;
}
}
}
if(total == 0){
cout << x << endl;
}
else{
cout << total << endl;
total = 0;
}
}
return 0;
}
質數建到 sqrt(20000000) 就可以了吧 ?