题目描述:
两个质数的和是 S,它们的积最大是多少?
代码:
package lanqiao;
import java.math.BigInteger;
import java.util.*;
public class Main {
public static int[] prime = new int[10000];
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int S = sc.nextInt();
getPrime(S);
int[] tmp = prime;
int max = 0;
int pro = 0;
for(int i = 0;i < S;i ++)
if(prime[i] != 0){
for(int j = 0;j < S;j ++)
{
if(tmp[j] != 0)
{
if(prime[i] + tmp[j] == S){
pro = prime[i] * tmp[j];
if(pro > max)
{
max = pro;
}
}
}
}
}
System.out.println(max);
}
//生成不大于S的所有质数
public static void getPrime(int S)
{
int k = 0;
for(int i = 2;i < S;i ++){
boolean flag = true;
for(int j = 2;j <= Math.sqrt(i);j ++)
{
if(i % j == 0)
{
flag = false;
break;
}
}
if(flag){
prime[k] = i;
k ++;
}
}
}
}