阅读程序 回答问题——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. P1616疯狂的采药

    传送 它不是可爱的01背包了!!!这个题中一种药可以采无限次!!! 它进化成了完全背包.完全背包中的内循环从m到v[i]改成了从v[i]到m 既然如此,代码如下: #include<iostre ...

  2. springboot(整合多数据源demo,aop,定时任务,异步方法调用,以及获取properties中自定义的变量值)

    有这么一个需求 每个部门,需要操作的数据库不同,A部门要将数据放test数据库,B 部门数据 要放在test1数据库 同一个项目 需要整合 多个数据源 上传个demo 方便自己以后回看!!!!!!!! ...

  3. Java学习——上转型与下转型对象

    上转型:重写父类方法才调用子类方法,其他仍用父类的,包括被子类隐藏的父类成员变量,而且不能调用子类新增的成员变量和成员方法. 下转型:只能是转上去的才能转下去.下转型类似于该子类直接继承父类. pac ...

  4. 远程服务器安装mysql数据库

    https://www.cnblogs.com/renjidong/p/7047396.html 1.新开的云服务器,需要检测系统是否自带安装mysql # yum list installed | ...

  5. 开启BBR加速

    在linux里用 wget --no-check-certificate https://github.com/teddysun/across/raw/master/bbr.sh chmod +x b ...

  6. 多款Android播放器源码集锦

    原帖地址:http://blog.csdn.net/jingwen3699/article/details/7765804/

  7. android 将项目下的数据库拷贝到sd卡中

    /** * 将项目下的数据库拷贝到sd卡中 */ public static boolean copyDbToSdCard() { FileInputStream fis = null; FileOu ...

  8. 0000 - Spring 中常用注解原理剖析

    1.概述 Spring 框架核心组件之一是 IOC,IOC 则管理 Bean 的创建和 Bean 之间的依赖注入,对于 Bean 的创建可以通过在 XML 里面使用 <bean/> 标签来 ...

  9. Redis进阶实践之十四 Redis-cli命令行工具使用详解

    转载来源:http://www.cnblogs.com/PatrickLiu/p/8508975.html 一.介绍 redis学了有一段时间了,以前都是看视频,看教程,很少看官方的东西.现在redi ...

  10. CentOS安装nginx以及负载均衡的搭建

    依赖环境,没有安装的需要安装一下 yum install gcc yum install pcre-devel yum install zlib zlib-devel yum install open ...