阅读程序 回答问题——FindTheNumber
阅读程序 回答问题——FindTheNumber
阅读下面程序,请回答如下问题:
问题1:这个程序要找的是符合什么条件的数?
问题2:这样的数存在么?符合这一条件的最小的数是什么?
问题3:在电脑上运行这一程序,你估计多长时间才能输出第一个结果?时间精确到分钟(电脑:单核CPU 4.0G Hz,内存和硬盘等资源充足)。
问题4:在多核电脑上如何提高这一程序的运行效率?
using System;
using System.Collections.Generic;
using System.Text;
namespace FindTheNumber
{
class Program
{
static void Main(string[] args)
{
int [] rg =
{,,,,,,,,,,,,,,,,,,
,,,,,,,,,,,};
for (Int64 i = ; i < Int64.MaxValue; i++)
{
int hit = ;
int hit1 = -;
int hit2 = -;
for (int j = ; (j < rg.Length) && (hit <=) ; j++)
{
if ((i % rg[j]) != )
{
hit++;
if (hit == )
{
hit1 = j;
}
else if (hit == )
{
hit2 = j;
}
else
break;
}
}
if ((hit == )&& (hit1+==hit2))
{
Console.WriteLine("found {0}", i);
}
}
}
}
}
回答:
1.程序要找到这样一个数TheNumber,该数 只能 被相临的 两个2到31之间的 数 不整除。
2.存在 最小数是2123581660200。
//3.这是一个13位数,主频为4.0 GHz的电脑,只是从1 加到2123581660200,也需要大约八,九分钟。加上循环,取余,判断,时间将翻倍。
4.多核,多线程编程,精简代码来提高运行效率,或者说换个算法来实现。
分析:
首先,我们先理清几个概念:
能被 x*y 整除,就一定能被 x 或 y 整除(能被 6 整除,就一定能被 2 或 3 整除);
推导:不能被 x 整除,就一定不能被 x*y 整除(不能被 2 整除,就一定不能被 6 整除)。
想要找到TheNumber,就要先确定这两个不能除尽的数,由上面推导:这俩数不能太小(不能被 4 整除的数,也不能被 8 整除);
最大范围是 31,而最小的素数为 2,(不能被 15.5 整除的数,也不能被 31 整除)所以这俩数从 16,开始取。
数字 2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31中,有些数是可以被取代的;
初等数学基本定理:任一大于1的自然数,要么本身是质数,要么可以分解为几个质数之积,且这种分解是唯一的。
31以内的素数有:2 3 5 7 11 13 17 19 23 29 31,要覆盖到所有2~31的数,2需要4个(2的5次方等于32大于31),
3需要3个(3的3次方等于27),5需要2个(5的平方等于25),7以上需要1个就够了(7的平方等于49大于31)。
也就是说:2~31里的关键数为:16, 27, 25, 7, 11, 13, 17, 19, 23, 29, 31;
这几个数的乘积可以被2~31的任何数整除,换句话说就是,少了谁,谁就不能被整除,其他的还都能;
再换句话说:就是找“少了谁,谁就不能被整除,其他的还都能”的数要在关键数里找。
两个不能除尽的数 是 16~31 任选的(是不是任选的再定),但关键数里只有16与17 是相临的。
姑且就选它俩了,16由2的4次方来;带走一个2,剩下2的3次方等于8;17由17的1次方来;带走一个17,剩下17的0次方等于1;
选16与17 所得的结果为,8*27*25*7*11*13*1*19*23*29*31=2123581660200。唯一的 最小的。
之后可以爱乘谁,乘谁了(当然不能是16,17)。。。
>>>追加
新的算法:思路跟上面的一样。
代码如下:
package hahahaha;
import java.math.BigInteger;
public class theNumber {
static int upline = 31;
static int num1;
static int num2;
static String prime[] = { "2", "3", "5", "7", "11", "13", "17", "19", "23", "29", "31", "37", "41", "43" };
static int primenum[] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43 };
static String thenumber;
static BigInteger zero = new BigInteger("0");
static BigInteger max = new BigInteger("9223372036854775807");
static BigInteger i = new BigInteger("2123581660200");
public static void main(String[] args) {
// 定界
for (int i = 0; Integer.parseInt(prime[i]) <= upline; i++) {
for (int j = 1;; j++) {
int a = (int) Math.pow(Integer.parseInt(prime[i]), j);
if (a > upline) {
a = a / Integer.parseInt(prime[i]);
prime[i] = String.valueOf(a);
break;
}
}
}
// 找数(16,17)
for (int i = 0; Integer.parseInt(prime[i]) <= upline; i++) {
for (int j = 0; Integer.parseInt(prime[j]) <= upline; j++) {
if (Integer.parseInt(prime[i]) - Integer.parseInt(prime[j]) == 1) {
num1 = i;
num2 = j;
}
}
}
// 造数
BigInteger thenum = new BigInteger("1");
for (int i = 0; Integer.parseInt(prime[i]) <= upline; i++) {
if (i != num1 && i != num2) {
BigInteger num = new BigInteger(prime[i]);
thenum = thenum.multiply(num);
}
}
int a = Integer.parseInt(prime[num1]) / primenum[num1];
BigInteger numa = new BigInteger(String.valueOf(a));
thenum = thenum.multiply(numa);
int b = Integer.parseInt(prime[num2]) / primenum[num2];
BigInteger numb = new BigInteger(String.valueOf(b));
thenum = thenum.multiply(numb);
thenumber = thenum.toString();
if (checkout(Integer.parseInt(prime[num2]), thenumber) == 0) {
System.out.println(thenumber);
}
//多来几个
for (int j = 0; j < 1; j++) {
for (int i = 0; Integer.parseInt(prime[i]) <= upline; i++) {
if (i != num1 && i != num2) {
BigInteger num = new BigInteger(prime[i]);
thenum = thenum.multiply(num);
thenumber = thenum.toString();
if (checkout(Integer.parseInt(prime[num2]), thenumber) == 0) {
System.out.println(thenumber);
}
}
}
}
}
public static int checkout(int a, String num) {
// 检查
BigInteger zero = new BigInteger("0");
BigInteger i = new BigInteger(num);
int hit = 0;
int hit1 = -1;
int hit2 = -1;
for (int j = 2; j <= 31; j++) {
String m = String.valueOf(j);
BigInteger n = new BigInteger(m);
BigInteger o = i.mod(n);
int r = o.compareTo(zero);
if (r != 0) {
hit++;
if (hit == 1) {
hit1 = j;
} else if (hit == 2) {
hit2 = j;
} else
break;
}
}
if ((hit1 == a) && (hit2 == a + 1)) {
return 0;// 正确
} else {
return 1;// 错误
}
}
}
运行的结果:

还可以更多。。。
:)
J.X.Dinosaur
阅读程序 回答问题——FindTheNumber的更多相关文章
- C++走向远洋——60(十四周阅读程序、STL中的简单容器和迭代器)
*/ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:text.cpp * 作者:常轩 * 微信公众号:Worldhe ...
- C++走向远洋——52(十三周阅读程序)
*/ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:text.cpp * 作者:常轩 * 微信公众号:Worldhe ...
- CSP-J2019 NOIP普及组初赛真题(阅读程序部分)
阅读程序(程序输入不超过数组或字符串定义的范围:判断题正确填√,错误填×:除特殊说明外,判断题1.5分,选择题3分,共计40分) #include <cstdio> #include &l ...
- C#代码分析--阅读程序,回答问题
阅读下面程序,请回答如下问题: 问题1:这个程序要找的是符合什么条件的数? 问题2:这样的数存在么?符合这一条件的最小的数是什么? 问题3:在电脑上运行这一程序,你估计多长时间才能输出第一个结果?时间 ...
- 阅读c#程序——回答问题
c#“小”程序: using System; using System.Collections.Generic; using System.Text; namespace FindTheNumber ...
- NOIP2016提高组初赛(2)四、阅读程序写结果2、
#include <iostream> using namespace std; int main() { ][], b[][]; ]; string tmp; , j = , k = , ...
- C++走向远洋——66(十五周阅读程序)
*/ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:text.cpp * 作者:常轩 * 微信公众号:Worldhe ...
- c++中,bool与int 的区别
菜鸟一枚,为了观察区别,特地运行了下面几个语句 /*阅读程序回答问题, 1.bool类型的false对应数值?true呢? 2.非0整数对应bool型的?0呢? */ #include<iost ...
- 程序开发心理学阅读笔记——第I篇
1.软件的任务是为了解决某一特定的问题,而软件开发者的任务却需要解决一系列问题.2.温伯格说,我们不能要求每个人都聪明异常,能够解决所有难题:但是我们必须持续思考,因为只有如此,我们才能明白自己在做什 ...
随机推荐
- P2412高精度减法
传送门 因为忘了带书回家,所以因为这道题我卡了半小时所以写篇博客“纪念”下 高精度减法中,如果被减数比减数小,就要用减数减去被减数.接下来的判断就是本题的核心.直接用strcmp是不行的,例如100与 ...
- 《Effective Java》笔记-服务提供者框架
静态工厂方法返回的对象所属的类,在编写包含该静态工厂方法的类时可以不必存在.这种灵活的静态工厂方法构成了服务提供者框架(Service Provider Framework)的基础,例如JDBC AP ...
- folly无锁队列,尝试添加新的函数(续)
基于上一篇文章,dropHead取出节点后,删除节点,会出现内存访问的问题.按照这个逻辑,如果将移出的节点保存到一个无锁队列中,然后在需要节点的时候,从这个备用的无锁队列中取出节点,那么应该就可以避开 ...
- String MVC @RequestParam(required=false) int XXX 参数为空报错解决方法
今天在用@RequestParam(required=false) int XXX 取参数的时候,当参数没有的时候Spring默认赋值为空.而此时使用基本类型int,所以报错,建议使用包装类 Inte ...
- maven工程强制更新 ,下载源码操作
看图,在window->preferences->maven , 勾选图中的选项就可以下载源码包了,
- 安装sublime txt3 并且设置为默认的text打开方式
1.安装 安装可以参考 http://jingyan.baidu.com/article/fa4125acb8569b28ac7092ea.html 1.添加sublime text 3的仓库: su ...
- 好文推荐:转载一篇别人kaggle的经验分享
转载:https://www.toutiao.com/i6435866304363627010/ 笔者参加了由Quora举办的Quora Question Pairs比赛,并且获得了前1%的成绩.这是 ...
- BCGcontrolBar(七) 添加仪表盘、动态图表显示等控件
BCG的 BCGPGaugesDemo有众多仪表盘控件可以参考使用 编写时同ListCtrl一样 在停靠面板上加入仪表盘和动态曲线 主要代码 //插入CPU图形 pContainer->SetF ...
- java 根据日期获取星期
private String getWeek(String date) { String[] arr=date.split("-"); Calendar calendar = Ca ...
- python各种推导式
字典推导式 例一:将一个字典的key和value对调 mcase = {, } mcase_frequency = {mcase[k]: k for k in mcase} print(mcase_f ...