import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; import java.util.Scanner; import java.io.*; import java.nio.file.SimpleFileVisitor; public class TwoSum { public static void main(String[] args) { int a=10; System.out.println(count(a)); } static int count(int num) { int length=num/2+1; int max=0; for (int i = 1; i < length; i++) { if(IsPrime(i)&&IsPrime(num-i)) { max++; } } return max; } static boolean IsPrime(int num) { boolean ret = true; int ubound =(int) (Math.sqrt(num)+1); for (int i = 2; i < ubound; i++) { if (num % i == 0) { ret = false; break; } } return ret; } }
import java.util.HashSet; import java.util.Scanner; import java.util.Set; /** * 题目描述 * 给定一个正整数,编写程序计算有多少对质数的和等于输入的这个正整数,并输出结果。输入值小于1000. * 如,输入为10,程序应该输出结果为2。(共有两对质数的和为10,分别为(5,5),(3,7)) */ public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int num = sc.nextInt(); int count = 0; Set<Integer> numberSet = new HashSet<>(); for (int i = 2; i < num; i++) { if (numberSet.contains(i)){ continue; } if (Main2.isPrimeNum(i)){ if (Main2.isPrimeNum(num - i)){ count++; numberSet.add(num - i); } } } System.out.println(count); } /** * 判断一个数是否是质数 * @param number 要判断的数 * @return 是否为质数:true是,false否 */ public static boolean isPrimeNum(int number){ boolean flag = true; for (int i = 2; i <= Math.sqrt(number); i++) { //排除偶数 if (i % 2 == 0 && i != 2){ continue; } if (number % i == 0){ flag = false; break; } } return flag; } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNext()) { int n = in.nextInt(); System.out.println(Solve(n)); } } public static int Solve(int n) { int sum = 0; for (int i = 2; i <= n / 2; i++) { if (isZhiShu(i) && isZhiShu(n - i)) sum++; } return sum; } //判断是否质数 public static boolean isZhiShu(int m) { for (int i = 2; i < Math.sqrt(m); i++) { if ((m % i) == 0) return false; } return true; } }
public class Main{ //我的思路:我会先把从1到n之间的所有质数都先找到,然后将这些质数两两相加,保证不能重复 //如果相加后的结果=n,则利用一个int型的变量+1;否则继续,最后输出这个变量即可 //这中间有两个难点:1、怎样筛选质数 2、怎样高效保证所有质数两两相加 public int countPairs(int n){ int[] zhishu = new int[n]; int j = 0; for(int i = 1; i<=n; i++){ //怎样取出质数 if(n==质数){ zhishu[j] = n; j++; } } int num = 0; for(int i = 0; i< zhishu.length(); i++){ for(int j=i; j < zhishu.length-i; j++){ if(zhishu[i] + zhishu[j] == n){ num++; } } } return num; } //主函数调用 public staic void main(String[] args){ System.out.print(countPairs); } }
import java.util.*; public class Main{ public static void main(String[] args){ Scanner scanner = new Scanner(System.in); int num = Integer.parseInt(scanner.next()); getPairs(num); } public static void getPairs(int num){ int pairs = 0; for(int i=2; i< num/2+1; i++){ if(isPrime(i)&&isPrime(num-i)){ pairs++; } } System.out.println(pairs); } public static boolean isPrime(int s){ boolean res = true; if(s==1){ res=false; } int bound = (int) (Math.sqrt(s)+1); for(int i=2; i<bound; i++){ if(s%i==0){ res= false; break; } } return res; } }
package prac_tencent;
import java.util.Scanner;
public class zhishu {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int x = scanner.nextInt();
int sum=0;
for (int i = x-1; i >= x/2; i--) {
if (isPrime(i)&&isPrime(x-i)) {
sum++;
}
}
System.out.println(sum);
}
public static boolean isPrime(int n) {
boolean flag = true;
if (n<=1) {
return flag = false;
}
for (int i = 2; i <=n/2; i++) {
if (n%i==0) {
flag = false;
break;
} }
return flag;
}}
#include <iostream> #include <vector> using namespace std; void primerNumInN(vector<int>& data, int n) { if(n < 2) return; data.push_back(2); for(int i = 3; i <= n; i++) { if(i%2 == 0) continue; int j = 3; while(j <= i/2 && i%j != 0) { j += 2; } if(j > i/2) data.push_back(i); } } int primerSum(vector<int>& data, int n) { int length = data.size(); int count = 0; for(int i = 0; i < length; i++) { for(int j = i; j < length; j++) { if(data[i] + data[j] == n) { //printf("%d and %d\n", data[i], data[j]); count++; } } } return count; } int main() { int n; cin >> n; vector<int> data; primerNumInN(data, n); int result = primerSum(data, n); printf("%d", result); return 0; }
public static void task6(){ Scanner sc = new Scanner(System.in); Boolean ad = true; int input = sc.nextInt(); //建立质数数组 ArrayList<Integer> prime = new ArrayList<Integer>(); for(int n=2;n<=1000;n++) { ad = true; for (int i = 2; i <= (int) sqrt(n); i++) { if(n%i==0) { ad = false; break; } } if(ad&&!prime.contains(n)) prime.add(n); } int count =0; for(int n=0;n<prime.size();n++){ for(int m = n;m<prime.size();m++){ if(prime.get(n)+prime.get(m)==input) count++; } } System.out.print(count); }
from math import sqrt n=int(raw_input()) num=0 if n <= 2: isPrime=0 isPrime = [x for x in range(n)] isPrime[0] = isPrime[1] = 0 for i in range(2, int(sqrt(n))+2): if isPrime[i] > 0: isPrime[i*i:n:i] = [0]*((n-1)/i-i+1) for i in range(len(isPrime)): if isPrime[i]>0: Cha=n-isPrime[i] for j in range(len(isPrime)): if isPrime[j] >0: Cha-=isPrime[j] if Cha==0: isPrime[i]=0 isPrime[j]=0 num+=1 Cha+=isPrime[j] print num