阅读程序 回答问题——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.温伯格说,我们不能要求每个人都聪明异常,能够解决所有难题:但是我们必须持续思考,因为只有如此,我们才能明白自己在做什 ...
随机推荐
- MySQL锁之三:MySQL的共享锁与排它锁编码演示
一.行锁之MySQL 使用SELECT ... FOR UPDATE 做事务写入前的确认 以MySQL 的InnoDB 为例,预设的Tansaction isolation level 为REPEA ...
- 继续循环continue
继续循环continue continue的作用是仅仅跳过本次循环,而整个循环体继续执行. 语句结构: for(初始条件;判断条件;循环后条件值更新) { if(特殊情况) { continue; } ...
- js读取iframe里的元素
父层 <div id="SubStepNav" class="SubStepNav"> <iframe src="aaa.html& ...
- linux达人养成计划
一.命令基本格式: ls -1 详细列表 (ll) -h 人性化显示文件大小 -a 显示所有文件,包括隐藏文件 -d 查看目录属性 -i 显示iNode 二.文件处理命令 mkdir -p [目录名] ...
- Java关键字final、static使用总结(转)
原文链接:Java关键字final, static一些要点 1. final final类不能被继承,没有子类,final类中的方法默认是final的 final方法不能被子类的方法复盖,但可以被 ...
- C# 自己动手实现Spy++(二)
昨天已经实现了获取窗口的标题.句柄等信息,但是高亮部分还有问题,而且红色绘制框擦除也有问题,今天就又研究了下上述两个问题. 高亮部分红色框只显示左上的边框,而右下的显示不出来,如图: 代码如下: pu ...
- 针对开发项目的NABCD的分析
N(Need需求) 我们的创意是用户登录我们的软件,就可以实现自己修改图片,添加文字.解决了目前用户不知道如何P图,如何添加文字的难题. A(Approach做法) 我们的团队将用VS软件开发一个修改 ...
- Javascript异步编程的4种方法(阮一峰)
转载: http://www.ruanyifeng.com/blog/2012/12/asynchronous%EF%BC%BFjavascript.html 你可能知道,Javascript语言的执 ...
- vue elment-ui 样式替换 input select
# 有时候经常需要替换element-ui的样式 第一种方法: 直接修改源码,样式路径如下 直接修改idnex.css即可. 第二种方法: 直接在当前页面修改,替换掉原来的样式. <style ...
- windows下mysql5.7 root密码重置
1.在mysql根目录下新建配置文件my.ini(因为我安装的mysql-5.7.24安装完成后未看到该配置文件,所以新建一个,有的直接修改即可) [mysqld] skip-grant-tables ...