0 penilaian0% menganggap dokumen ini bermanfaat (0 suara)
9 tayangan2 halaman
T he problem can easily be solved wit hout any programming. However, our analysis will lead in a different direct ion. Our aim is to develop an efficient algorithm capable of solving for larger values of k.
T he problem can easily be solved wit hout any programming. However, our analysis will lead in a different direct ion. Our aim is to develop an efficient algorithm capable of solving for larger values of k.
Hak Cipta:
Attribution Non-Commercial (BY-NC)
Format Tersedia
Unduh sebagai PDF, TXT atau baca online dari Scribd
T he problem can easily be solved wit hout any programming. However, our analysis will lead in a different direct ion. Our aim is to develop an efficient algorithm capable of solving for larger values of k.
Hak Cipta:
Attribution Non-Commercial (BY-NC)
Format Tersedia
Unduh sebagai PDF, TXT atau baca online dari Scribd
Proj ect Euler Furt her dist ribut ion wit hout consent of t he aut hor( s) is prohibit ed.
it ed. Aut hor: euler
Pr obl em 5 What i s t he smal l est number di v i si bl e by each of t he number s 1 t o 20? Given t he small size of t he paramet er, k = 20, t his problem can easily be solved wit hout any programming, as it is equivalent t o finding t he least common mult iple of t he set of int egers { 2, 3, 4, , 20} . However, our analysis will lead in a different direct ion wit h t he aim of developing an efficient algor it hm capable of solving for larger values of k. Let N be t he smallest number t hat is divisible by all t he int egers from 2 t o k. For N t o be t he smallest value wit h t his propert y we must ensure t hat in it s prime fact orisat ion it does not cont ain any more prime fact ors t han is absolut ely necessary. Consider t he first t hree cases of k: k = 2, N = 2 k = 3, N = 2* 3 = 6 k = 4, N = 2* 3* 2 = 12 We can see t hat for k = 4 we did not need t o evaluat e 2* 3* 4, because one of t he 2s in t he prime fact orisat ion of 4 = 2* 2 was already included. I f we now consider t he next t wo cases: k = 5, N = 2* 2* 3* 5 = 60 k = 6, N = 2* 2* 3* 5 = 60 We can see t hat N = 60 for bot h k = 5 and k = 6, because if N cont ains t he fact ors 2 and 3 it already cont ains everyt hing necessary for it t o be divisible by 6. Applying t his pr inciple for t he case k = 20: N = 2 * 3 * 2 * 5 * 7 * 2 * 3 * 11 * 13 * 2 * 17 * 19 = 232792560 So how do we solve t his programmat ically? Let p[ i] be t he p t h pr ime number: p[ 1] = 2, p[ 2] = 3, p[ 3] = 5, . Let N = p[ 1] ^ a[ 1] * p[ 2] ^ a[ 2] * p[ 3] ^ a[ 3] * ; init ially, let a[ i] = 0 for all i. For j = 2 t o k, wr it e each j in t he form, p[ 1] ^ b[ 1] * p[ 2] ^ b[ 2] * p[ 3] ^ b[ 3] * and set a[ i] = max( a[ i] , b[ i] ) . Let us observe t his algorit hm wit h a t race for k = 10. j b[ 1] b[ 2] b[ 3] b[ 4] a[ 1] a[ 2] a[ 3] a[ 4] 2 1 1 3 1 1 1 4 2 2 1 5 1 2 1 1 6 1 1 2 1 1 7 1 2 1 1 1 8 3 3 1 1 1 9 2 3 2 1 1 10 1 1 3 2 1 1 Hence N = 2^ 3 * 3^ 2 * 5^ 1 * 7^ 1 = 2520. However, t his approach requires a funct ion t o be const ruct ed t o express a given number as a product of pr ime fact ors. So we shall consider an alt ernat ive approach. Proj ect Euler Furt her dist ribut ion wit hout consent of t he aut hor( s) is prohibit ed. Aut hor: euler Let us consider t he case of finding t he least value of N for k = 20. We know t hat N must be divisible by each of t he pr imes, p[ i] , less t han or equal t o k. But what det ermines t he exponent , a[ i] , in t he prime fact orisat ion of N is t he great est perfect power of p[ i] t hat is less t han or equal t o k. For example, as 2^ 4 = 16 and 2^ 5 = 32, we know t hat a[ 1] = 4 as no ot her numbers, 2 j 20, can cont ain more t han four repeat ed fact ors of 2. Similar ly 3^ 2 = 9 and 3^ 3 = 27, so a[ 2] = 2. And for p[ i] l 5, a[ i] = 1. Hence N = 2^ 4 * 3^ 2 * 5 * 7 * 11 * 13 * 17 * 19 = 232792560. For a given p[ i] we can det ermine a[ i] in t he following way. Let p[ i] ^ a[ i] = k. By logging bot h sides: a[ i] log( p[ i] ) = log( k) . So a[ i] = log( k) / log( p[ i] ) . But as a[ i] must be int eger, a[ i] = floor( log( k) / log( p[ i] ) ) . For example, when k = 20, t he exponent of t he first pr imes fact or, p[ 1] = 2, will be: a[ 1] = floor( log( 20) / log( 2) ) = floor( 4.32) = 4 To opt imise t he approach even furt her we not e t hat a[ i] = 1 for p[ i] ^ 2 > k. I n ot her words, we only need t o evaluat e a[ i] for p[ i] sqrt(k). To put all t his t oget her int o an algor it hm we shall assume t hat an array of pr imes p[ 1] , p[ 2] , p[ 3] , has already been creat ed. k = 20 N = 1 i = 1 check = t r ue l i mi t = sqr t ( k) whi l e p[ i ] <= k a[ i ] = 1 i f check t hen i f p[ i ] <= l i mi t t hen a[ i ] = f l oor ( l og( k) / l og( p[ i ] ) ) el se check = f al se end i f end i f N = N * p[ i ] ^ a[ i ] i = i + 1 end whi l e out put N I t must be not ed t hat t he pract ical limit of t his algor it hm is t he capabilit y of your programming language in handling t he size of N as k increases.