Euler's plan (1 ~ 3) PS: Be sure to read questions carefully in the future

Source: Internet
Author: User

That day's question is quite simple

Next let's take a look

  No1

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.

Find the sum of all the multiples of 3 or 5 below 1000.

//project euler num1#include <stdio.h>#include <stdlib.h>#include <string.h>int main(){                                                          int sum = 0;    int i;    for(i = 0; i < 1000; i++)    {           if(i % 3 == 0 || i % 5 == 0)            sum += i;    }       printf("The sum is %d\n", sum);}

The first question is very simple. Don't explain it ~

  No 2

Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:

1, 2, 3, 5, 8, 13, 21, 34, 55, 89 ,...

By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms

The second question is to find the sum of the even values of the Fibonacci series smaller than 4e6. It is easy to think of recursive algorithms.

  

// Project Euler pro02 # include <iostream> # include <string> # include <vector> using namespace STD; int fib_temp [10000]; // avoid repeated operations, the calculated items are saved to the array int fib (int n) {If (n = 1) {fig [0] = 1; return fig [0];} else if (n = 0) {fig [1] = 2; return fig [1];} else {If (Fig [n-1]! = 0) {If (Fig [n-2]! = 0) return maid [n-1] + maid [n-2]; // if it has been pre-stored, directly return else maid [n-2] = fib (n-2); Return maid [n-1] + maid [n-2];} else {maid [n-1] = fib (n-1); maid [n-2] = fib (n-2 ); return maid [n-1] + maid [n-2] ;}}} int sum_even_fib (INT top_num) {int I = 0; int sum = 0; int temp = 0; while (1) {if (I % 2 = 0) {If (temp = fib (I) <top_num) sum + = temp; else break ;} I ++;} return sum;} int main () {int sum = sum_even_fib (400000000); cout <sum <Endl; return 0 ;}

In this case, the most basic recursive method is not used because the efficiency is too low. It is better to store the calculated values to the array first to avoid repeated computation.

However, this reminds me of the previous dynamic planning algorithm:

The recursive algorithm is very simple from top to bottom. It can be seen from N step by step to the first item;

But dynamic planning is just the opposite. It starts from the first item, and then stores the calculated result into an array for later use.

1 // project Euler pro02 2 # include <iostream> 3 # include <string> 4 # include <vector> 5 using namespace STD; 6 7 int fib_temp [10000]; 8 // set the memory storage group 9 int fib (int n) 10 {11 if (n = 0 | n = 1) 12 {13 if (maid [0] = 0) 14 maid [0] = 1; 15 if (maid [1] = 0) 16 maid [1] = 2; 17 // initialize 18} 19 else 20 {21 for (INT I = 2; I <= N; I ++) 22 {23 if (maid [I] = 0) 24 maid [I] = maid [I-1] + maid [I-2]; 25 // use the following formula to calculate the response 26} 27} 28 return fig [N]; 29 // directly return bytes 30} 31 32 int sum_even_fib (INT top_num) 33 {34 int I = 0; 35 int sum = 0; 36 int temp = 0; 37 while (1) 38 {39 if (temp = fib (I) % 2 = 0) 40 {41 if (temp <top_num) 42 sum + = temp; 43 else 44 break; 45} 46 cout <fib (I) <Endl; 47 I ++; 48} 49 return sum; 50} 51 int main () 52 {53 int sum = sum_even_fib (4e6); 54 cout <sum <Endl; 55 return 0; 56}

NO3

The prime factors of 13195 are 5, 7, 13 and 29.

What is the largest prime factor of the number 600851475143?

I will say this is what I did not see clearly. I think it is to find all prime numbers smaller than this number ~

However, the question is to find the maximum prime factor smaller than this number.

Sorrow ~~

Well, the two are done. Let's first look at the calculation of the maximum prime factor.

# Include <iostream> 2 # include <string> 3 # include <vector> 4 # include <math. h> 5 6 using namespace STD; 7 8 9 bool is_prime (long int I) 10 {11 long Int J; 12 for (j = 2; j <= SQRT (I); j ++) 13 {14 if (I % J = 0) 15 return false; 16} 17 if (j> SQRT (I )) 18 return true; 19} 20 // This is 21 22 void max_prime_facter (long int N) 23 {24 if (is_prime (n )) 25 {26 cout <n <Endl; 27 return; 28 // if n itself is a prime number, 29} 30 for (long int I = 2; I <(n/2); ++ I) 31 {32 If (N % I = 0) 33 {34 N = N/I; 35 // if a small factor is found, replace N with N/I 36 cout <"factor is" <I <Endl; 37 I = 2; 38 // reset the cyclic variable 39 if (is_prime (N) 40 {41 cout <n <Endl; 42 // if n is changed to a prime number during the process, the maximum prime factor 43 break; 44} 45} 46} 47} 48 49 int main (INT argc, const char * argv []) is obtained. 50 {51 long int n = 600851475143; 52 max_prime_facter (n); 53 return 0; 54}

View ~ It's not hard.

The problem is that the excavator is really strong !!

What if I want to output all prime numbers smaller than this number?

1 # include <iostream> 2 # include <string> 3 # include <vector> 4 # include <stdio. h> 5 # include <stdlib. h> 6 # include <string. h> 7 # include <math. h> 8 using namespace STD; 9 10 bool is_prime (long int I) 11 {12 for (long Int J = 2; j <= SQRT (I ); j ++) 13 {14 if (I % J = 0) 15 return false; 16} 17 if (I> SQRT (I) 18 return true; 19} 20 // function for determining prime numbers 21 vector <int> vec_prime; 22 // an array containing prime numbers 23 Void prime_number (long int N) 24 {25 long int Max; 26 for (INT I = 2; I <n; I ++) 27 {28 If (vec_prime.size ()! = 0) 29 // 30 {31 vector with no elements in the first array <int>: iterator it; 32 for (IT = vec_prime.begin (); it! = VEC _ Prime. end (); ++ it) 33 {34 if (I % (* It) = 0) 35 break; 36 // judge whether the array has its factor 37} 38 If (it! = Vec_prime.end () 39 continue; 40 // This indicates that there is another prime factor 41} 42 if (is_prime (I) = true) 43 // here, it indicates that there is no factor 44 for this number in the array. // we know that all positive integers can represent the product of a prime number. 45 // otherwise, if this number cannot be expressed as the product of the prime number 46 //, then this number itself is probably a prime number 47 // so we can determine whether it is a prime number, if yes, add the array 48 {49 vec_prime.push_back (I); 50 cout <I <Endl; 51 // output the prime number 52} 53} 54 return in sequence; 55} 56 57 int main () 58 {59 60 long int n = 600851475143; 61 prime_number (n); 62 Return 0; 63}

We can see that this algorithm is actually very fast ~

Finally, let's talk about it:

Today I learned two new data types in C ++: long int and _ int64.

References:

Http://www.cnblogs.com/jiai/articles/2613900.html

Http://www.cnblogs.com/felove2013/articles/3880590.html

Euler's plan (1 ~ 3) PS: Be sure to read questions carefully in the future

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.