Saturday, November 9, 2013

Adding Git to Windows Path

This is under Windows 7, but it should work in a similar way for other versions.
  • Right-click on My Computer.
  • Click Advanced System Settings.
  • Click Environment Variables.
  • Under System Variables (or User Variables for ....), select PATH and click edit.
  • Add the following to the end of the line
      ;C:\Program Files\Git\bin;C:\Program Files\Git\cmd
 
This depends of course on where you have installed Git.
Then restart your command-line window. 

Sunday, November 3, 2013

BigInteger sqrt method

Since Java doesn't have a sqrt method for BigInteger, I found this one which works fine.
It's short and practical

source: http://faruk.akgul.org/blog/javas-missing-algorithm-biginteger-sqrt/

BigInteger sqrt(BigInteger n) {
  BigInteger a = BigInteger.ONE;
  BigInteger b = new BigInteger(n.shiftRight(5).add(new BigInteger("8")).toString());
  while(b.compareTo(a) >= 0) {
    BigInteger mid = new BigInteger(a.add(b).shiftRight(1).toString());
    if(mid.multiply(mid).compareTo(n) > 0) b = mid.subtract(BigInteger.ONE);
    else a = mid.add(BigInteger.ONE);
  }
  return a.subtract(BigInteger.ONE);
}