#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <vector> using namespace std; const int MAX = 4e4; bool arr[MAX]; vector<int> prime; void initial() { for (int i = 2; i < MAX; i++) { arr[i] = true; } for (int i = 2; i < MAX; i++) { if (!arr[i]) { continue; } prime.push_back(i); for (int j = 2 * i; j < MAX; j += i) { arr[j] = false; } } } int getNum(int n) { int total = 0; for (int i = 0; i < prime.size(); i++) { if (n < prime[i]) { break; } int sum = 0; while (n % prime[i] == 0) { sum++; n /= prime[i]; } total += sum; } if (n > 1) { total++; } return total; } int main() { initial(); int n; while (cin >> n) { cout << getNum(n) << endl; } return EXIT_SUCCESS; }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in=new Scanner(System.in); while (in.hasNext()) { Long n=in.nextLong(); int count=0; for (long i=2;i<=Math.sqrt(n);i++){ while (n % i == 0) { n/=i; count++; } if (n<=1) break; } if (n>1) count++; System.out.println(count); } in.close(); } }
#include<stdio.h> #define N 100001 int prime[N]; int primesize; bool mark[N]; void init(){ for(int i=0;i<N;i++) mark[i]=false; primesize=0; for(int i=2;i<N;i++){ if(mark[i]==true) continue; else{ prime[primesize++]=i; for(int j=i*i;j<N;j+=i) mark[i]=true; } } } int main(){ init(); int x; while(scanf("%d",&x)!=EOF){ int ansPrime[30]; int ansSize=0; int ansNum[30]={0}; for(int i=0;i<primesize;i++){ if(x%prime[i]==0){ ansPrime[ansSize]=i; ansNum[ansSize]=0; while(x%prime[i]==0){ ansNum[ansSize]++; x/=prime[i]; } ansSize++; if(x==1) break; } } if(x!=1){ ansPrime[ansSize]=x; ansNum[ansSize++]=1; } int ans=0; for(int i=0;i<ansSize;i++) ans+=ansNum[i]; printf("%d\n",ans); } return 0; }
#include<iostream> #include<vector> using namespace std; const int N = 4e4; vector<int> v; bool isPrime[N]; void init(){ fill(isPrime, isPrime + N, true); isPrime[0] = false; isPrime[1] = false; for(int i = 2;i < N;i++){ if(!isPrime[i]) continue; v.push_back(i); if(i > N / i) continue; for(int j = i * i;j < N;j += i) isPrime[j] = false; } } int main(){ int n, i; init(); int total = v.size(); while(scanf("%d", &n) != EOF){ int count = 0; while(n != 1){ for(i = 0;i < total;i++){ if(n % v[i] == 0){ count++; break; } } if(i < total) n /= v[i]; else break; } if(n == 1) printf("%d\n", count); else printf("%d\n", count + 1); } return 0; }
#include<bits/stdc++.h> using namespace std; const int maxn = 1e5; int primes[maxn+5]; //素数表 bool isPrime[maxn+5]; int cnt; void getPrimes() //线性欧拉筛拿素数表 { fill(isPrime, isPrime+maxn, true); isPrime[0] = isPrime[1] = false; cnt = 0; for(int i = 2;i <= maxn; i++) { if(isPrime[i]) primes[cnt++] = i; for(int j = 1;j < cnt && primes[j]*i <= maxn; j++) { isPrime[primes[j]*i] = false; if(i % primes[j] == 0) break; } } } int ansFacts(int n) //质因子个数 { int h = sqrt(n); int ans = 0; for(int i = 0;primes[i] <= h; i++) { while(n % primes[i] == 0) { ans++; n /= primes[i]; } if(n == 1) break; } if(n > 1) ans++; return ans; } int main() { ios::sync_with_stdio(false); int n; getPrimes(); while(cin >> n) { cout << ansFacts(n) << "\n"; } return 0; }
#include<bits/stdc++.h> using namespace std; int getFacts(int n) { int ans = 0; int h = sqrt(n); for(int i = 2;i <= h; i++) { while(n % i == 0) { ans++; n /= i; } if(n == 1) break; } if(n > 1) ans++; return ans; } int main() { ios::sync_with_stdio(false); int n; while(cin >> n) { cout << getFacts(n) << "\n"; } return 0; }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()){ int i = scanner.nextInt(); int count=0; for (int j = 2; j*j<=i ; j++) { while (i%j==0){ i/=j; count++; } } if (i>1) count++; System.out.println(count); } } }
#include<iostream> (720)#include<cmath> using namespace std; bool isPrime(int n){ bool flag=true; for(int i=2;i<=sqrt(n);i++){ if(n%i==0){ flag=false; break; } } return flag; } int main(){ int j=0,prime[1000];//求5000以内的所有质数 for(int i=2;i<5000;i++){ if(isPrime(i)){ prime[j]=i; j++; } } int N; while(cin>>N){ int sum=1; int i=0; while(!isPrime(N)){ if(N%prime[i]==0){ sum++; N=N/prime[i]; } else{ i++; } } cout<<sum<<endl; } return 0; }
//感觉以前好像没学过分解质因数诶,应该是没好好学吧T_T; #include<iostream> using namespace std; int NumberOfPrime(int x){ int number = 0; for(int i=2; i*i<=x; i++){ if(x%i==0){ number++; x /= i; i--; } } number++; return number; } int main(){ int N; while(cin>>N){ cout<<NumberOfPrime(N)<<endl; } return 0; }
#include<iostream> #include<cmath> using namespace std; int digui(int num) { int count = 0,i; for(i=2;i<=sqrt(num);i++) { if(num%i==0) {//i一定是一个小质数 count++; count+= digui(num/i); break; } } if(count==0)//说明是一个质数,count加1 count++; return count; } int main() { int num; cin>>num; cout<<digui(num); }递归求解
#include <stdio.h> #include <stdbool.h> unsigned int getPrimeFactor(unsigned long n) {// 返回n代表形参为质数 //此函数正常返回值为最小的质因数 for(unsigned long i = 2; i * i < n; ++i) { if(n % i == 0) return i; } return n; } int main(void) { unsigned long num; int count = 0; while(scanf("%ld", &num) != EOF) { unsigned int factor; bool flag = false; while( (factor = getPrimeFactor(num)) != num) { flag = true; count ++; num /= factor; } if(flag == true) count ++; printf("%d\n", count); } }
#include<iostream> #include<vector> #include<math.h> using namespace std; bool isPrime(int n) { if(n == 1)//1不是质数,但是为了循环结束条件逻辑一致 return true; for(int i = 2; i <= sqrt(n); i++) { if(n % i == 0) return false; } return true; } int main() { vector<int> prime; for(int i = 2; i <= 10000; i++) { if(isPrime(i)) prime.push_back(i); } int ret = 1; int index = 0; int num; cin >> num; while(!isPrime(num)) { if(num % prime[index] == 0) { num = num / prime[index]; ret++; } else index++; } cout << ret << endl; return 0; }
//王道简化版解法 #include<stdio.h> #include<math.h> int prime[100000]; int primeSize = 0; bool mark[100000] = { 0 }; void init() { for (int i = 2; i <= 100000; i++) { if (mark[i] == 0) { prime[primeSize++] = i; if (i <= 1000) { for (int j = i * i; j<100000; j += i) { mark[j] = true; } } } } } int main() { init(); int n; while (scanf("%d", &n) != EOF) { int count = 0, i = 0; while (n != 1 && i < primeSize) { if (n%prime[i] == 0) { count++; n /= prime[i]; } else i++; } if (n != 1) { count++; } printf("%d\n", count); } return 0; }
/* 此题是一道大数分解的题目,可以直接递归调用方法进行分解 */ #include<vector> #include<iostream> #include<cmath> using namespace std; //getFactor对num进行分解,将分解后的数放到vec中 void getFactor(const int& num, vector<int>& vec) { if (num == 1)//不需要分解,直接返回 return; int x = sqrt(num) + 1; int i = 2; for (i; i < x; ++i) { if (num%i == 0) break; } if (i == x) vec.push_back(num);//判断为质数,直接放入vec中 else//若不为质数,则继续进行分解 { getFactor(i, vec); getFactor(num / i, vec); } } int main() { int num; vector<int> rslt; while (cin >> num) { vector<int> vec; getFactor(num, vec); rslt.push_back(vec.size()); } for (auto&e : rslt) cout << e << endl; }