质数 & 约数

1、判断质数

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 bool is_prime(int n){
 5     if(n < 2) return false;
 6     for(int i = 2; i <= n / i; ++i){
 7         if(n % i == 0) return false;
 8     }
 9     return true;
10 } 
11 
12 signed main(){
13     int n;
14     cin >> n;
15     
16     if(is_prime(n)) cout << "YES\n";
17     else cout << "NO\n";
18     
19     return 0;
20 } 

2、分解质因数

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 void div(int n){
 5     for(int  i = 2; i <= n / i; ++i){
 6         //i一定是质数 
 7         if(n % i ==0){ 
 8             int cnt = 0; //记录指数
 9             while(n % i == 0){
10                 n /= i;
11                 cnt++;
12             } 
13             cout << i << " " << cnt << "\n";
14         }
15     }
16     //一个合数最多有一个大于sqrt(n)的质因子
17     if(n > 1) cout << n << " 1\n"; 
18 }
19 
20 signed main(){
21     int x;
22     cin >> x;
23     div(x);
24     
25     return 0;
26 }

3、求一个数的约数集合

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 vector<int> to_divisors(int n){
 5     vector<int> res;
 6     for(int i = 1; i <= n / i; ++i){
 7         if(n % i == 0){
 8             res.push_back(i);
 9             if(i != n / i) res.push_back(n / i);
10         }
11     }
12     sort(res.begin(), res.end());
13     
14     return res;
15 }
16 
17 signed main(){
18     auto ans = to_divisors(100);
19     
20     for(int i = 0; i < ans.size(); ++i){
21         cout << ans[i] << " ";
22     }
23     
24     return 0;
25 }

4、约数个数

 1 /*
 2 求一个数的因子个数,等于分解质因数:
 3 n = a1^p1*a2^p2...*an^pn
 4 pi选择的组合数
 5 因子的个数:(p1 + 1) * (p2 + 1) ... (pn + 1) 
 6 */
 7 #include <bits/stdc++.h>
 8 using namespace std;
 9 
10 int cal_div(int n){
11     int res = 1;
12     for(int i = 2; i <= n / i; ++i){
13         if(n % i == 0){
14             int cnt = 0;
15             while(n % i == 0){
16                 n /= i;
17                 cnt++;
18             }
19             res *= (cnt + 1);
20         }
21     }
22 
23     if(n > 1) res <<= 1;
24     return res;
25 }
26 
27 signed main(){
28     int n = 12;
29     int res = cal_div(n);
30     
31     cout << res;
32     
33     return 0;
34 }

5、约数之和

 1 /*
 2 
 3 n = a1^p1*a2^p2...*an^pn
 4 
 5 因子的个数:(p1 + 1) * (p2 + 1) ... (pn + 1)
 6 
 7 因子之和:(a1^0 + a1^1 + ... a1^p1) * ( an^0 + an^1 + ... a2^pn) 该式子拆开就是各种因子 
 8 */
 9 #include <bits/stdc++.h>
10 using namespace std;
11 
12 int cal_div_sum(int n){
13     int res = 1;
14     for(int i = 2; i <= n / i; ++i){
15         if(n % i == 0){
16             int cnt = 0;
17             while(n % i == 0){
18                 n /= i;
19                 cnt++;
20             }
21             int t = 1;
22             for(int j = 0; j < cnt; ++j) t = (i * t + 1);
23             res = res * t;
24         }
25     }
26 
27     if(n > 1) res = res * (n + 1);
28     return res;
29 }
30 
31 signed main(){
32     int n;
33     cin >> n; 
34     int res = cal_div_sum(n);
35 
36     cout << res;
37 
38     return 0;
39 }

 

posted @ 2022-04-03 16:13  std&ice  阅读(90)  评论(0编辑  收藏  举报