Do you want to add an algorithm from 1 to 100? Then, how can I add m to n?

Source: Internet
Author: User

When I was watching a video tutorial today, I heard "Yang zhongke" say that there are many large companies who often ask some basic things during the interview, I often ask you some questions that are simple to "abnormal". For web development, I suddenly asked you some algorithm questions. Maybe many people were blank at the time; teacher Yang said that many programmers cannot write algorithms from 1 to 100. Today, I use a mathematical formula to calculate this question ~

First, you need to know the number formula from 1 to 100; N * (n-1)/2 + N; everyone knows that adding from 1 to 100 is equal to 5050. Use this formula to set it, wait for not equal to 5050;

N is the number from 1 to the nth digit; 100 * (100-1) = 9900; 9900/2 = 4950; 4950 + 100 = 5050; it seems that this formula is okay; so when you don't use the for loop to write, you can use this formula;

 

Code from 1 to n:

 

Using system;
Using system. Collections. Generic;
Using system. text;

Namespace consoleapplication1
{
Public class Program
{
Static void main (string [] ARGs)
{
Int n, m, result;
String chars = "Y ";
Bool test;
Do
{
Console. writeline ("================== sum from 1 to n ==================" );
Console. writeline ("enter a number! ");
Test = int. tryparse (console. Readline (), Out N );
If (test = true)
{
Result = N * (N-1)/2 + N;
Console. writeline ("sum: {0}", result );
Console. Readline ();
}
Else
{
Console. writeline ("Please do not enter invalid characters! ");
Chars = console. Readline ();
}
} While (chars = "N ");

}
}
}

Running result:

Now we know the formula from 1 to n. What is the formula for adding m to n?

Let's take a look. Let's use the calculator provided by the computer from 5 to 10 to see how much it is equal (a good math can be considered); the result is equal to 45;

Let's use this formula to try: (N * (N-1)/2 + n)-(m-1) * (m-2) /2 + m-1 );

 

 

Thank you for the formula: (m + n) * (N-m + 1)/2, (first item + last item) × number of items/2. You can also try this method! It is simpler and clearer than the formula I wrote; 14 floor: recursive Writing of... Shen...; private State int add (INT m)
{
If (M <1)
Return 0;
If (M = 1)
Return 1;
If (M> 1)
Return add (m) + Add m-1 );
}

 

M is the first number, and N is the number to be added. Once we see so many parentheses, it may be a bit confusing. But it doesn't matter. We should remove the parentheses first, if we divide it into two parts, there will be more clear;

 

The first part is still the same as the formula for adding 1 to n: N * (n-1)/2 + N; the second part is (m-1) * (m-1-1) /2 + m-1;

Why does it need "S-1? How many numbers do I have before 5 to 10 or 5? Four, right? (1, 2, 3, 4); therefore, we want to calculate the numbers and values above. I don't know if everyone is dizzy here. (I am not very clear about it, understanding it slowly)

Using the formula in the first part, the formula in the second part is the result we want;

Let's try again;

(10*(10-1)/2 + 10)-(5-1) * (5-2)/2 + 5-1) = 45;

 

The following is the code:

 

Using system;
Using system. Collections. Generic;
Using system. text;

Namespace consoleapplication1
{
Public class Program
{
Static void main (string [] ARGs)
{
Int n, m, result;
String chars = "Y ";
Bool test;
Do
{

Console. writeline ("sum from N to M ");
Console. Write ("Enter the first number :");
Test = int. tryparse (console. Readline (), out M );
If (test = true)
{
// If it is a number, we do not operate anything, because there is another number that is not input;
}
Else
{
Console. writeline ("Please do not enter invalid characters! ");
}
Console. Write ("enter the second number :");
Test = int. tryparse (console. Readline (), Out N );
If (test = true)
{
Result = (N * (N-1)/2 + n)-(m-1) * (m-2)/2 + m-1 );
Console. writeline ("sum from {0} to {1}: {2}", M, N, result );
Console. Readline ();
}
Else
{
Console. writeline ("Please do not enter invalid characters! ");
}
} While (chars = "N ");

}
}
}

Running result:

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.