Skip to content

Commit 0cf10c4

Browse files
authored
Add files via upload
1 parent 04a423d commit 0cf10c4

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package ch_10;
2+
3+
import java.math.BigDecimal;
4+
import java.math.BigInteger;
5+
import java.util.Arrays;
6+
7+
/**
8+
* 10.17 (Square numbers) Find the first ten square numbers that are greater than
9+
* Long.MAX_VALUE. A square number is a number in the form of n2. For example, 4, 9, and 16 are square numbers.
10+
* Find an efficient approach to run your program fast.
11+
*/
12+
public class Exercise10_17 {
13+
public static void main(String[] args) {
14+
BigInteger maxLongVal = BigInteger.valueOf(Long.MAX_VALUE);
15+
int count = 0;
16+
long rootNum = (long) Math.sqrt(maxLongVal.doubleValue());
17+
BigInteger root = BigInteger.valueOf(rootNum);
18+
BigInteger[] result = new BigInteger[10];
19+
while (count < 10) {
20+
root = root.add(BigInteger.ONE);
21+
BigInteger n2 = root.pow(2);
22+
if (n2.compareTo(maxLongVal) > 0) {
23+
result[count] = n2;
24+
count++;
25+
}
26+
27+
}
28+
System.out.println("Long.MAX_VALUE = " + Long.MAX_VALUE);
29+
System.out.println("The square numbers greater than Long.MAX_VALUE are: ");
30+
System.out.println(Arrays.toString(result));
31+
32+
33+
}
34+
}

0 commit comments

Comments
 (0)