Advertisement
Guest User

Untitled

a guest
Jan 26th, 2015
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.05 KB | None | 0 0
  1.  
  2. public class Assignemnt1 {
  3.  
  4.     static long cntr=1;
  5.     static long[][]a1={{1},{0}};
  6.     static long[][]auxMat={{1,1},{1,0}};
  7.     static long[][]b1 = new long[2][2];
  8.     private static long startTime, endTime, elapsedTime;
  9.     private static int phase = 0;
  10.     public static long RecFib(long n,int p) // 2 raise to power n ::time complexity
  11.     {
  12.         long ans;
  13.         if(n%p<2) return(n%p);
  14.         else
  15.         return RecFib((n-1)%p,p)+RecFib((n-2)%p,p);
  16. }
  17.    
  18.    
  19.    
  20.      public static long FibDP(long fib)
  21.      {
  22.          long []a = {0,1};
  23.          
  24.          for (int i=2;i<=fib;i++)
  25.          {
  26.             Long ans =( a[0])+(a[1]);
  27.             a[0]=a[1];
  28.             a[1]=ans;
  29.              
  30.          }
  31.          return(a[1]);
  32.          
  33.      }
  34.    
  35.      public static long RecFibMatrix(long fib,int p)// to get O(n)
  36.         {
  37.        
  38.         if(fib<2) return fib;
  39.         else
  40.             calculatePower(fib,p);
  41.            
  42.         return (b1[0][0]);
  43.         }
  44.    
  45. public static void calculatePower(long fib,int p)
  46.     {
  47.    
  48.     while(cntr<fib)
  49.     {
  50.         multiplyMatrix(p);
  51.         cntr++;
  52.    
  53.     }
  54.     }
  55.  
  56.         public static long RecFibMatrixDAC(long fib,int p)// to get O(logn)
  57. {
  58.  
  59.             if(fib<2) return fib;
  60.             else
  61.                 calculatePower(fib,p);
  62.    
  63.             return ((b1[0][0]*a1[0][0])+(b1[1][0]*a1[1][0])%p);
  64. }
  65.    
  66.     public static void calculatePowerDAC(long fib,int p)
  67.     {
  68.    
  69.     while(cntr<(fib/2)+1)
  70.     {
  71.         multiplyMatrix(p);
  72.         cntr++;
  73.    
  74.     }
  75.  
  76.    
  77.    
  78.     }
  79.    
  80.  
  81.     public static void multiplyMatrix(int p)
  82.     {
  83.         b1[0][0]=(auxMat[0][0]*a1[0][0]+auxMat[0][1]*a1[1][0])%p;
  84.         b1[1][0]=(auxMat[1][0]*a1[0][0]+auxMat[1][1]*a1[1][0])%p;
  85.        
  86.         a1[0][0]=b1[0][0];
  87.         a1[1][0]=b1[1][0];
  88.  
  89.      
  90.     }
  91.    
  92.  
  93.     public static void timer()
  94.     {
  95.         if(phase == 0) {
  96.         startTime = System.currentTimeMillis();
  97.         phase = 1;
  98.     } else {
  99.         endTime = System.currentTimeMillis();
  100.             elapsedTime = endTime-startTime;
  101.             System.out.println("Time: " + elapsedTime + " msec.");
  102.             //memory();
  103.             phase = 0;
  104.         }
  105.     }
  106.    
  107.    
  108.     public static void main(String[] args) {
  109.         // TODO Auto-generated method stub
  110.      timer();
  111.     long ans=RecFibMatrixDAC(1234567890,997);
  112.     System.out.println(ans);
  113.     timer();
  114.     }
  115.  
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement