Wednesday, April 7, 2010

Nice Mathematical Operations

In this post we will share some nice mathematical operations, these operations exist in all languages we will show the logic behind them or work-around to get the same result for any reason.
The common thing when you work on mobile headset and it is still old and not supporting float calculations, how to avoid max loss of precision especially in case of sequential operations.

(1) Square Root:
//Newton’s method for finding the root of a function
private static double square_root(double a){
double x=0 , xold=0 ;
x = a;
xold = x ;
System.out.println(x) ;
} while(x!=xold);
return x;

(2) Simple Round Function:
This is the logic behind the round function, add 0.5 to the fraction number and take the int value.
//simple round function using float to int.
private static int round(float s){
int x=new Double(s+0.5).intValue();
System.out.println("Round results="+x) ;
return x;

(3) Get Maximum Precision when no Float operations supported:
This is done simple by multiplying 1 number into predefined number according to the needed precision, then do the multiplication/division operation then divide the results again by this value to get the max possible precision.

private static int noFloatCalculation(int x,int y){
System.out.println("No Float results b4="+x/y) ;
int percision=100;
int s=(x*percision)/(y);
System.out.println("No Float results="+s) ;
return s;

(4) Get the Greatest Common Divisor for 2 numbers:(Euclid’s)
//Euclid’s Greatest Common Divisor (GCD)
private static int calculateGCD(int a, int b){
if(a>b) a=a-b;
else b=b-a;
System.out.println( "GCD="+a ) ; // or b since a=b
return a;

(5) Cumulative sum from 0 to x:
//cumulative sum from 0 to the send number
private static int caluclateSumFromZero(int end){
int i,start=0;
int cumulLoop=0;
for ( i=start; i<end ; i++) {
cumulLoop+=i ;
int cumul=(end*(end-1))/2;
System.out.println( cumulLoop+" closed -form:"+cumul);
return cumul;

(6) Calculating Factorial of a number:
//using recursive call
public static int factorial(int n){
if(n==0) return 1;
else return n*factorial(n-1);

(7) Selection Sort Algorithm:
To sort array of int , loop over them and swap the smaller one till you sort the array completely.

//loop sequential replace current with the the smallest one (if current is not the smallest)
//using 2 nested loops
static void SelectionSort(int[] array) {
int n=array.length;
for ( int i =0; i<n-1; i++){
for ( int j=i+1; j<n ; j++){
//swap them
int temp=array[i];

For String objects you do the same but you need to use the java built in compareTo() method to compare the 2 Strings, that return
the value 0 if the argument string is equal to this string; a value less than 0 if this string is lexicographically less than the string argument; and a value greater than 0 if this string is lexicographically greater than the string argument

//for String objects ...
static void SelectionSort(String[] array) {
int n=array.length;
for ( int i =0; i<n-1; i++){
for ( int j=i+1; j<n ; j++){
//swap them
String temp=array[i];

(8) Convert Decimal format into integer format:
To convert number formatted like 12.45 into 1245/100 , We simple multiply the number with the number of digits after the decimal point then we can get the integer division format...

Now what if we have round number like 15.23232323...... and 23 is repeated forever ?
We do the following simple steps:
x = 15.23232323..... (23 is repeated forever)
100x=1523.23232323...... (get 1 of the repeated sequence to the left of decimal point)
now subtract x from 100x --> 99x = 1508.0 so x=1508/99

Another example:
If we have x=15.8567567567567..... (567 is repeated forever)
1st 10x=158.567567567..... (get rid of the un-repeated part)
(get 1 of the repeated sequence to the left of decimal point)
now subtract 10x from 10000x --> 9990x=158409.0 , so x = 158409/9990.

Easy , right ?

No comments:

Post a Comment