Number Theory
Primes: A positive integer p is called prime if it has just two divisors, namely 1 and p.
By convention, 1 is not prime, so the sequence of primes starts out like this:
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41,
Primes are of great importance, because they are the fundamental building blocks of all
the positive integers. Any positive integer n can be written as a product of primes,
1 2
1
m
m k
k
n p p p p

   ,where 1 2 mp p p    (1)
For example, 12 2 2 3 ; 11011 7 11 11 13 ; 11111 41 271        
Here the products denoted by  are analogous to sums denoted by  . There is only one
way to write n as a product of primes in non-decreasing order. This statement is called
the Fundamental Theorem of Arithmetic.
We can prove the Fundamental Theorem of Arithmetic by contradiction. Suppose, we
have two factorization of an integer number, n
1 2 1 2m nn p p p q q q   , 1 2 1 2andm np p p q q q      
where p’s and q’s are all prime. We will prove that 1 1p q . If not, we can assume that
1 1p q , making 1p smaller than all q’s. Since 1p and 1q are prime their gcd must be 1.
Hence, Euclid’s self-certifying algorithm gives us integer a and b such that
1 1
1 2 3 1 2 3 2 3
1 2 3 2 3
1 2 3 1 2 3 2 3
1 2 3 2 3 2 3
1
( )
n n n
n n
n m n
n m n
ap bq
ap q q q bq q q q q q q
ap q q q bn q q q
ap q q q bp p p p q q q
p aq q q bp p p q q q
 
  
  
  
  
  
 
  
  
Since 1p divides left hand side, hence 1p should divide right hand side 2 3 nq q q . But 1p is
not divisible by 2 3 nq q q according to our assumption. Thus 1p and 1q must be equal.
Similarly we can show that, 2 2 3 3, , , m np q p q p q   .
Every positive integer can be written uniquely in the form
pn
p
n p  where each 0pn   (2)
Formula (2) represents n uniquely, so we can think of the sequence 2 3 5( , , , )n n n  as a
number system for positive integers. For example, prime exponent representation of 12 is
(2 , 1 , 0 , 0 , . . .) and the prime exponent representation of 18 is (1 , 2 , 0 , 0 , . . .). To
multiply two number, we simply add their representations. In other words,
for allp p pk mn k m n p     (3)
This implies that
 for allp pm n m n p   (4)
and it follows immediately that,
p1 can't divide ...
np is the power of the prime p in the unique prime factorization of the integer n
example:
100 = 2^2 * 3^0 * 5^2 * 7^0 * 11^0 * ...
352 = 2^5 * 3^0 * 5^0 * 7^0 * 11^1 * ...
11 = 1 * 11
43 = 1 * 43
for prime integers:
***
MUST
SEE
THIS
PROOF
***
GCD (p1, q1) = 1 because both p1, q1 primes
Euclid's theorem: The GCD of two numbers
can always be written as a linear combination
of the two numbers ==> so, ap1 + bq1 = 1 for
integers a, b
uniquely
END
Start
gcd( , ) min( , ) for allp p pk m n k m n p    (5)
( , ) max( , ) for allp p pk lcm m n k m n p    (6)
For example, since 2 1
12 2 3  and 1 2
18 2 3  , we can get their gcd and lcm by taking the
min and max of common exponents:
min(2,1) min(1,2) 1 1
gcd(12,18) 2 3 2 3 6    
max(2,1) max(1,2) 2 2
(12,18) 2 3 2 3 36lcm     
Prime Examples: Euclid proved that, there are infinitely many primes. The proof is
as follows: Suppose, there are only finitely many primes, say k of them 2,3,5, , kp .
Then we should consider the number, 2 3 5 1kM p      .
None of k primes can divide M, because each divides M-1. Thus there must be some other
prime that divides M; perhaps M itself is a prime. This contradicts our assumption that,
2,3,5, , kp are the only primes, so there must be infinitely many prime.
Euclid Number: 1 2 1 1n ne e e e   , when 1n   (7)
The sequence starts out
1
2
3
1 1 2
2 1 3
2 3 1 7
e
e
e
  
  
   
4 2 3 7 1 43e     
these are all prime. But the next case, 5e is 1807 13 139  . However, Euclid numbers are
all relatively prime to each other; that is
gcd( , ) gcd(1, ) gcd(0,1) 1m n me e e   , where m n .
Recurrence (7) can be simplified by removing three dots. If 1n  , we have
2
1 2 2 1 1 1 1 11 ( 1) 1 1n n n n n n ne e e e e e e e e            
The numbers of the form 2 1p
 (where p is a prime), is called Mersenne numbers. The
Mersenne prime known occur for p  2,3,5,7,13,17,19,31,61,89,107,127,521,607,1279,
2203,2281,3217,4253,4423,9689,9941,11213,19937,21701,23209,44497,86243,110503,
132049,216091,756839 and 859433. The number 2 1n
 cannot possibly prime if n is not
prime, because 2 1km
 has 2 1m
 as a factor. We can prove it by following way:
2 3 ( 1) 2 1
1 2 2 2 2
2 1
km
m m m k m
m
 
     


Left hand side of the equation is integer, which refers to right hand side is also an integer.
That means, 2 1km
 is divisible by 2 1m
 . Thus 2 1km
 is not a prime number.
But, 2 1p
 is not always a prime when p is prime. For example, 11
2 1 2047 23 89    is
the smallest such nonprime.
e0 = 1 and e1=e0+1=2 .... now compute e2, e3, e4, so on ...
Q: Write the Prime Exponent Representation of GCD(m,n), LCM(m,n) and PRODUCT(m,n). Also, Provide Concrete Numeric
Example.
Q: What do you understand by Euclid number/ Mersenne number? Explain with Concrete examples.
e2 = e0*e1 + 1 = 1*2+1=3
e3 = e0*e1*e2+1=1.2.3 + 1 = 7
e4 = 1.2.3.7 + 1 = 43
en = e0 * e1 * e2 * ... ... ... en-1 + 1
Short Q: Prove or Disprove that Euclid number is always Prime: Ans: Disprove by a counter example (e5 = 1807 = 13*139)
Short Q: Prove or Disprove that Mersenne number is always Prime: Ans: Disprove by Counter example (p=11 => 2^11 - 1
= 2047 = 23 *89)
e5 = 1.2.3.7.43 + 1 = 1807
Prove that: Any integer of the form 2^p - 1 can never be a prime number, given that p is a composite number.
Number Theory
Factorial Factors: The factorials:
1
! 1 2
n
k
n n k

      , integer 0n 
We can find out factorial of an integer n using following recursive formula:
0! 1
! ( 1)!n n n  , for 0n 
Here are the first few values of the factorial function.
n 0 1 2 3 4 5 6 7 8 9 10
!n 0 1 2 6 24 120 720 5040 40320 362880 3628800
Because of it’s exponential nature it’s very difficult to find a closed form of factorial. To
approximate n! more accurately for large n we can use Stirling’s formula:
! 2
n
n
n n
e

 
 
 
We would like to determine, for any given prime p, the largest power of p that divides n! ;
that is we want the exponent of p in n!’s unique factorization. We denote this number by
( !)p n , and we start our investigations with the small case p = 2 and n = 10.
1 2 3 4 5 6 7 8 9 10 Powers of 2
Divisible by 2 X X X X X 105
2
 
 
Divisible by 4 X X 102
4
 
 
Divisible by 8 X 101
8
 
 
Powers of 2 0 1 0 2 0 1 0 3 0 1 8
For general n this method gives,  2
1
!
2 4 8 2k
k
n n n n
n

       
                  

This sum is actually finite, since the summand is zero when 2k
n . Therefore it has
only lg n   nonzero terms, and it’s computationally quite easy. For instance, if n = 100,
 2
100 100 100 100 100 100
100! 50 25 12 6 3 1 97
2 4 8 16 32 64

           
                                  
Let’s look at the binary representation of all numbers to find out what’s going on.
2
2
2
2
100 (1100100) 100
100 (110010) 50
2
100 (11001) 25
4
100 (1100) 12
8
 
   
 
   
 
   
 
2
2
2
100 (110) 6
16
100 (11) 3
32
100 (1) 1
64
   
 
   
 
   
 
Find
e2(10!)
= 8
We merely drop the least significant bit from one term to get the next. The binary
representation shows us how to derive another formula,
2 2( !) ( )n n v n   , where 2 ( )v n is the number of 1’s in the binary representation of n.
Generalizing our findings to an arbitrary prime p, we have
2 3
1
( !)p k
k
n n n n
n
p p p p


       
           
       

We can find out the upper bound of ( !)p n by simply removing the floor from the
summand and then summing an infinite geometric progression.
2 3 2
1 1
( !) 1p
n n n n
n
p p p p p p

 
        
 
 
1
1
1 1
1 ;[ <1]
1
1
1
n
p p p
n p
p p
n p
p p
n
p


 
  
 
 
  
 
 
  
 



For example, when p = 2 and n = 100, this inequality says that 97 < 100. Thus the upper
bound 100 is not only correct but also close to the true value 97.
☺Good Luck☺
Prove
that ...
Number Theory
Relative Primality: When gcd( , ) 1m n  , the integers m and n have no prime factors
in common and we say that they are relatively prime.
m _|_ n  m, n are integers and gcd( , ) 1m n 
A fraction /m n is in lowest terms if and only if m _|_ n. Since we reduce fractions to
lowest terms by casting out common factor of numerator and denominator,
/ gcd( , )m m n _|_ / gcd( , )n m n .
There is a beautiful way to construct the set of all nonnegative fractions /m n with m _|_ n,
called Stern-Brocot tree. The idea is to start with the two fractions
0 1
,
1 0
 
 
 
and then to
repeat the following operation as many times as desired:
Insert
m m
n n


between two adjacent fractions
m
n
and
m
n


. The new fraction
m m
n n


is
called mediant of
m
n
and
m
n


. For example, the first step gives us one new entry.
0 1 1
, ,
1 1 0
and the next gives two more:
0 1 1 2 1
, , , ,
1 2 1 1 0
. The next gives four more:
0 1 1 2 1 3 2 3 1
, , , , , , , ,
1 3 2 3 1 2 1 1 0
and then we will get 8, 16 and so on. The entire array can be
regarded as an infinite binary tree structure whose top levels look like this:
0 1
1 0
1
1
1 2
2 1
1 2 3
3 3 2
3
1
1 2 3 3 4 5 5 4
4 5 5 4 3 3 2 1
1 2 3 3 4 5 5 4 5 7
5 7 8 7 7 8 7 5 4 5
8 7 7 8 7 5
5 4 3 3 2 1
Level 0
Level 1 from 1 to 1
Level 2 from 1/2 to 2/1
Level 3 from 1/3 to 3/1
Level 4 from 1/4
to 4
Level 5
from 1/5 to 5
Level 1
a
a
Level 2
Level 3
(Or, m and n are Relatively Prime)
If /m n and /m n  are consecutive fractions at any stage of the construction, we have
1m n mn   . We can prove it by induction.
Basis: Initially,
0 1
and
1 0
m m
n n

 

. Thus 1 1 0 0 1m n mn      
Hypothesis: Let, 1m n mn   is true for two consecutive fractions / and /m n m n  at
any stage of the Stern-Brocot tree.
Induction: Consider, we have new mediant ( ) / ( )m m n n   between / and /m n m n .
( ) ( ) 1
( ) ( ) 1 (Proved)
m m n m n n mn m n mn mn m n mn
m n n m m n m n m n mn m n m n mn
              
                    
If
m m
n n



and if all values are nonnegative, it’s easy to verify that
m m m m
n n n n
 
 
 
.
1
( ) ( ) ( )
m m m mn m n mn mn m n mn
n n n n n n n n n n n n
        
   
      
.
Again,
1
( ) ( ) ( )
m m m m n m n mn m n m n mn
n n n n n n n n n n n n
             
   
          
.
Though 0n  and 0n  . we can write,
1 1
0 and 0
( ) ( )n n n n n n
 
   
which concludes
the prove
m m m m
n n n n
 
 
 
.
Is there any positive fraction /a b with a _|_ b possibly omitted from Stern-Brocot tree?
Let, /a b is a positive fraction. Thus,
0 1
1 0
m a m
n b n
 
      
. The construction forms
( ) / ( )m m n n   and there are three cases. Either ( ) / ( ) /m m n n a b    and we win;
or ( ) / ( ) /m m n n a b    and we can set ,m m m n n n     ;
or( ) / ( ) /m m n n a b    and we can set ,m m m n n n       . The process can’t go
infinitely because of the conditions
0 0 (1) and 0 0 (2)
a m m a
an bm bm an
b n n b

          

  .
 (1) ( ) (2) ( ) ( ) ( )( )
( ) ( )
( )( )
m n m n m n an bm m n bm an m n m n
am n bmm ann bmn bmm amn bm n ann m n m n
am n bmn amn bm n m n m n
m n a b mn a b m n m n
a b m n mn m n m n
a b m
                     
                    
            
          
         
    ; [ 1]n m n m n mn     
Either m or n or morn increases at each step, so we must win after at most (a + b) steps.
m/n, m+m'/n+n', m'/n'
Proof 1:
Proof 2:
Proof 3:
m/n < a/b < m'/n' and the inequality gradually comes closer and closer to equality to a/b as
SKIP the
Calculation
(i.e., Move to the
Right SubTree)
(Move to Left Subtree)
*Important *
*Important*
The Farey series of order N, denoted by NF , is the set of all reduced fractions between 0
and 1 whose denominators are N or less, arranged in increasing order. For example, if
6N  we have 6
0 1 1 1 1 2 1 3 2 3 4 5 1
, , , , , , , , , , , , .
1 6 5 4 3 5 2 5 3 4 5 6 1
F 
We can obtain NF in general by starting with 1
0 1
,
1 1
F  and then inserting mediants
whenever it’s possible to do so. To obtain NF from 1NF  , we simply insert the fraction
( ) /m m N between consecutive fractions / and /m n m n  of 1NF  whose denominators
sum equals to N. For example, it’s easy to obtain 7F from the elements of 6F by inserting
1 2 6
, , ,
7 7 7
 according to the stated rule:
7
0 1 1 1 1 2 1 2 3 1 4 3 2 5 3 4 5 6 1
, , , , , , , , , , , , , , , , , , .
1 7 6 5 4 7 3 5 7 2 7 5 3 7 4 5 6 7 1
F 
When N is prime, 1N  new fractions will appear, but otherwise we will have fewer than
1N  factors, because this process generates only numerators that are relatively prime to
N. NF is a subtree of the Stern-Brocot tree, obtained by pruning off unwanted branches.
It follows that 1m n mn   whenever / and /m n m n  are consecutive elements of a
Farey series.
Let’s used the letter L and R to stand for going down to the left or right branch as we
proceed from the root of the Stern-Brocot tree to a particular fraction; then a string of L’s
and R’s uniquely identifies a place in the tree. For instance, LRRL means that we go left
from
1
1
down to
1
2
, then right to
2
3
, then right to
3
4
, then left to
5
7
. We can consider
LRRL to represent
5
7
. Every positive fraction gets represented in this way as a unique
string of L’s and R’s.
Suppose, we are given a string of L’s and R’s, we have to find out what fraction
corresponds to it in a Stern-Brocot tree. For example, f (LRRL) =
5
7
. We can maintain a
2 2 matrix to find out such fractions.
( )
n n
M S
m m
 
   
A step to left replaces n by n n and m by m m ; hence
1 1 1 1
( ) ( )
0 1 0 1
n n n n n
M SL M S
m m m m m
       
               
Similarly, when we turn right then, we replace n by n n and m by m m .
1 0 1 0
( ) ( )
1 1 1 1
n n n n n
M SR M S
m m m m m
        
                
Therefore, we can define L and R as 2 2 matrices.
1 1 1 0
and
0 1 1 1
L R
   
    
   
For example,
1 1 1 0 1 0 1 1
( )
0 1 1 1 1 1 0 1
M LRRL LRRL
    
      
    
2 1 1 1 3 4
1 1 1 2 2 3
    
     
    
The ancestral fraction that enclose LRRL =
2 3 5
3 4 7



.
Consider, we are given positive integers m and n with m _|_ n, we have to find out the
string of L’s and R’s that corresponds to /m n in Stern-Brocot tree. We can do it using
‘binary search’ on Stern-Brocot tree:
:S I
while / ( )m n f S do
if / ( )m n f S then (output(L); :S SL )
else (output(R); R SR ) .
This outputs the desired string of L’s and R’s. For example, if given / 5/ 7m n  , then the
algorithm works according to the following way:
Pass 1 : m/n = 5/7 < f(S) = 1, Output : L.
1 1 0 1 1
0 1 1 1 2
L
  
   
 
Pass 2 : m/n = 5/7 > f(L) = 1/2 , Output : LR.
1 1 1 0 2 1 1 1 2
0 1 1 1 1 1 2 1 3
LR
     
       
    
Pass 3 : m/n = 5/7 > f(LR) = 2/3 , Output : LRR.
2 1 1 0 3 1 2 1 3
1 1 1 1 2 1 3 1 4
LRR
     
       
    
Pass 4 : m/n = 5/7 < f(LRR) = 3/4 , Output : LRRL.
3 1 1 1 3 4 2 3 5
2 1 0 1 2 3 3 4 7
LRRL
     
       
    
.
There is another algorithm available to find out L’s and R’s of particular fraction in
Stern-Brocot tree using following property
( ) ( ), where
( ) ( ), where
m m n
f RS f S m n
n n
m m
f LS f S m n
n n m

   
   

Determine the fraction corresponding to the sequence: LRRL without drawing the Stern Brocot tree
read the
Algorithm
of the
NEXT PAGE
i.e. we can transform the binary search algorithm to the following matrix-free procedure.
while m n do
if m < n then (output(L); :n n m  )
else (output(R); :m m n  )
For instance, given m/n = 5/7, we have successively
Pass 1 Pass 2 Pass 3 Pass 4
m = 5 5 5 – 2 = 3 3 – 2 = 1 1
n = 7 7 – 5 = 2 2 2 2 – 1 = 1
output : L R R L
☺Good Luck☺
Write an Algorithm to generate the the L-R sequence to locate a given Fractional value in the Stern-Brocot tree.
Demonstrate the algorithm using an example.
*include these 2 lines in the algo ==> if (gcd(m,n) > 1) then output ("Not Present in Tree, because m and n are not relatively prime"); return;
else if (m=n==1) output ("Found at Root!"); return;
Q: Determine the level of the Stern Brocot tree that contains the fraction 5/7
Ans: Find the LR sequence as above: LRRL ... this means level no. = 4+1 = 5

Number theory

  • 1.
    Number Theory Primes: Apositive integer p is called prime if it has just two divisors, namely 1 and p. By convention, 1 is not prime, so the sequence of primes starts out like this: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, Primes are of great importance, because they are the fundamental building blocks of all the positive integers. Any positive integer n can be written as a product of primes, 1 2 1 m m k k n p p p p     ,where 1 2 mp p p    (1) For example, 12 2 2 3 ; 11011 7 11 11 13 ; 11111 41 271         Here the products denoted by  are analogous to sums denoted by  . There is only one way to write n as a product of primes in non-decreasing order. This statement is called the Fundamental Theorem of Arithmetic. We can prove the Fundamental Theorem of Arithmetic by contradiction. Suppose, we have two factorization of an integer number, n 1 2 1 2m nn p p p q q q   , 1 2 1 2andm np p p q q q       where p’s and q’s are all prime. We will prove that 1 1p q . If not, we can assume that 1 1p q , making 1p smaller than all q’s. Since 1p and 1q are prime their gcd must be 1. Hence, Euclid’s self-certifying algorithm gives us integer a and b such that 1 1 1 2 3 1 2 3 2 3 1 2 3 2 3 1 2 3 1 2 3 2 3 1 2 3 2 3 2 3 1 ( ) n n n n n n m n n m n ap bq ap q q q bq q q q q q q ap q q q bn q q q ap q q q bp p p p q q q p aq q q bp p p q q q                          Since 1p divides left hand side, hence 1p should divide right hand side 2 3 nq q q . But 1p is not divisible by 2 3 nq q q according to our assumption. Thus 1p and 1q must be equal. Similarly we can show that, 2 2 3 3, , , m np q p q p q   . Every positive integer can be written uniquely in the form pn p n p  where each 0pn   (2) Formula (2) represents n uniquely, so we can think of the sequence 2 3 5( , , , )n n n  as a number system for positive integers. For example, prime exponent representation of 12 is (2 , 1 , 0 , 0 , . . .) and the prime exponent representation of 18 is (1 , 2 , 0 , 0 , . . .). To multiply two number, we simply add their representations. In other words, for allp p pk mn k m n p     (3) This implies that for allp pm n m n p   (4) and it follows immediately that, p1 can't divide ... np is the power of the prime p in the unique prime factorization of the integer n example: 100 = 2^2 * 3^0 * 5^2 * 7^0 * 11^0 * ... 352 = 2^5 * 3^0 * 5^0 * 7^0 * 11^1 * ... 11 = 1 * 11 43 = 1 * 43 for prime integers: *** MUST SEE THIS PROOF *** GCD (p1, q1) = 1 because both p1, q1 primes Euclid's theorem: The GCD of two numbers can always be written as a linear combination of the two numbers ==> so, ap1 + bq1 = 1 for integers a, b uniquely END Start
  • 2.
    gcd( , )min( , ) for allp p pk m n k m n p    (5) ( , ) max( , ) for allp p pk lcm m n k m n p    (6) For example, since 2 1 12 2 3  and 1 2 18 2 3  , we can get their gcd and lcm by taking the min and max of common exponents: min(2,1) min(1,2) 1 1 gcd(12,18) 2 3 2 3 6     max(2,1) max(1,2) 2 2 (12,18) 2 3 2 3 36lcm      Prime Examples: Euclid proved that, there are infinitely many primes. The proof is as follows: Suppose, there are only finitely many primes, say k of them 2,3,5, , kp . Then we should consider the number, 2 3 5 1kM p      . None of k primes can divide M, because each divides M-1. Thus there must be some other prime that divides M; perhaps M itself is a prime. This contradicts our assumption that, 2,3,5, , kp are the only primes, so there must be infinitely many prime. Euclid Number: 1 2 1 1n ne e e e   , when 1n   (7) The sequence starts out 1 2 3 1 1 2 2 1 3 2 3 1 7 e e e           4 2 3 7 1 43e      these are all prime. But the next case, 5e is 1807 13 139  . However, Euclid numbers are all relatively prime to each other; that is gcd( , ) gcd(1, ) gcd(0,1) 1m n me e e   , where m n . Recurrence (7) can be simplified by removing three dots. If 1n  , we have 2 1 2 2 1 1 1 1 11 ( 1) 1 1n n n n n n ne e e e e e e e e             The numbers of the form 2 1p  (where p is a prime), is called Mersenne numbers. The Mersenne prime known occur for p  2,3,5,7,13,17,19,31,61,89,107,127,521,607,1279, 2203,2281,3217,4253,4423,9689,9941,11213,19937,21701,23209,44497,86243,110503, 132049,216091,756839 and 859433. The number 2 1n  cannot possibly prime if n is not prime, because 2 1km  has 2 1m  as a factor. We can prove it by following way: 2 3 ( 1) 2 1 1 2 2 2 2 2 1 km m m m k m m           Left hand side of the equation is integer, which refers to right hand side is also an integer. That means, 2 1km  is divisible by 2 1m  . Thus 2 1km  is not a prime number. But, 2 1p  is not always a prime when p is prime. For example, 11 2 1 2047 23 89    is the smallest such nonprime. e0 = 1 and e1=e0+1=2 .... now compute e2, e3, e4, so on ... Q: Write the Prime Exponent Representation of GCD(m,n), LCM(m,n) and PRODUCT(m,n). Also, Provide Concrete Numeric Example. Q: What do you understand by Euclid number/ Mersenne number? Explain with Concrete examples. e2 = e0*e1 + 1 = 1*2+1=3 e3 = e0*e1*e2+1=1.2.3 + 1 = 7 e4 = 1.2.3.7 + 1 = 43 en = e0 * e1 * e2 * ... ... ... en-1 + 1 Short Q: Prove or Disprove that Euclid number is always Prime: Ans: Disprove by a counter example (e5 = 1807 = 13*139) Short Q: Prove or Disprove that Mersenne number is always Prime: Ans: Disprove by Counter example (p=11 => 2^11 - 1 = 2047 = 23 *89) e5 = 1.2.3.7.43 + 1 = 1807 Prove that: Any integer of the form 2^p - 1 can never be a prime number, given that p is a composite number.
  • 3.
    Number Theory Factorial Factors:The factorials: 1 ! 1 2 n k n n k        , integer 0n  We can find out factorial of an integer n using following recursive formula: 0! 1 ! ( 1)!n n n  , for 0n  Here are the first few values of the factorial function. n 0 1 2 3 4 5 6 7 8 9 10 !n 0 1 2 6 24 120 720 5040 40320 362880 3628800 Because of it’s exponential nature it’s very difficult to find a closed form of factorial. To approximate n! more accurately for large n we can use Stirling’s formula: ! 2 n n n n e        We would like to determine, for any given prime p, the largest power of p that divides n! ; that is we want the exponent of p in n!’s unique factorization. We denote this number by ( !)p n , and we start our investigations with the small case p = 2 and n = 10. 1 2 3 4 5 6 7 8 9 10 Powers of 2 Divisible by 2 X X X X X 105 2     Divisible by 4 X X 102 4     Divisible by 8 X 101 8     Powers of 2 0 1 0 2 0 1 0 3 0 1 8 For general n this method gives,  2 1 ! 2 4 8 2k k n n n n n                              This sum is actually finite, since the summand is zero when 2k n . Therefore it has only lg n   nonzero terms, and it’s computationally quite easy. For instance, if n = 100,  2 100 100 100 100 100 100 100! 50 25 12 6 3 1 97 2 4 8 16 32 64                                                 Let’s look at the binary representation of all numbers to find out what’s going on. 2 2 2 2 100 (1100100) 100 100 (110010) 50 2 100 (11001) 25 4 100 (1100) 12 8                     2 2 2 100 (110) 6 16 100 (11) 3 32 100 (1) 1 64                   Find e2(10!) = 8
  • 4.
    We merely dropthe least significant bit from one term to get the next. The binary representation shows us how to derive another formula, 2 2( !) ( )n n v n   , where 2 ( )v n is the number of 1’s in the binary representation of n. Generalizing our findings to an arbitrary prime p, we have 2 3 1 ( !)p k k n n n n n p p p p                                We can find out the upper bound of ( !)p n by simply removing the floor from the summand and then summing an infinite geometric progression. 2 3 2 1 1 ( !) 1p n n n n n p p p p p p                 1 1 1 1 1 ;[ <1] 1 1 1 n p p p n p p p n p p p n p                           For example, when p = 2 and n = 100, this inequality says that 97 < 100. Thus the upper bound 100 is not only correct but also close to the true value 97. ☺Good Luck☺ Prove that ...
  • 5.
    Number Theory Relative Primality:When gcd( , ) 1m n  , the integers m and n have no prime factors in common and we say that they are relatively prime. m _|_ n  m, n are integers and gcd( , ) 1m n  A fraction /m n is in lowest terms if and only if m _|_ n. Since we reduce fractions to lowest terms by casting out common factor of numerator and denominator, / gcd( , )m m n _|_ / gcd( , )n m n . There is a beautiful way to construct the set of all nonnegative fractions /m n with m _|_ n, called Stern-Brocot tree. The idea is to start with the two fractions 0 1 , 1 0       and then to repeat the following operation as many times as desired: Insert m m n n   between two adjacent fractions m n and m n   . The new fraction m m n n   is called mediant of m n and m n   . For example, the first step gives us one new entry. 0 1 1 , , 1 1 0 and the next gives two more: 0 1 1 2 1 , , , , 1 2 1 1 0 . The next gives four more: 0 1 1 2 1 3 2 3 1 , , , , , , , , 1 3 2 3 1 2 1 1 0 and then we will get 8, 16 and so on. The entire array can be regarded as an infinite binary tree structure whose top levels look like this: 0 1 1 0 1 1 1 2 2 1 1 2 3 3 3 2 3 1 1 2 3 3 4 5 5 4 4 5 5 4 3 3 2 1 1 2 3 3 4 5 5 4 5 7 5 7 8 7 7 8 7 5 4 5 8 7 7 8 7 5 5 4 3 3 2 1 Level 0 Level 1 from 1 to 1 Level 2 from 1/2 to 2/1 Level 3 from 1/3 to 3/1 Level 4 from 1/4 to 4 Level 5 from 1/5 to 5 Level 1 a a Level 2 Level 3 (Or, m and n are Relatively Prime)
  • 6.
    If /m nand /m n  are consecutive fractions at any stage of the construction, we have 1m n mn   . We can prove it by induction. Basis: Initially, 0 1 and 1 0 m m n n     . Thus 1 1 0 0 1m n mn       Hypothesis: Let, 1m n mn   is true for two consecutive fractions / and /m n m n  at any stage of the Stern-Brocot tree. Induction: Consider, we have new mediant ( ) / ( )m m n n   between / and /m n m n . ( ) ( ) 1 ( ) ( ) 1 (Proved) m m n m n n mn m n mn mn m n mn m n n m m n m n m n mn m n m n mn                                     If m m n n    and if all values are nonnegative, it’s easy to verify that m m m m n n n n       . 1 ( ) ( ) ( ) m m m mn m n mn mn m n mn n n n n n n n n n n n n                     . Again, 1 ( ) ( ) ( ) m m m m n m n mn m n m n mn n n n n n n n n n n n n                              . Though 0n  and 0n  . we can write, 1 1 0 and 0 ( ) ( )n n n n n n       which concludes the prove m m m m n n n n       . Is there any positive fraction /a b with a _|_ b possibly omitted from Stern-Brocot tree? Let, /a b is a positive fraction. Thus, 0 1 1 0 m a m n b n          . The construction forms ( ) / ( )m m n n   and there are three cases. Either ( ) / ( ) /m m n n a b    and we win; or ( ) / ( ) /m m n n a b    and we can set ,m m m n n n     ; or( ) / ( ) /m m n n a b    and we can set ,m m m n n n       . The process can’t go infinitely because of the conditions 0 0 (1) and 0 0 (2) a m m a an bm bm an b n n b                .  (1) ( ) (2) ( ) ( ) ( )( ) ( ) ( ) ( )( ) m n m n m n an bm m n bm an m n m n am n bmm ann bmn bmm amn bm n ann m n m n am n bmn amn bm n m n m n m n a b mn a b m n m n a b m n mn m n m n a b m                                                                                  ; [ 1]n m n m n mn      Either m or n or morn increases at each step, so we must win after at most (a + b) steps. m/n, m+m'/n+n', m'/n' Proof 1: Proof 2: Proof 3: m/n < a/b < m'/n' and the inequality gradually comes closer and closer to equality to a/b as SKIP the Calculation (i.e., Move to the Right SubTree) (Move to Left Subtree) *Important * *Important*
  • 7.
    The Farey seriesof order N, denoted by NF , is the set of all reduced fractions between 0 and 1 whose denominators are N or less, arranged in increasing order. For example, if 6N  we have 6 0 1 1 1 1 2 1 3 2 3 4 5 1 , , , , , , , , , , , , . 1 6 5 4 3 5 2 5 3 4 5 6 1 F  We can obtain NF in general by starting with 1 0 1 , 1 1 F  and then inserting mediants whenever it’s possible to do so. To obtain NF from 1NF  , we simply insert the fraction ( ) /m m N between consecutive fractions / and /m n m n  of 1NF  whose denominators sum equals to N. For example, it’s easy to obtain 7F from the elements of 6F by inserting 1 2 6 , , , 7 7 7  according to the stated rule: 7 0 1 1 1 1 2 1 2 3 1 4 3 2 5 3 4 5 6 1 , , , , , , , , , , , , , , , , , , . 1 7 6 5 4 7 3 5 7 2 7 5 3 7 4 5 6 7 1 F  When N is prime, 1N  new fractions will appear, but otherwise we will have fewer than 1N  factors, because this process generates only numerators that are relatively prime to N. NF is a subtree of the Stern-Brocot tree, obtained by pruning off unwanted branches. It follows that 1m n mn   whenever / and /m n m n  are consecutive elements of a Farey series. Let’s used the letter L and R to stand for going down to the left or right branch as we proceed from the root of the Stern-Brocot tree to a particular fraction; then a string of L’s and R’s uniquely identifies a place in the tree. For instance, LRRL means that we go left from 1 1 down to 1 2 , then right to 2 3 , then right to 3 4 , then left to 5 7 . We can consider LRRL to represent 5 7 . Every positive fraction gets represented in this way as a unique string of L’s and R’s. Suppose, we are given a string of L’s and R’s, we have to find out what fraction corresponds to it in a Stern-Brocot tree. For example, f (LRRL) = 5 7 . We can maintain a 2 2 matrix to find out such fractions. ( ) n n M S m m       A step to left replaces n by n n and m by m m ; hence 1 1 1 1 ( ) ( ) 0 1 0 1 n n n n n M SL M S m m m m m                         Similarly, when we turn right then, we replace n by n n and m by m m . 1 0 1 0 ( ) ( ) 1 1 1 1 n n n n n M SR M S m m m m m                          
  • 8.
    Therefore, we candefine L and R as 2 2 matrices. 1 1 1 0 and 0 1 1 1 L R              For example, 1 1 1 0 1 0 1 1 ( ) 0 1 1 1 1 1 0 1 M LRRL LRRL                  2 1 1 1 3 4 1 1 1 2 2 3                 The ancestral fraction that enclose LRRL = 2 3 5 3 4 7    . Consider, we are given positive integers m and n with m _|_ n, we have to find out the string of L’s and R’s that corresponds to /m n in Stern-Brocot tree. We can do it using ‘binary search’ on Stern-Brocot tree: :S I while / ( )m n f S do if / ( )m n f S then (output(L); :S SL ) else (output(R); R SR ) . This outputs the desired string of L’s and R’s. For example, if given / 5/ 7m n  , then the algorithm works according to the following way: Pass 1 : m/n = 5/7 < f(S) = 1, Output : L. 1 1 0 1 1 0 1 1 1 2 L          Pass 2 : m/n = 5/7 > f(L) = 1/2 , Output : LR. 1 1 1 0 2 1 1 1 2 0 1 1 1 1 1 2 1 3 LR                    Pass 3 : m/n = 5/7 > f(LR) = 2/3 , Output : LRR. 2 1 1 0 3 1 2 1 3 1 1 1 1 2 1 3 1 4 LRR                    Pass 4 : m/n = 5/7 < f(LRR) = 3/4 , Output : LRRL. 3 1 1 1 3 4 2 3 5 2 1 0 1 2 3 3 4 7 LRRL                    . There is another algorithm available to find out L’s and R’s of particular fraction in Stern-Brocot tree using following property ( ) ( ), where ( ) ( ), where m m n f RS f S m n n n m m f LS f S m n n n m           Determine the fraction corresponding to the sequence: LRRL without drawing the Stern Brocot tree read the Algorithm of the NEXT PAGE
  • 9.
    i.e. we cantransform the binary search algorithm to the following matrix-free procedure. while m n do if m < n then (output(L); :n n m  ) else (output(R); :m m n  ) For instance, given m/n = 5/7, we have successively Pass 1 Pass 2 Pass 3 Pass 4 m = 5 5 5 – 2 = 3 3 – 2 = 1 1 n = 7 7 – 5 = 2 2 2 2 – 1 = 1 output : L R R L ☺Good Luck☺ Write an Algorithm to generate the the L-R sequence to locate a given Fractional value in the Stern-Brocot tree. Demonstrate the algorithm using an example. *include these 2 lines in the algo ==> if (gcd(m,n) > 1) then output ("Not Present in Tree, because m and n are not relatively prime"); return; else if (m=n==1) output ("Found at Root!"); return; Q: Determine the level of the Stern Brocot tree that contains the fraction 5/7 Ans: Find the LR sequence as above: LRRL ... this means level no. = 4+1 = 5