Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Contents
1
Problem ................................................................................................................................................. 1
Example ................................................................................................................................................. 4
6.1
Introduction .................................................................................................................................. 6
6.2
6.3
6.4
6.5
6.6
Example ....................................................................................................................................... 10
1 Problem
Let ()be the prime counting function, i.e. the number of prime numbers less of equal to . For
example, (1) = 0, (2) = 1, (100) = 25.
Let (, ) be the number of k-tuples (1 , , ) which satisfy:
1. every is a positive integer;
2. =1 ( ) =
For example (3,3) = 19.
The 19 tuples are (1,1,5), (1,5,1), (5,1,1), (1,1,6), (1,6,1), (6,1,1), (1,2,3), (1,3,2), (2,1,3), (2,3,1), (3,1,2),
(3,2,1), (1,2,4), (1,4,2), (2,1,4), (2,4,1), (4,1,2), (4,2,1), (2,2,2).
You are given (10,10) = 869 985 and (103 , 103 ) 578 270 566 (mod 1 004 535 809).
Find (20 000,20 000) mod 1 004 535 809.
2 Basic solution
Split the k-tuple in two smaller tuples of length and , with + = .
Let = =1 ( ), the sum of the prime counting function on the first tuple .
The sum on the second tuple is = =+1 ( ).
Any tuple counted in (, ) must be composed of a tuple counted in (, ) and one counted in (
, ). Thus:
(, ) = =0 (, )( , )
(1)
1
+1
if = 0
if > 0
(2)
The answer (, ) is returned in the ( + 1)th element of the vector, i.e. using a zero based
vector index.
Copyright Project Euler, further distribution without the consent of the author(s) prohibited.
Author: Martin Piotte
(3)
, (), () = +
=0
(4)
With:
= =0 (, )( , ) for .
(5)
(6)
()
, () = ,1
mod +1
(7)
Where (() mod ) indicates the polynomial () truncated by removing all terms containing
raised to a power larger or equal to , i.e. keeping only the powers of smaller than .
We can rewrite Algorithm 1 using polynomials as shown in Algorithm 2. Using long polynomial
multiplication, its complexity is also (2 log ).
() 1
() n,1 ()
while 0
if is odd then
() (() ()) mod +1
() ()2 mod +1
Copyright Project Euler, further distribution without the consent of the author(s) prohibited.
Author: Martin Piotte
4 Example
To illustrate the approach, we shall compute (3,3) using the polynomial view.
From equation (2) we have 3,1 = [1,1,2,2].
From equation (3) we have 3,1 () = 1 + + 2 2 + 2 3 .
From equation (6) we have:
2 ()
3,2 () = 3,1
mod 4
= (1 + + 2 2 + 2 3 )2 mod 4
= (1 + 2 + 5 2 + 8 3 + 8 4 + 8 5 + 4 6 ) mod 4
= 1 + 2 + 5 2 + 8 3
5 Karatsuba multiplication
Polynomial multiplication, using long multiplication, has complexity (2 ), and when combined with
exponentiation by squaring results in an algorithm with complexity (2 log ).
However, by framing the algorithm using polynomial multiplication, we can leverage fast polynomial
multiplication algorithms to create a faster algorithm. Karatsuba multiplication is one such algorithm.
Let () and () be polynomials of degree not exceeding 2 1. We can write ()and () as:
() = () + ()
(8)
() = () + ()
(9)
Where (), (), () and () have degree not exceeding 1. Leading zeros are added if the
polynomials do not have the same degree or the degree is an even number. The product becomes:
()() = (() + () ) (() + () )
= ()() + (()() + ()()) + ()() 2
= ()() + ((() + ())(() + ()) ()() ()()) + ()() 2
The last expression contains only three distinct products:
()(), ()() and (() + ())(() + ()).
Copyright Project Euler, further distribution without the consent of the author(s) prohibited.
Author: Martin Piotte
The product of two polynomials of degree 2 1 is achieved using only three multiplications of
polynomials of degree 1, at the cost of a few extra additions and subtractions. However, since
addition and subtraction have complexity (), Karatsuba multiplication has asymptotic complexity
log 3
(log 2 ) (1.58 ) when used recursively on the smaller pieces, much faster than long multiplication
for sufficiently large .
Lets use Karatsuba multiplication to compute 3,1 ()3,2 ().
3,1 () = (1 + ) + (2 + 2) 2
3,2 () = (1 + 2) + (5 + 8) 2
Compute () + ():
(1 + ) + (2 + 2) = 3 + 3
Compute () + ():
(1 + 2) + (5 + 8) = 6 + 10
Compute (() + ())(() + ()):
(3 + 3)(6 + 10) = 18 + 48 + 30 2
Compute ()():
(1 + )(1 + 2) = 1 + 3 + 2 2
Compute ()():
(2 + 2)(5 + 8) = 10 + 26 + 16 2
Compute (() + ())(() + ()) ()() ()():
(18 + 48 + 30 2 ) (1 + 3 + 2 2 ) (10 + 26 + 16 2 ) = 7 + 19 + 12 2
Compute ()() + ((() + ())(() + ()) ()() ()()) 2 + ()() 4 :
(1 + 3 + 2 2 ) + (7 + 19 + 12 2 ) 2 + (10 + 26 + 16 2 ) 4
= 1 + 3 + 9 2 + 19 3 + 22 4 + 26 5 + 16 6
We obtain the same expression we obtained in the first example.
Using Karatsuba multiplication, the asymptotic complexity of our complete algorithm becomes
log 3
(log 2 log ), which allows us to compute (50 000, 50 000) 587 156 969 (mod 1 004 535 809)
in under a minute.
Copyright Project Euler, further distribution without the consent of the author(s) prohibited.
Author: Martin Piotte
(log 3 ) (1.46 ) and can be used to compute (105 , 105 ) 160 727 258 (mod 1 004 535 809) in
under a minute. However we shall not provide a detailed explanation here.
Copyright Project Euler, further distribution without the consent of the author(s) prohibited.
Author: Martin Piotte
For a prime modulus (e.g. 1 004 535 809) and a power of two, 1 1 (mod ) and thus
(mod )
(10)
1
2
2 1 (mod ).
(11)
The easiest way to find a primitive th root of unity is simply to test different values of until condition
(11) is met.
For example, using = 1 004 535 809 and = 221 (the largest power of two dividing 1), then
3 (mod ) is the smallest value that satisfies condition (11) which gives us
702 606 812 (mod ) from equation (10). Note that there are other values of that we could use
(other primitive th roots of unity). However, for our algorithm, any primitive th root of unity will do.
Note that if we want to compute the FFT over the complex numbers instead of the finite field of the
2
coefficients respectively:
() = 0 + 1 + 2 2 + + 1 1
(12)
() = 0 + 2 + 4 2 + + 2 2 1
(13)
() = 1 + 3 + 5 2 + + 1 2 1
(14)
() = ( 2 ) + ( 2 )
(15)
() = ( 2 ) ( 2 )
(16)
values of () and
+
2
(). Since
= , to evaluate ( 0 ), ( 1 ), , ( 1 ), we need only to evaluate
( 2 ), ( 4 ), , ( 2 ), ( 2 ), ( 4 ), , ( 2 ) and apply equations (15)
and (16).
By applying this procedure recursively, we can evaluate ( 0 ), ( 1 ), , ( 1 ) with complexity
( log ).
Copyright Project Euler, further distribution without the consent of the author(s) prohibited.
Author: Martin Piotte
Function FFT
Input: , , [0 , 1 , , 1 ]
Output: [( 0 ), ( 1 ), , ( 1 )]
If = 1 return [0 ]
m
( 2 , , [0 , 2 , , 2 ])
2
m
2
( , , [1 , 3 , , 1 ])
2
for 0 <
2
for
<
2
= {
/2 + /2
return .
Algorithm 3 FFT
=
[
0
1
2
0
2
4
2(1)
0
1
2(1)
(1)(1) ]
= [0 , 1 , 2 , , 1 ]
Then:
=
(17)
(18)
=
[ 0
0
1
2
(1)
0
2
4
2(1)
(1)
2(1)
(1)(1) ]
Copyright Project Euler, further distribution without the consent of the author(s) prohibited.
Author: Martin Piotte
Let = . .
Z has element zi,j at row , column :
1
()
, = 1
= 1
=0 , , = =0
=0
(19)
, =
1 ()(+)
=0 =0
, =
1
()( +)
2
(
=0 =0
()( ++
, =
1 ()(+)
2
(1 +
=0 =0
(20)
)
2
( ) )
(21)
(22)
, =
1 ()(+)
2
(1
=0 =0
1) = 0
(23)
Thus:
, = {
(24)
(25)
1 = 1
(26)
Equation (26) tells us that in order to compute the coefficients of (), all we need is to compute
1
( 1 , [(0 ), (1 ), (2 ), , (1 )]).
Copyright Project Euler, further distribution without the consent of the author(s) prohibited.
Author: Martin Piotte
10
Function MultiplyFFT
Input: , [0 , 1 , , ], [0 , 1 , , ]
Output: [0 , 1 , , 2 ]
22 (2+1) where the brackets indicate the ceiling function
If 1 return error.
2
1
While 2 1 (mod )
+1
1
mod
(, , [0 , 1 , , 1 ]) vector padded to length m with zeros
(, , [0 , 1 , , 1 ]) vector padded to length m with zeros
Return 1 ( 1 , , [0 0 , 1 1 , 2 2 , , m1 1 ])
Algorithm 4 Multiplication using FFT
The time required to execute this algorithm is dominated by the three FFT invocations, so the
asymptotic complexity is the same as the FFT, i.e. ( log ). Solving the complete problem, including
the exponentiation by squaring, has asymptotic complexity ( log log ), which allows us to
compute (106 , 106 ) 10 833 650 (mod 1 004 535 809) in under a minute.
Algorithm 4 requires that | 1. It is the simplest form of FFT. More advanced forms can cope with
cases where 1 does not contain a large power of two, at the cost of extra complexity. In the current
problem, was selected so that 221 | 1, making FFT computations simpler.
6.6 Example
Lets use FFT multiplication to compute 3,1 ()3,2 () mod 97. We selected = 97 to keep numbers
small.
1. = 3
2. = 8
3. = 5
1
4.
5.
6.
7.
11
12
Copyright Project Euler, further distribution without the consent of the author(s) prohibited.
Author: Martin Piotte