阅读程序 回答问题——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 =
          {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};
      for (Int64 i = 1; i < Int64.MaxValue; i++)
      {
        int hit = 0;
        int hit1 = -1;
        int hit2 = -1;
        for (int j = 0; (j < rg.Length) && (hit <=2) ; j++)
        {
          if ((i % rg[j]) != 0)
          {
            hit++;
            if (hit == 1)
            {
              hit1 = j;
            }
            else if (hit == 2)
            {
              hit2 = j;
            }
            else
              break;
          }
        }
        if ((hit == 2)&& (hit1+1==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

 

posted on 2016-03-21 12:16  Mr.Winter  阅读(648)  评论(4编辑  收藏  举报