标题: 将一个大于2的偶数分解成2个质数的所有组合 [打印本页] 作者: 我有上将潘凤 时间: 2016-5-12 19:44 标题: 将一个大于2的偶数分解成2个质数的所有组合 class Operator2 {
public static void main(String[] args) {
int n = 30;
int[] arr = GetPrime(n);
for (int i = 0;i<arr.length;i++) {
for (int j = i;j<arr.length;j++) {
if (arr+arr[j] == n) {
System.out.println(n+"="+arr+"+"+arr[j]);
}
}
}
}
//获取小于x的所有质数,并组成数组:
public static int[] GetPrime(int x) {
int number = 0;
int[] prime = {-1};
//获取数组长度:
for (int m = 2;m<=x;m++) {
int i = 2;
do {
if (i == m) {
number++;
//prime = new int[number];
//prime[number-1] = m;
}
i++;
}
while (i<=m && m % (i-1) != 0);
}
prime = new int[number]; //定义一个长度为质数个数的数组
//对数组的元素进行赋值:
number = 0;
for (int m = 2;m<=x;m++) {
int i = 2;
do {
if (i == m) {
number++;
//prime = new int[number];
prime[number-1] = m;
}
i++;
}
while (i<=m && m % (i-1) != 0);
}
return prime;
}
}