Search for multiple IRRs
////////////////////////////////

irr_f3<-function(cashflows,bigV=100000){
   smallV<-1e-5
   a<--1+smallV
   rates<-seq(a,1,smallV)
   sign1<-sign(npv_f(a,cashflows))
   #
   for(r in rates){
      		  npv<-npv_f(r,cashflows)
        sign2<-sign(npv)
        if(sign1+sign2==0){
             cat("irr =",r,"\n")
             sign1<-sign2
        }
   }
}

# We can use the following cashflows to test the R code. 
cashflows<-c(-17,16,16,16,16,-52)*1000
irr_f3(cashflows)
irr = 0.06767 
irr = 0.6536

///////////////////////////////

present value for perpetuity 
////////////////////////////////
The definition of perpetuity: 
     1) the same cash flows 
     2) at the same interval 
     3) forever.
 Example 1:
           c       c      c        c   -> infinity 
   |-------|-------|-------|   ... |
   0       1       2       3           -> infinity 

 Example 2:                c       c   -> infinity 
   |-------|-------|-------|   ... |
   0       1       2       3           -> infinity 

 Example 3:
                                   c   -> infinity 
   |-------|-------|-------|   ... |
   0       1       2       3       20   -> infinity 

  When the first cash flow occurs at the end of the first period, we have the following formula 
                    c  
  pv(perpetuity) = --  
                    r  

     where c is the constant cash flow and r is the discount rate.
////////////////////////////////

Payback period and its rule
////////////////////////////////

The payback period is the number of years it takes to recover 
    the initial investment. The Payback Rule Period is based 
    on the following principle.

Payback Period Rule:
   if   T < Tc    -> accept
        T > Tc    -> reject 

T is an estimated payback period, and 
   Tc is an acceptable minimum payback period. 

///////////////////////////////

pv(perpetuity): several ways to estimate 
////////////////////////////////
Method I:  pv(perpetuity)= c/r
              where c is the constant cash flow
                    r is the discount rate
Method II: 
---------
     Write a loop to apply pv_f<-function(f,r,n)fv/(1+r)^n
         to estimate pv(perpetuity)

  a) a for()  loop
  b) a while() loop
  c) a repeat() loop
  d) when the first cash flow happens at the 10th period 

////////////////////////////////