Big number square root

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 3663 Views - Last Post: 05 August 2009 - 04:47 PM Rate Topic: -----

#1 dejandenib  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 10-June 09

Big number square root

Posted 04 August 2009 - 05:59 PM

I have a program in Java that calculates squareroot of hune numbers. Can somebody help me to change the input and output of the program.It should be standard input and output like this:
Input
3
6
2
5
Output
2
2
5
At the moment, the input is inserted with String testNums[] = {"625", n.toString ()}; and the output is
625
Initial guess 10
Next guess 36.2500000000
Next guess 26.7456896552
Next guess 25.0569705327
Next guess 25.0000647653
Next guess 25.0000000001
Next guess 25.0000000000
Next guess 25.0000000000
Thanks in advance

import java.math.*;
public class BigSquareRoot {

	/**
	 * @param args
	 */
	
	 private static BigDecimal ZERO = new BigDecimal ("0");
	  private static BigDecimal ONE = new BigDecimal ("1");
	  private static BigDecimal TWO = new BigDecimal ("2");
	  public static final int DEFAULT_MAX_ITERATIONS = 50;
	  public static final int DEFAULT_SCALE = 10;

	  private BigDecimal error;
	  private int iterations;
	  private boolean traceFlag;
	  private int scale = DEFAULT_SCALE;
	  private int maxIterations = DEFAULT_MAX_ITERATIONS;

	  //---------------------------------------
	  // The error is the original number minus
	  // (sqrt * sqrt). If the original number
	  // was a perfect square, the error is 0.
	  //---------------------------------------

	  public BigDecimal getError () {
		return error;
	  }

	  //-------------------------------------------------------------
	  // Number of iterations performed when square root was computed
	  //-------------------------------------------------------------

	  public int getIterations () {
		return iterations;
	  }

	  //-----------
	  // Trace flag
	  //-----------

	  public boolean getTraceFlag () {
		return traceFlag;
	  }

	  public void setTraceFlag (boolean flag) {
		traceFlag = flag;
	  }

	  //------
	  // Scale
	  //------

	  public int getScale () {
		return scale;
	  }

	  public void setScale (int scale) {
		this.scale = scale;
	  }

	  //-------------------
	  // Maximum iterations
	  //-------------------

	  public int getMaxIterations () {
		return maxIterations;
	  }

	  public void setMaxIterations (int maxIterations) {
		this.maxIterations = maxIterations;
	  }

	  //--------------------------
	  // Get initial approximation
	  //--------------------------

	  private static BigDecimal getInitialApproximation (BigDecimal n) {
		BigInteger integerPart = n.toBigInteger ();
		int length = integerPart.toString ().length ();
		if ((length % 2) == 0) {
		  length--;
		}
		length /= 2;
		BigDecimal guess = ONE.movePointRight (length);
		return guess;
	  }

	  //----------------
	  // Get square root
	  //----------------

	  public BigDecimal get (BigInteger n) {
		return get (new BigDecimal (n));
	  }

	  public BigDecimal get (BigDecimal n) {

		// Make sure n is a positive number

		if (n.compareTo (ZERO) <= 0) {
		  throw new IllegalArgumentException ();
		}

		BigDecimal initialGuess = getInitialApproximation (n);
		trace ("Initial guess " + initialGuess.toString ());
		BigDecimal lastGuess = ZERO;
		BigDecimal guess = new BigDecimal (initialGuess.toString ());

		// Iterate

		iterations = 0;
		boolean more = true;
		while (more) {
		  lastGuess = guess;
		  guess = n.divide(guess, scale, BigDecimal.ROUND_HALF_UP);
		  guess = guess.add(lastGuess);
		  guess = guess.divide (TWO, scale, BigDecimal.ROUND_HALF_UP);
		  trace ("Next guess " + guess.toString ());
		  error = n.subtract (guess.multiply (guess));
		  if (++iterations >= maxIterations) {
			more = false;
		  }
		  else if (lastGuess.equals (guess)) {
			more = error.abs ().compareTo (ONE) >= 0;
		  }
		}
	  /////  String zbor = guess;
		return guess;

	  }

	  //------
	  // Trace
	  //------

	  private void trace (String s) {
		if (traceFlag) {
		  System.out.println (s);
		}
	  }

	  //----------------------
	  // Get random BigInteger
	  //----------------------

	  public static BigInteger getRandomBigInteger (int nDigits) {
		StringBuffer sb = new StringBuffer ();
		java.util.Random r = new java.util.Random ();
		for (int i = 0; i < nDigits; i++) {
		  sb.append (r.nextInt (10));
		}
		return new BigInteger (sb.toString ());
	  }

	  //-----
	  // Test
	  //-----
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		 BigInteger n;
			BigDecimal sqrt;
			BigSquareRoot app = new BigSquareRoot ();
			app.setTraceFlag (true);

			// Generate a random big integer with a hundred digits

			n = BigSquareRoot.getRandomBigInteger (1);

			// Build an array of test numbers

			String testNums[] = {"625", n.toString ()};

			for (int i = 0; i < 1; i++) {
			  n = new BigInteger (testNums[i]);
			  if (i > 0) {
				System.out.println ("----------------------------");
			  }
			  System.out.println ("Computing the square root of");
			  System.out.println (n.toString ());
			  int length = n.toString ().length ();
			  if (length > 20) {
				app.setScale (length / 2);
			  }
			 sqrt = app.get (n);
			 // System.out.println ("Iterations " + app.getIterations ());
			 // System.out.println ("Sqrt " + sqrt.toString ());
		   //   System.out.println (sqrt.multiply (sqrt).toString ());
		   //   System.out.println (n.toString ());
			//  System.out.println ("Error " + app.getError ().toString ());
			}
	}

}



Is This A Good Question/Topic? 0
  • +

Replies To: Big number square root

#2 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Big number square root

Posted 04 August 2009 - 06:01 PM

hmm i dont quite follow...what's wrong with your input/output?
Was This Post Helpful? 0
  • +
  • -

#3 dejandenib  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 10-June 09

Re: Big number square root

Posted 04 August 2009 - 06:09 PM

Well it's not wrong, but i don't want it to be like that, i want it to be standard input and output
Was This Post Helpful? 0
  • +
  • -

#4 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Big number square root

Posted 04 August 2009 - 06:31 PM

you mean to take input from the user?

check out the Scanner class.. it also has a method to input BigIntegers so it will make things easier for you
Was This Post Helpful? 0
  • +
  • -

#5 dejandenib  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 10-June 09

Re: Big number square root

Posted 05 August 2009 - 02:14 AM

But i'm beginner at Java. I don't know what is Scanner class or where to find it. Even if i knew this, i wouldn't know how to implement it
Was This Post Helpful? 0
  • +
  • -

#6 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Big number square root

Posted 05 August 2009 - 05:31 AM

Scanner sc = new Scanner(System.in);
BigInteger bi = sc.nextBigInteger();



and at the top of the class import the class by
import java.util.*;
Was This Post Helpful? 0
  • +
  • -

#7 dejandenib  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 10-June 09

Re: Big number square root

Posted 05 August 2009 - 06:36 AM

I don't understand this. I think this is not completely. And what about the output?
Was This Post Helpful? 0
  • +
  • -

#8 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Big number square root

Posted 05 August 2009 - 06:57 AM

what about it??..just print out the results using the System.out.println() statement
Was This Post Helpful? 0
  • +
  • -

#9 dejandenib  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 10-June 09

Re: Big number square root

Posted 05 August 2009 - 07:52 AM

OK, i managed to do the output, but can you help me with the input?
Was This Post Helpful? 0
  • +
  • -

#10 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Big number square root

Posted 05 August 2009 - 07:59 AM

 public static void main(String[] args) {
		// TODO Auto-generated method stub
		 BigInteger n;
			BigDecimal sqrt;
		Scanner sc = new Scanner(System.in);//creates a scanner object
			BigSquareRoot app = new BigSquareRoot ();
			app.setTraceFlag (true);

			// Generate a random big integer with a hundred digits

			n = BigSquareRoot.getRandomBigInteger (1);

			// Build an array of test numbers
		System.out.println("how many numbers do you want to test?");
		int x = sc.nextInt();

			for (int i = 0; i < x; i++) {
			  n = sc.nextBigInteger();//inputs a bigint on each iteration
			  if (i > 0) {
				System.out.println ("----------------------------");
			  }
			  System.out.println ("Computing the square root of");
			  System.out.println (n.toString ());
			  int length = n.toString ().length ();
			  if (length > 20) {
				app.setScale (length / 2);
			  }
			 sqrt = app.get (n);
			 // System.out.println ("Iterations " + app.getIterations ());
			 // System.out.println ("Sqrt " + sqrt.toString ());
		   //   System.out.println (sqrt.multiply (sqrt).toString ());
		   //   System.out.println (n.toString ());
			//  System.out.println ("Error " + app.getError ().toString ());
			}
	}


there you go
Was This Post Helpful? 1
  • +
  • -

#11 dejandenib  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 10-June 09

Re: Big number square root

Posted 05 August 2009 - 01:24 PM

Thanks!Thanks! Thanks! Thanks! Thanks!!!!!!!!!!!!!!
Was This Post Helpful? 0
  • +
  • -

#13 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Big number square root

Posted 05 August 2009 - 02:15 PM

no problem :)..now click the "this post was helpful link" :P
Was This Post Helpful? 1
  • +
  • -

#14 dejandenib  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 10-June 09

Re: Big number square root

Posted 05 August 2009 - 02:38 PM

And one more thing. With this input if i read 3 then it will calculate square root for 6, then for 2 and then for 5. But i want it to calculate for 625. And the input to be
3
6
2
5
Now the input is
1
625 if i want to get an answer 25.
How do i do that?
Was This Post Helpful? 0
  • +
  • -

#15 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Big number square root

Posted 05 August 2009 - 02:52 PM

i just tested it and it works fine
how many numbers do you want to test?
1  
625
Computing the square root of
625
Initial guess 10
Next guess 36.2500000000
Next guess 26.7456896552
Next guess 25.0569705327
Next guess 25.0000647653
Next guess 25.0000000001
Next guess 25.0000000000
Next guess 25.0000000000


i inputted 625 and this was the output
Was This Post Helpful? 0
  • +
  • -

#16 dejandenib  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 10-June 09

Re: Big number square root

Posted 05 August 2009 - 03:02 PM

I managed to do the output. It's not anymore like that, but i wan't the input to be
3
6
2
5
not
1
625
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2