软件工程第三周作业(二)

程序代码:

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.   这个程序找的是在rg数组中只有连续的两个数不能整除,其余都能整除的符合这样条件的数。

2.   存在;最小为数组中的数除16和17外的最小公倍数2123581660200 .

3.   外循环执行了2123581660200次,执行一次外循环内循环执行大约12次,参考电脑配置,执行一次%用80个周期,所以执行时间约为:2123581660200*12*80/(4*10^9*60)=8500min 。

4.   同时对n个i进行操作,大大提高程序执行速度。

注:一开始认为不能被整除的只有连续的两个质数2,3但如果这样这个数就不会存在。经过与同学的交流得到思路。连续的两个数只能是16和17。(程序执行时间参考了薛鹏飞的博客)

posted on 2016-03-19 17:01  王新亮shuai  阅读(148)  评论(1编辑  收藏  举报

导航