普通做法
public class Main {
static int j = 0;
static int[] ssss = new int[10001];
public static void main(String[] args) {
sn(30);
for (int i = 0; i < j; i++) {
System.out.println(ssss[i]);
/*
1
30
2
15
3
10
5
6
*/
}
}
/*
求一个数的所有因子
*/
public static void sn(int s){
for(int i = 1;i <= s/i;i++) {
if (s % i == 0) {
ssss[j++] = i;
if (i * i != s)
ssss[j++] = s / i;
}
}
}
}
唯一分解定理
import java.util.Scanner;
public class Main {
static int index = 0;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] su = new int[100001]; //素数表
int[] sus = new int[100001]; //在唯一分解定理中出现的素数
int[] ci = new int[100001]; //各个素数的指数
int ind = 0;
sushu(su, n);
int c = n;
boolean flag = false;
//唯一分解定理 求出 x = p1^a1 * p2^a2 .... 的形式
for (int i = 0; i < index; i++) {
if (c == 1) {
break;
}
while (c % su[i] == 0) {
c /= su[i];
sus[ind] = su[i];
ci[ind]++;
flag = true;
}
if (flag) {
ind++;
}
flag = false;
}
int ans = 1;
for(int i = 0;i<ind;i++){
ans *= (1+ci[i]);//求出因子个数
}
System.out.println(ans);
}
//构造素数表
public static void sushu(int[] su,int n) {
boolean[] vis = new boolean[n + 1];
int m = (int) Math.sqrt(n + 0.5);
for (int i = 2; i <= m; i++) {
if (vis[i] == false) {
for (int j = i * i; j <= n; j += i) {
vis[j] = true;
}
}
}
for(int i = 2;i<=n;i++){
if(vis[i] == false){
su[index++] = i;
}
}
}
}