本文共 9528 字,大约阅读时间需要 31 分钟。
17
#include
using namespace std;
bool IsZhiShu(int m)
{
for (int i = 2; i < sqrt((float)m); i++)
{
if ((m % i) == 0)
return false;
}
return true;
}
int Solve(int n)
{
int sum = 0;
int i = 0;
for (i = 2; i <= n/2; i++)
{
if (IsZhiShu(i) && IsZhiShu(n - i))
sum++;
}
return sum;
}
int main()
{
int n = 0;
cin >> n;
cout << Solve(n) << endl;
system("pause");
return 0;
}
发表于 2017-08-21 10:47:15
回复(5)
6
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;
}
}
发表于 2017-09-10 12:42:07
回复(0)
4
import java.util.Scanner;
public class Test7 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int count = 0;
for(int i=2;i<=n/2;i++){
if(IsPrime(i) && IsPrime(n-i))
count++;
}
System.out.println(count);
}
public static boolean IsPrime(int num) {
if (num <= 1)
return false;
if (num == 2)
return true;
boolean flag = false;
for (int i = 2; i <= Math.sqrt(num); i++) {
if(num % i == 0){
flag = true;
break;
}
}
if(flag){
return false;
}else{
return true;
}
}
}
发表于 2018-04-04 23:04:50
回复(0)
2
/**
*主要思路,将 key-减数=差
*从1到key的1/2值时,判断减数和差是否是质数,是则对数加一
**/
public class DemoUtil { public static void main(String[] args) {
int conut = conut(15);
System.out.println(conut);
}
public static int conut(int key){
int c=0;
for (int i = 1; i <=key/2; i++) {
//将 key-减数=差
int cha=key-i;
//判断减数和差是否是质数
if(isSushu(i)&&isSushu(cha)){
//是则对数加一 c++;
}
}
return c;
}
//判断该数是否是质数 public static boolean isSushu(int key){
for (int i = 2; i <= key/2; i++) {
if(key%i==0){
return false;
}
}
return true;
}
}
发表于 2017-08-20 09:19:13
回复(1)
1
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 numberSet = new HashSet<>();
for (int i = 2; 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;
}
}
发表于 2020-08-22 23:14:33
回复(0)
1
def IsZhishu(n):
if n>1:
for i in range(2,n):
if n%i==0:
return False
return True
if __name__ == '__main__':
num = int(input("输入一个数"))
result=0
for i in range(num//2+1):
tmp=num-i
if IsZhishu(i) and IsZhishu(tmp):
print("质数和",i,tmp)
result+=1
print(result)
发表于 2019-08-15 20:06:49
回复(0)
1
#include
#include
#include
using namespace std;
bool isPrime(int num) {
if (num <= 0) {
cout << "wrong input" << endl;
return false;
}
int k = sqrt(num);
for (int i = 2; i <= k; ++i) {
if (num%i == 0)
return false;
}
return true;
}
bool inPrime(vector prime, int num) {
for (auto it = prime.begin(); it != prime.end(); it++)
if (num == *it)
return true;
return false;
}
int main() {
int input;
int count=0;
vector prime;
cin >> input;
for (int i = 2; i < 1000; ++i)
if (isPrime(i))
prime.push_back(i);
for (auto it = prime.begin(); it != prime.end() && *it <= input/2; it++)
{
int max = input - *it;
if (inPrime(prime, max))
count++;
}
cout << count << endl;
}
发表于 2018-07-26 18:21:16
回复(0)
1
思路:
1.从给定的数的一半的位置n/2开始向前遍历,然后每次遇到一个质数p1时,判断n-p1是否是质数,如果是则找到一个,继续向后前查找即可,直到
遇到找到2结束
发表于 2018-07-08 16:08:28
回复(0)
1
#include
int fun(int i)
{
int m=0;
if (i==1)
return m;
else
{
for(int j=2;j
{
if (i%j==0)
m=m+1;
}
return m;
}
}
void main()
{
int m,k=0;
printf("请输入一个取值为0到1000的正整数\n");
scanf("%d",&m);
for(int i=1;i<=m/2;i++)
{
if (fun(i)==0&&fun(m-i)==0)
k=k+1;
}
printf("%d\n",k);
}
发表于 2018-03-23 18:23:23
回复(0)
1
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;
}
}
发表于 2017-08-23 14:07:12
回复(0)
1
public static boolean ISNOT(int num){
if(num==2){
return true;
}
if(num<=2||num%2==0){
return false;
}
for(int i=3;i<=Math.sqrt(num);i+=2){
if(num%i==0){
return false;
}
}
return true;
}
public static int getsum(int num){
int sum=0;
if(ISNOT(num-2)){
sum++;
}
for(int i=1;i<=(num/2);i+=2){
if(ISNOT(i)&&ISNOT(num-i)){
sum++;
}
}
return sum;
}
发表于 2017-03-25 20:50:49
回复(0)
1
saasfa
发表于 2017-03-25 17:34:41
回复(0)
0
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
for(int j=i; j
if(zhishu[i] + zhishu[j] == n){
num++;
}
}
}
return num;
}
//主函数调用
public staic void main(String[] args){
System.out.print(countPairs);
}
}
发表于 2021-03-18 10:02:14
回复(0)
0
java
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
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
if(s%i==0){
res= false;
break;
}
}
return res;
}
}
发表于 2019-08-15 09:52:55
回复(0)
0
package prac_tencent;
import java.util.Scanner;
publicclasszhishu {
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);
}
publicstaticbooleanisPrime(intn) {
booleanflag=true;
if (n<=1) {
returnflag=false;
}
for (int i = 2; i <=n/2; i++) {
if (n%i==0) {
flag = false;
break;
} }
returnflag;
}}
发表于 2019-04-05 14:41:08
回复(0)
0
#include
#include
using namespace std;
void primerNumInN(vector& 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& 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 data;
primerNumInN(data, n);
int result = primerSum(data, n);
printf("%d", result);
return 0;
}
发表于 2019-04-04 16:07:46
回复(0)
0
public static voidtask6(){
Scanner sc = newScanner(System.in);
Boolean ad = true; intinput = sc.nextInt(); //建立质数数组ArrayList prime = newArrayList(); for(intn=2;n<=1000;n++) {
ad = true; for(inti = 2; i <= (int) sqrt(n); i++) { if(n%i==0) {
ad = false; break;
}
} if(ad&&!prime.contains(n))
prime.add(n);
} intcount =0; for(intn=0;n
count++;
}
}
System.out.print(count);
}
发表于 2019-04-03 20:16:46
回复(0)
0
int function(const int val)
{
deque de;
for (int i = 2; i < val; i++)
{
if (MyPrime(i))
{
de.push_back(i);
}
}
int count = 0;
while (! de.empty())
{
int front = de.front();
int back = de.back();
int sum = front + back;
if (sum == val)
{
//cout << front << " " << back << endl;
count++;
de.pop_back();
if (front == back)
{
break;
}
de.pop_front();
continue;
}
if (sum < val)
{
de.pop_front();
continue;
}
if (sum > val)
{
de.pop_back();
}
}
return count;
}
bool MyPrime(const int val)
{
if (val == 0 || val == 1)
{
return false;
}
for (int i = 2; i < val; i++)
{
if (val % i == 0)
{
return false;
}
}
return true;
}
发表于 2019-03-10 21:54:59
回复(0)
0
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
发表于 2019-03-08 20:44:48
回复(0)
0
//my solution ,O(n^2) ,not tested yet.
#include
using namespace;
int main(){
int a,q,p;
int odd = 0;
int odd2 = 0;
int res = 0;
cin >> a;
for(q = 2;q < 1000;q++){
for(int part = 2;part
if(q%part == 0) {
odd = 1;
break;
}
if(odd == 0){
for(p = q;p < 1000;p++){
for(int part2 = 2;part2 < p;part2++)
if(p%part == 0) {
odd2 = 1;
break;
} if(odd2 == 0){
if(q + p == a)
res++;
}
}
}
}
cout << res;
return 0;
}
发表于 2018-08-31 10:35:11
回复(0)
转载地址:http://rbtnx.baihongyu.com/