1 ) Algorithm to compute square root that handles perfect and non-perfect squares. 2) Reverse a LinkedList
Anonymous
package com.coding.tests; public class SquareRoot { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int X = 625; int Y = 771; System.out.println(sqrt(X)); System.out.println(sqrt(Y)); } //sqrt(S): Xn+1=(Xn+S/Xn)/2; public static int sqrt(int X){ if(X==0) return 0; boolean isNegtive=X<0?true:false; int S = Math.abs(X); int Xn = X/2; int preXn=Xn; do{ preXn=Xn; Xn=(Xn+S/Xn)/2; }while((Xn-preXn)!=0); return isNegtive?0-Xn:Xn; } }
Check out your Company Bowl for anonymous work chats.