/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package bs_tree_test; /** * * @author suchenek */ public class MyMath { public static int s2(int n) { int toReturn = 0; int m = n; //a working copy for (int i = 0; i <= floorLg(n);i++) { toReturn += m%2; m /= 2; } return toReturn; } public static int e2(int n) { int m = n; //a working copy for (int toReturn = 0; toReturn <= floorLg(n);toReturn++) { if(m%2 != 0) return toReturn; m /= 2; } return -1; } public static int floorLg(int n) { int power2 = 1; for (int toReturn = 0; toReturn <= Math.log(n)/Math.log(2);toReturn++) { if (2*power2 > n) return toReturn; power2 *= 2; } return -1; } public static int floorLog(int n) { int power10 = 1; for (int toReturn = 0; toReturn <= Math.log10(n);toReturn++) { if (10*power10 > n) return toReturn; power10 *= 10; } return -1; } public static int twoToFloorLg(int n) { int power2 = 1; for (int toReturn = 0; toReturn <= Math.log(n)/Math.log(2);toReturn++) { if (2*power2 > n) return power2; power2 *= 2; } return -1; } public static int ceilLg(int n) { int power2 = 1; for (int toReturn = 0; toReturn <= Math.log(n)/Math.log(2)+1;toReturn++) { if (power2 >= n) return toReturn; power2 *= 2; } return -1; } public static int ceilLog(int n) { int power10 = 1; for (int toReturn = 0; toReturn <= Math.log10(n)+1;toReturn++) { if (power10 >= n) return toReturn; power10 *= 10; } return -1; } public static int twoToCeilLg(int n) { int power2 = 1; for (int toReturn = 0; toReturn <= Math.log(n)/Math.log(2)+1;toReturn++) { if (power2 >= n) return power2; power2 *= 2; } return -1; } public static int twoTo(int n) { int power2 = 1; for (int i = 1; i <= n; i++) { power2 *= 2; } return power2; } public static int c0(int n) { if (n<9) return 0; if ((twoToFloorLg(n) + 1 <= n) && (n < twoToFloorLg(n) + 5)) return 1; return 0; } public static int c1(int n) { if (n<9) return 0; if ((twoToFloorLg(n) + 1 <= n) && (n < twoToCeilLg(n) - 3)) return 1; return 0; } public static int c(int n) { if (n<9) return 0; if ((twoToFloorLg(n) + 1 <= n) && (n < 2*twoToFloorLg(n) - 3)) return 1; return 0; } public static int cAbstr(int n) // as defined in the Abstract { if (n<9) return 0; if ((n < twoToCeilLg(n) - 3)) return 1; return 0; } public static void main(String[] args) { for (int i = 1; i<35; i++) { System.out.print("n = " + i); System.out.print(" s2 = " + s2(i)); System.out.print(" e2 = " + e2(i)); System.out.print(" floorLg = " + floorLg(i)); System.out.print(" floorLog = " + floorLog(i)); System.out.print(" twoToFloorLg = " + twoToFloorLg(i)); System.out.print(" ceilLg = " + ceilLg(i)); System.out.print(" ceilLog = " + ceilLog(i)); System.out.print(" twoToCeilLg = " + twoToCeilLg(i)); System.out.print(" c0 = " + c0(i)); System.out.print(" c1 = " + c1(i)); System.out.println(); } } }