Skip to content

Commit

Permalink
Library, P64, P66, P100: Replaced Library.sqrt() with BigInteger.sqrt…
Browse files Browse the repository at this point in the history
…() (Java SE 9+).

Readme: Updated JDK version requirement and copyright year.
  • Loading branch information
nayuki committed Nov 23, 2023
1 parent 77fa0e4 commit 15becb0
Show file tree
Hide file tree
Showing 5 changed files with 4 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Readme.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Every solved problem has a program written in Java and usually Python. Some solu

All problems from #1 to #100 have a Java and Python program, and problems #1 to #50 have a Mathematica program. This package contains at least 205 solutions in Java, at least 200 in Python, at least 125 in Mathematica, and at least 95 in Haskell.

Java solutions require JDK 8+. Python solutions are tested to work on CPython 3.9.6. Mathematica solutions are tested to work on Mathematica 5.1.
Java solutions require JDK 9+. Python solutions are tested to work on CPython 3.9.6. Mathematica solutions are tested to work on Mathematica 5.1.

Home page with background info, table of solutions, benchmark timings, and more: [https://www.nayuki.io/page/project-euler-solutions](https://www.nayuki.io/page/project-euler-solutions)

Expand Down
14 changes: 0 additions & 14 deletions java/Library.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,6 @@ public static long sqrt(long x) {
}


// Returns floor(sqrt(x)), for x >= 0.
public static BigInteger sqrt(BigInteger x) {
if (x.signum() == -1)
throw new IllegalArgumentException("Square root of negative number");
BigInteger y = BigInteger.ZERO;
for (int i = (x.bitLength() - 1) / 2; i >= 0; i--) {
y = y.setBit(i);
if (y.multiply(y).compareTo(x) > 0)
y = y.clearBit(i);
}
return y;
}


// Tests whether x is a perfect square, for any value x.
public static boolean isSquare(int x) {
if (x < 0)
Expand Down
2 changes: 1 addition & 1 deletion java/p064.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public QuadraticSurd reciprocal() {


public BigInteger floor() {
BigInteger temp = Library.sqrt(b.multiply(b).multiply(d));
BigInteger temp = b.multiply(b).multiply(d).sqrt();
if (b.signum() == -1)
temp = temp.add(BigInteger.ONE).negate();
temp = temp.add(a);
Expand Down
2 changes: 1 addition & 1 deletion java/p066.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public QuadraticSurd reciprocal() {


public BigInteger floor() {
BigInteger temp = Library.sqrt(b.multiply(b).multiply(d));
BigInteger temp = b.multiply(b).multiply(d).sqrt();
if (b.signum() == -1)
temp = temp.add(BigInteger.ONE).negate();
temp = temp.add(a);
Expand Down
2 changes: 1 addition & 1 deletion java/p100.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public String run() {
BigInteger y = y0; // An alias for the number of red discs
while (true) {
// Check if this solution is acceptable
BigInteger sqrt = Library.sqrt(y.multiply(y).multiply(BigInteger.valueOf(8)).add(BigInteger.ONE));
BigInteger sqrt = y.multiply(y).multiply(BigInteger.valueOf(8)).add(BigInteger.ONE).sqrt();
if (sqrt.testBit(0)) { // Is odd
BigInteger blue = sqrt.add(BigInteger.ONE).divide(BigInteger.valueOf(2)).add(y);
if (blue.add(y).compareTo(BigInteger.TEN.pow(12)) > 0)
Expand Down

0 comments on commit 15becb0

Please sign in to comment.