hpsaturn

NDK Example FibLib

Jan 22nd, 2013
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.71 KB | None | 0 0
  1. public class FibLib {
  2.  
  3.   // Java implementation - recursive
  4.   public static int fibJ(int n) {
  5.     if (n <= 0)
  6.       return 0;
  7.     if (n == 1)
  8.       return 1;
  9.     return fibJ(n - 1) + fibJ(n - 2);
  10.   }
  11.  
  12.   // Java implementation - iterative
  13.   public static int fibJI(int n) {
  14.     int previous = -1;
  15.     int result = 1;
  16.     for (int i = 0; i <= n; i++) {
  17.       int sum = result + previous;
  18.       previous = result;
  19.       result = sum;
  20.     }
  21.     return result;
  22.   }
  23.  
  24.   // Native implementation
  25.   static {
  26.     System.loadLibrary("fib");
  27.   }
  28.  
  29.   // Native implementation - recursive
  30.   public static native int fibN(int n);
  31.  
  32.   // Native implementation - iterative
  33.   public static native int fibNI(int n);
  34. }
Advertisement
Add Comment
Please, Sign In to add comment