C # Program Analysis

Source: Internet
Author: User

I. Procedures and ISSUES

To read the following procedure, please answer the following questions:

Question 1: What are the criteria for this program?

Question 2: Does such a number exist? What is the minimum number that meets this condition?

Question 3: How long do you expect to be able to output the first result when running this program on a computer? The time is accurate to minutes (computer: Single core CPU 4.0G Hz, memory and hard disk resources are sufficient).

Question 4: How can I improve the efficiency of this program on multi-core computers?

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

Second, the procedure analysis

First of all this is a C # program, a look, a bit like Java and a bit like C, look closely, and really like the teacher said, at least it seems to read no obstacles.

In short, this is a small program to find a number, the main method is nested within the two for loop, for (Int64 i = 1; i < int64.maxvalue; i++). Don't know what Int64.maxvalue stands for? On the Baidu find to get as follows: Int32 is a structure MaxValue is a property value in Int32, the type of the property value is a const int, the value of the property is the 0x7fffffff int, not the Int32, int is the basic data type, which represents shaping. The exact number of bytes (or bit), and the current system, is not what you understand, fixed 32 bits. It could be 32, it could be 64. So, int64, is equivalent to a C language in a long shape data type, the approximate range is -2^63-2^62 between, good freshman number, in this range to find a number, exactly what number?

Next look at the second loop, for (int j = 0; (J < RG. Length) && (hit <=2); J + +), from the above program can be seen rg.length=30, such 30 integers, through the second for loop operation, and then see the following program:

if ((i% rg[j])! = 0)
{
hit++;
if (hit = = 1)
{
Hit1 = j;
}
else if (hit = = 2)
{
Hit2 = j;
}
Else
Break
}

In this case, hit is used to record several divisions, Hit2 is used to record the number cannot be divisible by the previous one, Hit2 is the latter.

After this two cycles, the following actions are performed:

if (hit = = 2) && (HIT1+1==HIT2))
{
Console.WriteLine ("Found {0}", i);
}

So the function of the program is to find out 2-31 of these 30 numbers, cannot be divisible by the preceding two consecutive numbers, can be divisible by the third number of an integer, and then print it.

An instant reminds of the C language overflow problem, but the scope set here is very large, there should be no overflow. But the program in the run for a long time has not come out of the results, and then I would like to be able to written calculation out the results. However, it has overestimated its ability and underestimated the computational capacity of the program. Written calculation is not out, there may be my Computer configuration is not enough, perhaps I have not enough time, in short, not run out of such a number, this number may not be.

Since there is no proper program run time function, the run time is unknown. Here is my Computer configuration.

For how to improve the speed of multi-core computers, you should reset the Task Manager aspect, for the principle I do not understand, therefore, and can not be very sure to say the method.

C # Program Analysis

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.