Interview Questions: the rules for a column are as follows: 1, 1, 2, 3, 5, 8, 13, 21, 34 ...... Calculate the number of 30th digits and use recursive algorithms (C #)

Source: Internet
Author: User

Interview Questions: The rules for one column are as follows:: 1,1,2,3,5,8,13,21,34 ......Find30What is the number of digits, RecursionAlgorithmImplementation (C #).

 

After I got out of a software company that has been working for more than four years (wangxin software), I have been interviewing these days, but I have not found a suitable one. Seeing these basic interview questions is a little difficult to do. I haven't been reading these basic questions for a long time. It's a little unfamiliar.

From the rule of the series, we can see that, starting from the third digit, the value is equal to the sum of the first two numbers30The number of digits.28Bitwise AND29The sum of BITs, and29The number of digits is27Bitwise AND28The sum of digits. At that time, according to this idea, the code was not implemented on the exam.

When I go home, I search online. Most of the answers are similar:

Class Program

{

Static VoidMain (String[] ARGs)

{

Console.Writeline (String.Format ("{0 }", Foo (50)));

Console.Read ();

}

 

Public Static LongFoo (IntI)

{

LongResult= 0;

If(I<= 0)

Return 0;

Else If(I> 0 &&I<= 2)

Return 1;

Else

Result=Foo (I- 1)+Foo (I- 2);

 

// Console. Write (string. Format ("{0} \ t", result ));

 

ReturnResult;

}

}

SuchProgramExtremely low performance, display50The result is basically not displayed, and developers are completely misled. To this end, I will implement my ideas as programs, programsCodeAs follows:

Class Program

{

Static VoidMain (String[] ARGs)

{

Console.Writeline (String.Format ("{0 }", Getresult (91)));

Console.Read ();

}

Public Static LongGetresult (IntNumber)

{

If(Number<= 2)

Return 1;

ReturnGetresult (1,1,3, Number );

}

 

Private Static LongGetresult (LongLeft,LongRight,IntCount,IntNumber)

{

LongResult=Left+Right;

Console.Write (String.Format ("{0} \ t", Result ));

//Obtain the number of instances.

If(Count<Number)

{

Count=Count+ 1;

//Data transfer

Left=Right;

Right=Result;

//Recursive call

Result=Getresult (left, right, count, number );

}

ReturnResult;

}

}

For data and machine reasons, we can see the 91st-bit computing result, and the subsequent display is not normal. Is there any better way to calculate more digits? Thank you!

Correction is not recommended!

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.