Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Brian Powell Project Euler 1. Multiples of 3 and 5 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. A key result needed to solve this problem is the sum of all the integers between 1 and some number N ; it was derived by Gauss when he was in elementary school: N X i= i=1 N (N + 1) . 2 (1) Suppose x is a multiple of 3 and that y is a multiple of 5. Then, we can write x = 3n and y = 5m for some positive integers n and m. From Equation (1) we can construct the sum of the multiples of 3 less than 1000 as follows: 333 X 3 × 333 × 334 3n = ; (2) 2 n=1 likewise, the sum of the multiples of 5 less than 1000 is 199 X 5m = m=1 5 × 199 × 200 . 2 (3) Now, if we just add these two sums we won’t arrive at the correct answer – it will be much too large. Can you see why? Consider the number 15: it is a multiple of both 3 and 5. If we simply add Equations (2) and (3) we will count these numbers that are multiples of both 3 and 5 twice! So how do we correct for this? A number z that is a multiple of both 3 and 5 can be written z = xy = (3 × 5)n × m = 15k for some positive integer k. These are the numbers that are counted twice when we sum Equations (2) and (3) and so we need to subtract them off to arrive at our answer: 333 X n=1 3n + 199 X 5m − m=1 66 X 15k = 233 168. (4) k=1 2. Even Fibonacci numbers 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. Notice first that every 3rd Fibonacci number is even (and no others). Next, observe the following: F6 = 8 = 4×2 F9 = 34 = 4×8+2 F12 = 144 = 4 × 34 + 8 F15 = 610 = .. . 4 × 144 + 34 By playing around with ways of writing the even Fibonnaci numbers, we discover an interesting pattern that they all seem to follow, F3i = 4 × F3i−3 + F3i−6 . (5) 2. Even Fibonacci numbers continued on next page. . . Page 1 of 12 Brian Powell Project Euler 2. Even Fibonacci numbers (continued) Consider the sum, S, of the first i even Fibonacci numbers, S = F3i + F3i−3 + · · · + 34 + 8 + 2. (6) This is what we’re after. Proceed by rewriting the sum by replacing each term with its recursive definition, Eq. (5), i.e., write S = (4 × F3i−3 + F3i−6 ) + (4 × F3i−6 + F3i−9 ) + (4 × F3i−9 + F3i−12 ) + · · · (7) Next, regroup terms: S = (4 × F3i−3 + 4 × F3i−6 + 4 × F3i−9 + · · · ) + (F3i−6 + F3i−9 + F3i−12 + · · · ) = 4(S − F3i ) + (S − F3i − F3i−3 ) (8) This gives an equation involving only two Fibonacci numbers – all the rest have been nicely absorbed into the two S’s on the right hand side. This means we can solve for S to obtain a simple, closed-form expression for the sum of the first i even Fibonacci numbers, S = F3i + 1 (F3i + F3i−3 ) . 4 (9) In order to answer the question: what is the sum of the even Fibonacci numbers less than 4 million, we need only know the two greatest even Fibonacci numbers less than 4 million. A quick search reveals these to be F33 = 3 524 578 and F30 = 832 040. Plugging into Eq. (9) gives S = 4 613 732. (10) 3. Largest prime factor The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143? The fundamental theorem of arithmetic guarantees the unique factorization of any integer into primes. Factorization is a computational problem with major applications to modern cryptology: while no analytic procedure exists, sophisticated numerical approaches have been developed in recent years. We will not be employing any of these! Below is a simple C code that uses trial division to factor integers into primes; the largest such factor can then be read off. Listing 1: Prime Factorization #include < s t d i o . h> #include <math . h> #include < s t d l i b . h> 5 /* * Generates a list of prime factors of input integer. * Returns nothing if input is prime. */ 10 int main ( int a r g c , char ∗ a r g v [ ] ) { long long n = a t o l l ( a r g v [ 1 ] ) ; int i , j , n o p r i m e = 0 ; 3. Largest prime factor continued on next page. . . Page 2 of 12 Brian Powell 15 20 25 30 35 40 Project Euler 3. Largest prime factor (continued) int c o u n t = 1 ; int k = 0 ; long long p a r t = 1 ; FILE ∗ o u t ; o u t = f o p e n ( ” o u t f a c . d a t ” , ”w” ) ; /* Start trial divisions of each prime. * First, check the number 2 separately*/ while ( 1 ) { if ( ( n%((long long ) pow ( 2 , c o u n t ++)))==0) // 2 divides n { f p r i n t f ( s t d e r r , ” 2\ n” ) ; p a r t = 2∗ p a r t ; if ( p a r t==n ) { f c l o s e ( out ) ; return 0 ; // we’re done } } else { break ; } } for ( i = 3 ; i < n ; i = i +2) // test only the odds { // First, check if the number is prime 45 50 55 60 65 noprime = 0 ; for ( j = 3 ; j <= ( long long ) c e i l ( s q r t ( i ) ) ; j=j +2) { if ( ( i%j == 0 ) ) { noprime = 1 ; break ; // not prime } } if ( ! n o p r i m e ) // if prime, attempt trial division { count = 1; while ( 1 ) { if ( ( n%((long long ) pow ( i , c o u n t ++)))==0) // prime i divides n { f p r i n t f ( s t d e r r , ”%d\n” , i ) ; part = part ∗ i ; if ( p a r t==n ) { f c l o s e ( out ) ; 3. Largest prime factor continued on next page. . . Page 3 of 12 Brian Powell Project Euler 3. Largest prime factor (continued) return 0 ; // we’re done } } else { 70 break ; } } } 75 } } Since all primes aside from 2 are odd, divisibility of the input integer, n, by 2 is tested first, separately. If n is divisible by 2, a second trial division by 2 is attempted on n1 = n/2, and so on until nk = n/2k is no longer divisible by 2. We next move on to test the odd primes. The code does not “know” a priori what the prime numbers are (besides 2, a small concession we’ve made to improve the efficiency of√the algorithm). Starting with 3, we check each odd integer i for primality by trial divisions of integers j < i.1 If i = pi is prime, we check if it is ki−1 ki−1 ki a factor of n by trial division into n/2k · pk22 · · · · pi−1 , repeating until pi fails to divide n/2k · pk22 · · · · pi−1 pi . √ This process is continued for all primes i < n. Applying this algorithm to 600851475143, we find the prime factors 71, 839, 1471, and 6857. And so, the largest prime factor of 600851475143 is 6857. (11) 4. Largest palindrome product A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99. Find the largest palindrome made from the product of two 3-digit numbers. This is exercise is another computational problem. The product of two 3-digit numbers is either 5- or 6-digits. The code is easy: the interesting part is figuring out how to access the individual digits of 5- and 6-digit numbers to check for palindromicity (is that a word?) Consider 5-digit numbers in the form n = abcde. The first digit a can be gotten as follows: divide n by 104 so that n/104 = a.bcde. By making use of the floor function in C, we can round this down to the nearest integer: floor(n/104 ) = a. The second digit, b, can be gotten similarly but with an additional step. Consider floor(n/103 ) =floor(ab.cde) = ab. Then, from above, 10× floor(n/104 ) = a0 and we can take the difference ab − a0 = b. The remaining digits can be gotten using similar maneuvers. Listing 2: Palindrome Generator #include < s t d i o . h> #include <math . h> 5 int main ( ) { 1 There are of course more time-efficient ways of finding prime numbers; however, the simplest of such methods – the Sieve of Eratosthenes – is difficult to implement for large-ish primes. The problem is that the sieve requires an array of size equal to the prime number, and memory allocation fails on typical computers for array sizes surpassing several hundred thousand elements. 4. Largest palindrome product continued on next page. . . Page 4 of 12 Brian Powell Project Euler 4. Largest palindrome product (continued) int i , j ; int a , b , c , d , e , f ; int l a r g e s t = 1 ; FILE ∗ o u t ; 10 o u t = f o p e n ( ” p a l i n d r o m e . o u t ” , ”w” ) ; for ( i =100; i < 9 9 9 ; i ++) { for ( j = i ; j < 9 9 9 ; j ++) // j=i so we don’t overcount { if ( i ∗ j /1 e5 < 1 ) // if we’ve got a 5-digit number { a = ( int ) f l o o r ( i ∗ j /1 e4 ) ; e = ( int ) ( i ∗ j − 10∗ f l o o r ( i ∗ j / 1 0 ) ) ; b = ( int ) ( f l o o r ( i ∗ j /1 e3 )−10∗ f l o o r ( i ∗ j /1 e4 ) ) ; d = ( int ) ( f l o o r ( i ∗ j /10) −10∗ f l o o r ( i ∗ j / 1 0 0 ) ) ; 15 20 if ( ( a==e )&&(b==d ) ) { f p r i n t f ( out , ”%d %d %d\n” , i , j , i ∗ j ) ; } 25 } else // we’ve got a 6-digit number { a = ( int ) f l o o r ( i ∗ j /1 e5 ) ; f = ( int ) ( i ∗ j − 10∗ f l o o r ( i ∗ j / 1 0 ) ) ; b = ( int ) ( f l o o r ( i ∗ j /1 e4 )−10∗ f l o o r ( i ∗ j /1 e5 ) ) ; e = ( int ) ( f l o o r ( i ∗ j /10) −10∗ f l o o r ( i ∗ j / 1 0 0 ) ) ; c = ( int ) ( f l o o r ( i ∗ j / 1 0 0 0 ) − 10∗ f l o o r ( i ∗ j /1 e4 ) ) ; d = ( int ) ( f l o o r ( i ∗ j / 1 0 0 ) − 10∗ f l o o r ( i ∗ j / 1 0 0 0 ) ) ; 30 35 if ( ( a==f )&&(b==e )&&( c==d ) ) { f p r i n t f ( out , ”%d %d %d\n” , i , j , i ∗ j ) ; if ( i ∗ j > l a r g e s t ) { largest = i∗j ; } } 40 45 } } } f p r i n t f ( s t d e r r , ” L a r g e s t p a l i n d r o m e = %d\n” , l a r g e s t ) ; f c l o s e ( out ) ; return 0 ; 50 } The code cycles through the products of integers between 100 and 999, inspecting 5-digit numbers of the form abcde for the condition a = e and b = d and 6-digit numbers of the form abcdef for the condition a = f , 4. Largest palindrome product continued on next page. . . Page 5 of 12 Brian Powell Project Euler 4. Largest palindrome product (continued) b = e, and c = d. The largest palindromic number is found to be 906 609. (12) 5. Smallest multiple 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20? An integer x that is divisible by an integer a must contain a factor of a: x = ma for some integer m. For example, the smallest number that is divisible by the integers 3 and 2 is 6 = 3 × 2. Naively, then, one might be tempted to construct the number in question by forming the product of all integers up to and including 20. The reason this fails is clear: if this number is divisible by 4 and 2, isn’t it then also necessarily divisible by 8? Yes, and 8 in this case should not be included as a separate factor. The idea is to build this number piecewise as follows. Starting with the smallest nontrivial integer, it must include a factor of 2 to be divisible by 2. Ditto for 3. That gives x = 2 × 3 × · · · . We need a factor of 4 as well, but we don’t just stick this on: there is already a factor of 2 and so only one additional factor needs to be included. This gives x = 22 × 3 × · · · . The factor 5 needs to be explicitly included, as, in fact, do all the primes up to 20 since these cannot be accounted for by other factors, x = 22 ×3×5×7×11×13×17×19×· · · . We proceed to consider the remaining composite integers. The number 6 is already included in our product in the factors 2 and 3; the number 8, however, is not – we need an additional factor of 2, giving x = 23 × 3 × 5 × 7 × 11 × 13 × 17 × 19 × · · · . To recap and emphasize: the factor 23 is divisible by 2, 4, and 8 and so the integers 4 and 8 do not explicitly appear in the product. Similar situation for 9 – we need only an additional factor of 3 on top of the one already present: x = 23 × 32 × 5 × 7 × 11 × 13 × 17 × 19 × · · · . The number 10 is accounted for (2 × 5) as are 12 (22 × 3), 14 (2 × 7), 18 (2 × 32 ), and 20 (22 × 5). That leaves 16 = 42 = 24 , so we need to add one more factor of 2, giving our final answer, 24 × 32 × 5 × 7 × 11 × 13 × 17 × 19 = 232 792 560 (13) This is a prime factorization of the number 232792560, and by the fundamental theorem of arithmetic is the unique prime factorization. And, while it is the smallest positive number divisible by all integers up to and including 20, it is also the smallest positive number divisible by all integeres up to an including 19, 21, and 22. This is because the factors of the composite numbers 20, 21, and 22 are included in the product. Inspection of the product reveals a prescription for generating the smallest multiple of integers less than some number, n: for each prime number pi less than n, include as many powers as possible without going over n. In other words, each prime should satisfy pki such that pk+1 > n. i 6. Sum of square difference The sum of the squares of the first ten natural numbers is, 12 + 22 + ... + 102 = 385. The square of the sum of the first ten natural numbers is, (1 + 2 + ... + 10)2 = 552 = 3025. Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025385 = 2640. Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum? When Carl Friedrich Gauss was a little boy, he derived a formula for the sum of natural numbers between 1 and n, n X n × (n + 1) . (14) i= 2 i=1 6. Sum of square difference continued on next page. . . Page 6 of 12 Brian Powell Project Euler 6. Sum of square difference (continued) A discussion and derivation of this well-known formula can be found in many places (see the discussion surrounding Figure 3 at http://tangentspace.info/Articles/tri.php). The square of the sum of the first n natural numbers is then the square of Eq. (14). Easy. What about the sum of the squares? Consider the graphical representation of the first few squares, Figure 1. Figure 1: Geometric representation of the first few square numbers – the red discs are those that must be added to the (i−1)st figure to obtain the ith . Denoting i2 by Si (for square number i), the figure shows S1 , S2 , S3 , and S4 . We are interested in finding Pn Pn a closed form expression for the sum i=1 Si = i=1 i2 , like Eq. (14 above. The key is to focus on the red discs in the figure: these are the discs that must be added to Si−1 to obtain Si . They are awesomely called gnomons, and have the values: 3, 5, 7, 9, · · · – the odd natural numbers greater than 1. The strategy is to use these gnomons to find a recursion relation between Si−1 and Si . First, we can write Si = Si−1 + 2i − 1. (15) Continuing on, = Si−2 + 2(i − 1) − 1 Si−1 = Si−2 + 2i − 3. (16) Putting this into Eq. (15) gives Si = Si−2 + (2i − 1) + (2i − 3). Pj Next, we examine the first few terms of the sum i Si , Si + Si−1 + Si−2 + · · · (17) = Si−1 + (2i − 1) + Si−1 + Si−2 · · · = Si−2 + (2i − 3) + (2i − 1) + · · · 3Si−3 + 3 × (2i − 5) + 2 × (2i − 3) + (2i − 1) + · · · = (18) The idea is to continue doing this until we have summed all the way down to S1 = 1 so that there are no S’s left on the right-hand side of the equation, except that that would take an eternity since i is arbitrarily large. The key is to identify the pattern emerging in the sum and guess the expression. The terms are of the general form j × (2i − 2j + 1), where j runs from 1 to i, so that i X j=1 Solving for P Sj = i X j × (2i − 2j + 1) = j=1 i X j2. (19) j=1 j 2 gives i X j=1 j2 = i(i + 1)(2i + 1) , 6 where we’ve used the earlier result Eq. (14). With i = 100, we solve ( (14) and Eq. (19) giving 25 164 150 (20) Pi j)2 − Pi j 2 using the square of Eq. (21) Page 7 of 12 Brian Powell Project Euler 6. Sum of square difference 7. 10001st prime By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. What is the 10001st prime number? The solution requires a simple program that computes prime numbers. Here is one that does so through trial division: Listing 3: Prime Generator #include < s t d i o . h> #include <math . h> /* 5 10 15 * This program takes a single integer command line argument. * It outputs the nth prime number. */ int main ( int a r g c , char ∗ a r g v [ ] ) { int n = a t o i ( a r g v [ 1 ] ) ; int i =0 , j =0 , k =0; int n o p r i m e = 0 ; FILE ∗ o u t ; o u t = f o p e n ( ” o u t t r i a l . d a t ” , ”w” ) ; f p r i n t f ( out , ” 2\ n” ) ; /* * For each i, perform trial divisions for all numbers less than * sqrt(i). */ 20 i = 3; 25 while ( k<(n −1)) // n-1 because 2 is already counted { noprime = 0 ; for ( j =1; j <=(int ) c e i l ( s q r t ( i ) ) ; j=j +2) { if ( ( i%j == 0)&&( j ! = 1 ) ) // j divides i { noprime = 1 ; break ; // not prime, break } } if ( ! n o p r i m e ) { f p r i n t f ( out , ”%d\n” , i ) ; k++; } i = i + 2; 30 35 40 } 7. 10001st prime continued on next page. . . Page 8 of 12 Brian Powell Project Euler 7. 10001st prime (continued) f c l o s e ( out ) ; f p r i n t f ( s t d e r r , ”%d\n” , k +1); return 0 ; 45 } It computes the nth prime number. This is essentially the code inside the for loop in Problem 3; a description of how it works is given there. For an input of 10001, we find 104 743 (22) 8. Largest product in a series The four adjacent digits in the 1000-digit number that have the greatest product are 9 9 8 9 = 5832. 73167176531330624919225119674426574742355349194934 96983520312774506326239578318016984801869478851843 85861560789112949495459501737958331952853208805511 12540698747158523863050715693290963295227443043557 66896648950445244523161731856403098711121722383113 62229893423380308135336276614282806444486645238749 30358907296290491560440772390713810515859307960866 70172427121883998797908792274921901699720888093776 65727333001053367881220235421809751254540594752243 52584907711670556013604839586446706324415722155397 53697817977846174064955149290862569321978468622482 83972241375657056057490261407972968652414535100474 82166370484403199890008895243450658541227588666881 16427171479924442928230863465674813919123162824586 17866458359124566529476545682848912883142607690042 24219022671055626321111109370544217506941658960408 07198403850962455444362981230987879927244284909188 84580156166097919133875499200524063689912560717606 05886116467109405077541002256983155200055935729725 71636269561882670428252483600823257530420752963450 Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product? This is a straight-forward coding exercise: the idea is to read this massive number in as a string and step through it in blocks of 13 digits, computing the product of each block. There will be many strings of 13 digits that contain zeros and these can be discarded from the outset; however, with only 988 products to compute this efficiency measure is probably not warranted. One caveat is that 64-bit data types are needed since the products can be quite large. In C, we therefore need long long int types, but these are painful to use2 . Python has a much more forgiving handling of integer types, and so Python it is: Listing 4: Product of Consecutives Integers import s y s import o s 5 10 s t r = ””” 7 3 1 6 7 1 7 6 5 3 1 3 3 0 6 2 4 9 1 9 2 2 5 1 1 9 6 7 4 4 2 6 5 7 4 7 4 2 3 5 5 3 4 9 1 9 4 9 3 4 9 6 9 8 3 5 2 0 3127745063262395783180169848018694788518438586156078911294949545950 1737958331952853208805511125406987471585238630507156932909632952274 4304355766896648950445244523161731856403098711121722383113622298934 2338030813533627661428280644448664523874930358907296290491560440772 3907138105158593079608667017242712188399879790879227492190169972088 8093776657273330010533678812202354218097512545405947522435258490771 2 Not so bad when dealing with literals, although still annoying because you need to cast the literal as a long long int even though you’ve already declared it as such, e.g. long long int x = (long long int)5*6*7*.... However, I’ve not figured out how to get long long int to work for variables. 8. Largest product in a series continued on next page. . . Page 9 of 12 Brian Powell 15 20 25 30 Project Euler 8. Largest product in a series (continued) 1670556013604839586446706324415722155397536978179778461740649551492 9086256932197846862248283972241375657056057490261407972968652414535 1004748216637048440319989000889524345065854122758866688116427171479 9244429282308634656748139191231628245861786645835912456652947654568 2848912883142607690042242190226710556263211111093705442175069416589 6040807198403850962455444362981230987879927244284909188845801561660 9791913387549920052406368991256071760605886116467109405077541002256 9 8 3 1 5 5 2 0 0 0 5 5 9 3 5 7 2 9 7 2 5 7 1 6 3 6 2 6 9 5 6 1 8 8 2 6 7 0 4 2 8 2 5 2 4 8 3 6 0 0 8 2 3 2 5 7 5 3 0 4 2 0 7 5 2 9 6 3 4 5 0 ””” biggest = 0 for i in r a n g e ( 0 , 9 8 8 ) : x = 1 for j in r a n g e ( 0 , 1 3 ) : x = int ( s t r [ i+j ] ) ∗ x # print("%s %s" % (x,str[i+j])) if x > b i g g e s t : biggest = x k = i print ( ” D i g i t s : %s \ n P r o d u c t : %s ” % ( s t r [ k : k +13] , b i g g e s t ) ) This script identifies 5576689664895 as the 13 consecutive digits. Their product is 23 514 624 000 (23) 9. Special Pythagorean triple A Pythagorean triplet is a set of three natural numbers, a < b < c, for which, a2 + b2 = c2 . For example, 32 + 42 = 9 + 16 = 25 = 52. There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product abc. Euclid in his Elements discovered that integers that satisfy the equation a2 + b2 = c2 can be written a = k · (m2 − n2 ) b = k · 2mn c = k · (m2 + n2 ) (24) where m, n, and k are positive integers with m > n, m − n odd, and m and n coprime (they share no common factors). In terms of these integers, the condition a + b + c = 1000 becomes km · (m + n) = 500. We have written 500 as essentially a product of two factors: km and (m + n). There are not many ways to write 500 as a product of two factors, only 10 actually: 500 × 1, 250 × 2, 125 × 4, 100 × 5, 50 × 10 and their commutated forms. The idea is to match km and (m + n) with one of these factorizations. It’s lucky that there are only 10, or else we’d likely need to use a computer. The first two don’t work: (m + n) > 2 since m > n. The third is also no good: while we can arrange for m + n = 4 with m = 3 and n = 1, 3k 6= 125 for any k since 125 is not a multiple of 3. We hit pay dirt with the fourth factorization: 100 × 5. We have m + n = 5 with m = 4 and n = 1, and 4k = 100 with k = 25. Using these values of m, n, and k in Eq. (24) gives a = 375, b = 200, and c = 425. It is easy enough to check that (a, b, c) forms a Pythagorean triplet, a2 + b2 = c2 . Their product is abc = 31 875 000 9. Special Pythagorean triple continued on next page. . . (25) Page 10 of 12 Brian Powell Project Euler 9. Special Pythagorean triple (continued) 10. Summation of primes The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. Find the sum of all the primes below two million.. We can use a code very similar to the one written to solve Problem 7, but one that computes and sums all the primes less than some number n rather than the k th prime. Listing 5: Summation of Primes #include < s t d i o . h> #include <math . h> /* 5 10 15 * This program takes a single integer command line argument. * It writes a file containing all the primes less than the input integer and outputs their su */ int main ( int a r g c , char ∗ a r g v [ ] ) { int n = a t o i ( a r g v [ 1 ] ) ; int i =0 , j =0 , k =0; int n o p r i m e = 0 ; FILE ∗ o u t ; o u t = f o p e n ( ” o u t t r i a l . d a t ” , ”w” ) ; long long int sum ; f p r i n t f ( out , ” 2 ” ) ; 20 sum = 2 ; /* * For each i, perform trial divisions for all numbers less than * sqrt(i). */ 25 30 35 40 for ( i =3; i <=n ; i=i +2) { noprime = 0 ; for ( j =1; j <=(int ) c e i l ( s q r t ( i ) ) ; j=j +2) { if ( ( i%j == 0)&&( j ! = 1 ) ) // j divides i { noprime = 1 ; break ; // not prime, break } } if ( ! n o p r i m e ) { sum = sum + i ; f p r i n t f ( out , ”%d ” , i ) ; k++; } 10. Summation of primes continued on next page. . . Page 11 of 12 Brian Powell Project Euler 10. Summation of primes (continued) } f c l o s e ( out ) ; f p r i n t f ( s t d e r r , ” l a r g e s t p r i m e = %d , sum o f p r i m e s = %l l d \n” , k +1,sum ) ; return 0 ; 45 } The sum of the first 2 million primes is 142 913 828 922 (26) Page 12 of 12