阅读程序 回答问题——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的更多相关文章

  1. C++走向远洋——60(十四周阅读程序、STL中的简单容器和迭代器)

    */ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:text.cpp * 作者:常轩 * 微信公众号:Worldhe ...

  2. C++走向远洋——52(十三周阅读程序)

    */ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:text.cpp * 作者:常轩 * 微信公众号:Worldhe ...

  3. CSP-J2019 NOIP普及组初赛真题(阅读程序部分)

    阅读程序(程序输入不超过数组或字符串定义的范围:判断题正确填√,错误填×:除特殊说明外,判断题1.5分,选择题3分,共计40分) #include <cstdio> #include &l ...

  4. C#代码分析--阅读程序,回答问题

    阅读下面程序,请回答如下问题: 问题1:这个程序要找的是符合什么条件的数? 问题2:这样的数存在么?符合这一条件的最小的数是什么? 问题3:在电脑上运行这一程序,你估计多长时间才能输出第一个结果?时间 ...

  5. 阅读c#程序——回答问题

    c#“小”程序: using System; using System.Collections.Generic; using System.Text; namespace FindTheNumber ...

  6. NOIP2016提高组初赛(2)四、阅读程序写结果2、

    #include <iostream> using namespace std; int main() { ][], b[][]; ]; string tmp; , j = , k = , ...

  7. C++走向远洋——66(十五周阅读程序)

    */ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:text.cpp * 作者:常轩 * 微信公众号:Worldhe ...

  8. c++中,bool与int 的区别

    菜鸟一枚,为了观察区别,特地运行了下面几个语句 /*阅读程序回答问题, 1.bool类型的false对应数值?true呢? 2.非0整数对应bool型的?0呢? */ #include<iost ...

  9. 程序开发心理学阅读笔记——第I篇

    1.软件的任务是为了解决某一特定的问题,而软件开发者的任务却需要解决一系列问题.2.温伯格说,我们不能要求每个人都聪明异常,能够解决所有难题:但是我们必须持续思考,因为只有如此,我们才能明白自己在做什 ...

随机推荐

  1. Java-Runoob-高级教程-实例-方法:09. Java 实例 – continue 关键字用法-un

    ylbtech-Java-Runoob-高级教程-实例-方法:09. Java 实例 – continue 关键字用法 1.返回顶部 1. Java 实例 - continue 关键字用法  Java ...

  2. sas 数据集导出到excel

    PROC EXPORT DATA= Loan.BOM_FILENAME_2      OUTFILE= "D:\output.xls"      DBMS=EXCEL REPLAC ...

  3. Javascript-string-Array

    1.得到数组里重复的值 function getRepeat(ar){ //数组排序 var ary = ar.sort(); //创建装重复值的新数组 var newArr = new Array( ...

  4. for 练习

    一张纸的厚度是0.0001米,将纸对折,对折多少次厚度超过珠峰高度8848米 var height = 0.00001; for (var i = 1; true; i++) { height *= ...

  5. 入坑机器学习?听听MIT在读博士的AI心得

    随着人工智能技术的火热,越来越多的年轻学者正准备投身其中,开启自己的研究之路.和所有其他学科一样,人工智能领域的新人总会遇到各种各样的难题,其中不仅有研究上的,也有生活方面的.MIT EECS 在读博 ...

  6. Android Activity传递数据使用getIntent()接收不到,揭秘Intent传递数据与Activity启动模式singleTask的关系。

    activity通过intent传递数据的时候,如果activity未启动,那么在这个刚启动的activity里通过getIntent()会获取到这个intent的数据.. 如果要启动的activit ...

  7. python第三方库,你要的这里都有

    Python的第三方库多的超出我的想象. python 第三方模块 转 https://github.com/masterpy/zwpy_lst   Chardet,字符编码探测器,可以自动检测文本. ...

  8. Django中的分页,cookies与session

    cookie Cookie的由来 大家都知道HTTP协议是无状态的. 无状态的意思是每次请求都是独立的,它的执行情况和结果与前面的请求和之后的请求都无直接关系,它不会受前面的请求响应情况直接影响,也不 ...

  9. this绑定丢失的问题

    在之前的一篇文章<this绑定>中已经说过this绑定的四种情况,也谈到了this绑定丢失的问题,但是没有解释为什么会出现this绑定的丢失,最近在多次阅读关于this绑定方面的文章之后, ...

  10. 导航栏 ------ z-index

    z-index 显示的层叠关系,数字越大越在上面 <!DOCTYPE html> <html lang="en"> <head> <met ...