程序阅读

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);
        }
      }
    }
  }
}

上述代码用C#编写

问题1:这个程序要找的是符合什么条件的数?

答:程序寻找一个数i,i属于1~2^63-1(9223372036854775807)之间,而i的要求是在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]中存在两个数a和b,a与b相邻,即b=a+1。要求i不能被a和b整除,却可以被数组中另外28个数整除。

问题2:这样的数存在吗?符合这一条件的最小的数是什么?

答:这样的数存在。首先寻找a和b:将数组中的30个数从15处分开,2~15可以在数组中寻找到其2倍的数,所以排除。在剩下的数中,取偶数:16,18,20,22,24,26,28,30,除16因其质因数的特殊性待定外均可排除。而大于16的奇数:17,19,21,23,25,27,29,31,除27的质因数拥有特殊性可以待定,其余数包括其中的质数均可排除。现在有16,27两个数待定,而27左右的数分别为26和28, 都已排除。所以寻找到的a=16,b=17。所以i=(31*29*23*19*13*11*7*5*3*2)*4*9,括号内为i的质因子,要满足需求,还需4和9两个数。上述等式中任意两个数的积都可在数组除a,b以外的28个数中找到。所以最小的i=424716332040。

问题3:在电脑上运行这一程序,你估计多长时间才能输出第一个结果?时间精确到分钟。

答:运行程序所需时间太长,无法计算。

问题4:在多核电脑上如何提高这一程序的运行效率?

答:关闭其他应用程序,减少CPU占用率。

时间: 2024-07-30 23:55:28

程序阅读的相关文章

程序阅读:简单C++学生信息管理系统

课程首页在:http://blog.csdn.net/sxhelijian/article/details/11890759,内有完整教学方案及资源链接 [程序阅读]阅读并执行以下的程序,找出当中出现构造函数.友元函数.运算符重载.静态数成员语法现象出现的位置.细致体会其使用方法,在以后的设计中可以灵活应用有关方法和技巧 #include <iostream> #include <string.h> using namespace std; #define MAX 100 clas

程序阅读题

阅读下面程序,请回答如下问题: 问题1:这个程序要找的是符合什么条件的数? 问题2:这样的数存在么?符合这一条件的最小的数是什么? 问题3:在电脑上运行这一程序,你估计多长时间才能输出第一个结果?时间精确到分钟(电脑:单核CPU 4.0G Hz,内存和硬盘等资源充足). 问题4:在多核电脑上如何提高这一程序的运行效率? (注:该程序.用C#语言编写,但是只要有C语言基础完全没有阅读压力,如果对部分语句不懂请自行查询) 将上述问题结果写到博客上,截止时间本周日(3月19日)晚8时 using Sy

15周《C++语言基础》程序阅读——二进制文件及文件的随机读写(1)

1.阅读并运行下面的两个程序,分别用记事本和二进制文件阅读器(请自行下载Binary Viewer等程序,或者用DOS中的Debug程序,并百度其用法).查看其内容,并理解文件存储的原理. (1) <code class="hljs cpp has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Cod

第15周 程序阅读-二进制文件及文件的读取1

1.阅读并运行下面的两个程序,分别用记事本和二进制文件阅读器(请自行下载Binary Viewer等程序,或者用DOS中的Debug程序,并百度其用法).查看其内容,并理解文件存储的原理. (1) #include <iostream> #include <fstream> #include <cstdlib> using namespace std; int main( ) { int a; ofstream outfile("f1.dat",io

第15周 程序阅读-二进制及二进制文件的读取3

3.阅读下面的程序,指出其功能,体会seekg().tellg()等函数的功能及其用法 (1) #include<iostream> #include <fstream> using namespace std; const char * filename = "a.txt"; int main () { long l,m; ifstream file (filename, ios::in|ios::binary); l = file.tellg(); file

第13周 《C++语言基础》程序阅读——多态性与抽象类 (3)

1.阅读下面的程序,并写出运行结果 (3)纯虚函数 #include <iostream> using namespace std; class Base { public: virtual void Who() =0; }; class FirstDerived:public Base { public: void Who() { cout<<"F"; } }; class SecondDerived:public Base { public: void Wh

第12周 《C++语言基础》程序阅读——多重继承(1)

问题描述: (1)阅读程序,写出执行结果 #include <iostream> using namespace std; class A { public: A() { a=0; } A (int i) { a=i; } void print() { cout<<a<<" "; } private: int a; }; class B: public A { public: B() { b=0; } B(int i, int j, int k):

第12周 《C++语言基础》程序阅读——多重继承(2)

问题描述: (2)阅读程序,写出执行结果 #include <iostream> using namespace std; class A { public: A(char *s) { cout<<s<<endl; } }; class B:public A { public: B(char *s1, char *s2):A(s1) { cout<<s2<<endl; } }; class C:public A { public: C(char

第12周 《C++语言基础》程序阅读——多重继承(3)

问题描述: (2)阅读程序,写出执行结果 #include <iostream> using namespace std; class Base { public: Base(char i) { cout<<"Base constructor. --"<<i<<endl; } }; class Derived1:virtual public Base { public: Derived1(char i,char j):Base(i) {

第12周 《C++语言基础》程序阅读——多重继承(4)

问题描述: (4)阅读程序,写出执行结果,并回答问题     #include<iostream> using namespace std; class A { public: int n; }; class B:public A {}; // class B:virtual public A{}; class C:public A {}; // class C:virtual public A{}; class D:public B,public C { public: int getn()