class IntegerWU {

    public static void main( String args[] ) {
        for( int accuracy = 1; accuracy<8; accuracy++ )
        //accuracy = 8 gives array of 100 000 000 of int32 = 400Mbytes
        //which gives java.lang.OutOfMemoryError: Java heap space ...
        //in our 1 Gig RAM PC.
        {  
           long start = System.currentTimeMillis();        
           String s = InitializeISqrt( accuracy );
           if( s == "" ){
               System.out.println( "Total Duration=" + ( System.currentTimeMillis() - start) + " milliseconds."  );
               System.out.println( "Duration per array element =" + 
                                   ( 1000000. * ( System.currentTimeMillis() - start)) / ISqrt.length  +
                                   " nanoseconds." );
               ISQRT(20000);
               System.out.println( "sqrt(20000)="+ ISignificand + " * 10^-" + IExponent);
               ISQRT(2000000);
               System.out.println( "sqrt(2000000)="+ ISignificand + " * 10^-" + IExponent + "\r\n\r\n\r\n");
           } else {
               System.out.println( s );
           }
        }           
    }    

    //To not exceede limts of int32:
    private static final int SAFE_DIGITS = 8;  
    private static int ROOTS_ARRAY_SIZE;
    //Redundant digits which exceed accuracy restricted by ACCURACY:
    private static int BONUS_DIGITS;          
    private static int BONUS_MULTIPLIER;
    //Auxiliary array to store precalculated powers of 10:
    private static int POWERES_OF_TEN[] = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000 };
    //Primary array where precalculated roots will be stored:
	private static int[] ISqrt;              
    

    //ACCURACY value 3 should provide sqrt(10^3) accuracy ~ 3% accuracy. 
    //ACCURACY value 4 should provide sqrt(10^4) accuracy ~ 1% accuracy. 
    //         In application to IntegerWU algorithm to find Intensity*ceil(sqrt(RR-yy)),
    //         this will give 100 grades of shade which should be good.
    //ACCURACY value 5 should provide sqrt(10^5) accuracy ~ .3% accuracy. 
	public static String InitializeISqrt(int ACCURACY) {
        System.out.println( "ACCURACY=" + ACCURACY + ". SAFE DIGITS=" + SAFE_DIGITS );
        if( ACCURACY < 3 ) {
            return "ACCURACY is too small: ACCURACY < " +  3;
        }
        int w = 2 * SAFE_DIGITS - ACCURACY;
        BONUS_DIGITS = (w - (w%2)) / 2;
        if( BONUS_DIGITS < 0 ) { 
            return "ACCURACY is too big: ACCURACY > " +  (2 * SAFE_DIGITS);
        }
        ROOTS_ARRAY_SIZE = POWERES_OF_TEN[ACCURACY];
        BONUS_MULTIPLIER = POWERES_OF_TEN[BONUS_DIGITS];
        System.out.println( "BONUS_DIGITS=" + BONUS_DIGITS + ". ROOTS_ARRAY_SIZE=" + ROOTS_ARRAY_SIZE + ". BONUS_MULTIPLIER=" + BONUS_MULTIPLIER);
        ISqrt = new int[ ROOTS_ARRAY_SIZE ];
    	for( int i=0; i<ROOTS_ARRAY_SIZE; i++ ){
	       ISqrt[i] = (int) (  Math.sqrt((double)i)  *  BONUS_MULTIPLIER  );
        }		 
	    return "";
    }

    //Calculating square root represented by integer numbers.
    //Input x>=0.
    //Output:  ISignificand * 10^IExponent
    //         where
    public static int IExponent;
    public static int ISignificand; 
	public static void ISQRT( int x ) {
       int shift=0;
       while ( x<ROOTS_ARRAY_SIZE ) {
           x = x*100;
           shift -=1;
       }
       while ( x>ROOTS_ARRAY_SIZE ) {
           x = x/100;
           shift +=1;
       }
       IExponent    = BONUS_DIGITS-shift;
       ISignificand = ISqrt[x];
    }

    
    /*    
	//This function replaces (double) D(R,y).
    //It returns Intensity in range 1-unit.
	private static int ID( int R, int y, int FullIntensity ) {
    
      int shift=0;
      while ( R>RANGE ) {
          R = R/100;
          shift -=1;
      }
      int sqrt = ISqrt[ R*R - y*y ] ;
          return = unit - sqrt % unit;
      }
      else
      {   //For R>50 we expand squrt to the series sqrt(1-x) ~ 1-x/2
          //Because 
          // dD/dx 
          //ID = ceil(R( 1 - yy/2R )) - R(1-yy/2R) = R(1-yy/(2R) mod 1.
          //In units: 
          return  ACCURACY - ACCURACY*y/R*y/(2*R);
      }
      
	}
	*/
	
} 


Copyright_C_2008_Landkey_Computers.txt