Projectelead (question 1 and 2)

Source: Internet
Author: User

Question 1: Find the sum of all numbers whose values can be 3 or 5 in 1000.

The first reaction is to cyclically determine whether it can be divisible by 3 or 5, and add it to a variable if it can,CodeAs follows:

 
Static void p1_1 () {int max = 1000; int sum = 0; For (INT I = 1; I <Max; I ++) {if (I % 3 = 0 | I % 5 = 0) {sum + = I ;}} console. writeline (SUM );}

Considering that the % modulo operation consumes more performance, we can find another method. In fact, it is to add all the multiples of 3 and the multiples of 5.

The Code is as follows:

Static void p1_2 () {int max = 1000; int sum = 0; For (INT I = 3; I <Max; I ++ = 3) {sum + = I ;} for (INT I = 5; I <Max; I + = 5) {sum + = I ;}for (INT I = 15; I <Max; I ++ = 15) {sum-= I;} console. writeline (SUM );}

After testing, when calculating the results within 1000000, method 2 is nearly 10 times faster than method 1. The larger the data volume, the more obvious the gap.

Of course, the most challengingAlgorithmIs the sum of the arithmetic difference series, 3 + 6 + 9 + .... + 999 + 5 + 10 + 15 + .... + 995-15-30-....-990

I browsed the official answers and comments from foreigners. There is no special solution.

The final answer is 233168

 

Question 2: calculate the total number of all even numbers in the Fibonacci series of no more than 4 million.

The Fibonacci series is 1 2 3 5 8 13 21 34 55 89 144 ..... Each item is equal to the sum of the first two items.

The first thing that comes to mind is to use recursion to store all values in the array and accumulate the even numbers into a variable. The Code is as follows:

Static void p2_1 () {int sum = 0; List <int> List = new list <int> (20000); list. add (1); list. add (2); sum + = 2; while (true) {// Add the last two items at a time. Int n = list [list. count-1] + list [list. count-2]; // an endless loop exceeded if (n> 4 million) {break;} List. add (n); // if it is an even number, it will accumulate if (N % 2 = 0) {sum + = n ;}} console. writeline (SUM );}

The first solution officially provided does not use arrays. It only stores the last two items and improves the efficiency a lot. The Code is as follows:

Static void p2_2 () {int sum = 0; int A = 1, B = 2; sum + = 2; while (B <4000000) {int c = A + B; A = B; B = C; If (C % 2 = 0) {sum + = C ;}} console. writeline (SUM );}

An algorithm proposed by a colleague is: Observe the sequence to obtain

1 1 2 3 5 8 13 21 34 55 89 144

Odd

Except for 1 and 2, the following numbers are odd and even, which is not hard to prove, because odd + even = odd, odd + ODD + even

So the sum of all the even numbers after 3 is equal to the sum of all the numbers after 3 (the last number is also an even number) divided by 2

For example: 8 + 34 + 144 = (3 + 5 + 8 + 13 + 21 + 34 + 55 + 89 + 144)/2

Combined with method 2, you can save the modulo operation in the loop to improve efficiency. The Code is as follows:

Static void p2_3 () {int sum = 0; int A = 1, B = 2; while (B <4000000) {int c = a + B; A = B; B = C; sum + = C;} // if the number of the last 2 is an odd number, remove if (a % 2 = 1) {sum-= ;} if (B % 2 = 1) {sum-= B;} // After dividing by 2, add 2 sum = sum/2 + 2; console. writeline (SUM );}

Through method 3, we know the law of odd/even odd parity. We regard all the even numbers as a new series and look for its recursive formula.

2 8 34 144... These are the 1st, and 10 items of the original series (starting from 0 ),

Column A (N), A (n) = a (n-1) + a (n-2)

Then the new series B (n) = a (3N + 1) = a (3N) + a (3n-1) = a (3n-1) + a (3N-2) + a (3N-2) + a (3n-3) =

A (3N-2) + a (3n-3) + 2a (3N-2) + a (3n-4) + a (3n-5) =

3A (3N-2) + a (3n-3) + a (3n-4) + a (3n-5) =

4A (3N-2) + a (3n-5) =

4A (3 (n-1) + 1) + a (3 (n-2) + 1) =

4B (n-1) + B (n-2)

The formula comes out, and the following is the addition of loops. The Code is as follows:

 
Static void p2_4 () {int sum = 0; int A = 2, B = 8; sum = A; while (B <4000000) {sum + = B; int c = 4 * B + A; A = B; B = C;} console. writeline (SUM );}

Of course, there is a general formula for the Fibonacci series. Of course, this series can also work out the general formula, and finally calculate the sum formula. This is the best way to go against the day.

The general formula is as follows:

B (n) = 4b (n-1) + B (n-2)

It takes about half an hour to complete. It's crazy !!!!! I really don't know how to calculate the sum formula. Forget it !!

The answer to this question is4613732

 

 

 

 

 

 

 

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.