哥德巴赫猜想 Java实现
1.接口实现
package goldbach;
/**
* 输入一个大于6的偶数,请输出这个偶数能被分解为哪两个质数的和。如:10=3+7 12=5+7
* 此为按接口实现类完成
*
* @author GGGXXC
*
*/
public class InterfaceGoldbachGuess {
public static void main(String[] args) {
divideNum(new InterGoldBach(), 100);
} /**
* 将两个数字分成两个,分别判断是不是素数
* @param cp 传入的CheckPrime接口的实现类
* @param target 传入的目标值
*/
public static void divideNum(CheckPrime cp, int target) {
for (int i = 2; i <= target / 2; i++) { if (cp.isPrime(i) && cp.isPrime(100 - i)) {
System.out.println(i + "和" + (target - i));
} } } } /**
* 定义检查一个整数是不是素数的接口
*/
interface CheckPrime {
boolean isPrime(int n);
}
/**
* 采用接口的方式检查某个整数是不是素数
*
* @author GGGXXC
*
*/
class InterGoldBach implements CheckPrime {
public boolean isPrime(int n) { // 这里能加上static吗? int k = 2; while (k <= n / 2) {
if (n % k == 0) {
return false;
}
k++;
}
return true;
}
}
2.封装实现
package goldbach; /**
* 输入一个大于6的偶数,请输出这个偶数能被分解为哪两个质数的和。如:10=3+7 12=5+7
* 此为封装成方法的方式
*
* @author GGGXXC
*
*/
public class Goldbach {
public static void main(String[] args) { int target = 100; for (int i = 2; i <= target / 2; i++) {
boolean ret = isPrime(i) && isPrime(100 - i); if (ret) {
System.out.println(i + "和" + (target - i));
}
} } /**
* 判断传入的数字是不是质数
*
* @param num 需要判断是否是质数的数字
* @return 是质数返回true,不是质数返回false
*/
public static boolean isPrime(int num) { int k = 2; while (k <= num / 2) {
if (num % k == 0) {
return false;
}
k++;
}
return true;
} }
哥德巴赫猜想 Java实现的更多相关文章
- P1579_哥德巴赫猜想(JAVA语言)
题目背景 1742年6月7日哥德巴赫写信给当时的大数学家欧拉,正式提出了以下的猜想:任何一个大于9的奇数都可以表示成3个质数之和.质数是指除了1和本身之外没有其他约数的数,如2和11都是质数,而6不是 ...
- Java实现蓝桥杯算法提高 哥德巴赫猜想
试题 算法提高 哥德巴赫猜想 资源限制 时间限制:1.0s 内存限制:256.0MB 问题描述 根据所给函数(判断一个整数是否是素数),然后依托该函数,将输入N内的偶数(6-N),输出为两个素数之和( ...
- Java实现哥德巴赫猜想
验证哥德巴赫猜想:任何一个大于 6 的偶数,都能分解成两个质数的和.要求输入一个整数,输出这个 数能被分解成哪两个质数的和. eg : 14 14=3+11 14=7+7 public class T ...
- Java实现 洛谷 P1579 哥德巴赫猜想(升级版)
题目背景 1742年6月7日哥德巴赫写信给当时的大数学家欧拉,正式提出了以下的猜想:任何一个大于9的奇数都可以表示成3个质数之和.质数是指除了1和本身之外没有其他约数的数,如2和11都是质数,而6不是 ...
- *CF2.D(哥德巴赫猜想)
D. Taxes time limit per test 2 seconds memory limit per test 256 megabytes input standard input outp ...
- C#实现哥德巴赫猜想
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Goet ...
- code forces 382 D Taxes(数论--哥德巴赫猜想)
Taxes time limit per test 2 seconds memory limit per test 256 megabytes input standard input output ...
- CF735D Taxes 哥德巴赫猜想\判定素数 \进一步猜想
http://codeforces.com/problemset/problem/735/D 题意是..一个数n的贡献是它的最大的因子,这个因子不能等于它本身 然后呢..现在我们可以将n拆成任意个数的 ...
- Codeforces Round #382 (Div. 2) D. Taxes 哥德巴赫猜想
D. Taxes 题目链接 http://codeforces.com/contest/735/problem/D 题面 Mr. Funt now lives in a country with a ...
随机推荐
- 数学--数论--HDU--5878 Count Two Three 2016 ACM/ICPC Asia Regional Qingdao Online 1001
I will show you the most popular board game in the Shanghai Ingress Resistance Team. It all started ...
- 图论--树的重心(DFS) 模板
const int maxn=500005; int tot=0,n; int ans,size; int sx[maxn],head[maxn]; int vis[maxn]; struct edg ...
- postman(环境设置)
1.点击小齿轮进入到环境变量添加页面,点击add添加环境变量 2.新增环境输入变量名称和变量值 3.添加成功 4.接口中设置变量,切换环境进行传参 5.调用环境变量断言 调用环境变量中的phone变量 ...
- Openwrt:mtd/mtd_write烧写固件
文章目录 1 查看当前系统分区信息 2 备份固件firmware 3 恢复固件firmware 4 备份恢复Openwrt路由器配置 5 恢复Openwrt路由器默认设置 6 刷新路由器固件 比较简单 ...
- SVN 报错问题
svn: error: The subversion command line tools are no longer provided by Xcode ```. ## 问题分析 由于Mac绝大部分 ...
- uCOS2014.1.11
typedef unsigned char BOOLEAN;typedef unsigned char INT8U; /* Unsigned 8 bit quantity */ty ...
- uCOS2014.1.8
目前uCOS中已经接触到的全局变量: OSTCBCur OSIntNesting OSPrioHighRdy 最高优先级任务 任哲编著<嵌入式实时操作系统uC/OS-II原理及应用> ...
- markdown:列表、表格、代码实现
插入列表 1. dsf2. dsds 插入表格header 1 | header 2---|---row 1 col 1 | row 1 col 2row 2 col 1 | row 2 col 2 ...
- Office 2016 英文版(VOL版)下载
Office 2016 英文版(大客户版)下载磁力链接: 1.专业版(含project.visio) ProPlus, Project Pro, Visio Pro (x86-x64) magnet: ...
- 如何搭建一个WEB服务器项目(三)—— 实现安卓端联网登录
安卓端调用服务器登录函数进行验证登录 观前提示:本系列文章有关服务器以及后端程序这些概念,我写的全是自己的理解,并不一定正确,希望不要误人子弟.欢迎各位大佬来评论区提出问题或者是指出错误,分享宝贵经验 ...