C # code Analysis

Source: Internet
Author: User

As a computer professional student, code analysis is a very common, and need to do often things.

When you just take over a program code, a lesson set, or a project, you're often crazy about not finding ideas. And the network is the best tool, look at many it People's technical blog site, often collect the strengths of others code, you will get extraordinary results. The following code is written by C #, not very familiar with this language, but with the C language Foundation, should not be particularly difficult.

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

Now the code is decomposed: basically it looks like a number-finding operation, which is controlled by two for loop nesting, for (Int64 i = 1; i < int64.maxvalue; i++)
First Int64.maxvalue How big is this number? Find the answer on the Internet, it should be long shape can represent the maximum value, the range is 2147483648 to 2147483647, that should be a very large number, in such a large range in order to find a few numbers, it seems not very easy ...

Look at the small loop again, for (int j = 0; (J < RG. Length) && (hit <=2); J + +), rg[] is an internal containing an integer composed of 2~31, a total of 30 numbers, rg.length=30.

What kind of a number would be the number to look for?

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

Hit==2&&hit1+1==hit2 satisfies this condition to output, look at the judging condition if ((i% rg[j])! = 0), that is, to judge from the 1~maxlalue the number that cannot be divisible, hit1 is the first subscript not divisible by the number, The Hit2 is the second subscript that cannot be divisible by a number. Requirements Hit1+1==hit2, that is to say that the two numbers to be continuous, and the second condition is hit==2,for (int j = 0; (J < RG. Length) && (hit <=2); J + +), Hit>2 will exit the small loop hit is also equal to 2, that is, only two can not be divisible by the number, and to be continuous.

Can only say this number is really not easy to find ...

C # code 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.