-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path_11461_SquareNumbers.java
76 lines (61 loc) · 1.78 KB
/
_11461_SquareNumbers.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package io.github.tahanima.uva;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
/**
* @author tahanima
*/
public class _11461_SquareNumbers {
static final int MAX = 100000;
static ArrayList<Integer> squareNumbers = new ArrayList<>();
public static void computeSquareNumbers() {
int num = 1;
while (num * num <= MAX) {
squareNumbers.add(num * num);
num++;
}
}
public static int lowerBound(int n) {
int index = Collections.binarySearch(squareNumbers, n);
if (index < 0) {
return Math.abs(index) - 1;
}
while (index > 0) {
if (squareNumbers.get(index - 1) == n) {
index--;
} else {
return index;
}
}
return index;
}
public static int upperBound(int n) {
int index = Collections.binarySearch(squareNumbers, n);
if (index < 0) {
return Math.abs(index) - 1;
}
int size = squareNumbers.size();
while (index < size - 2) {
if (squareNumbers.get(index + 1) == n) {
index++;
} else {
return index + 1;
}
}
return index + 1;
}
public static void main(String[] args) {
computeSquareNumbers();
Scanner scanner = new Scanner(System.in);
StringBuilder stringBuilder = new StringBuilder();
while (scanner.hasNext()) {
int a = scanner.nextInt();
int b = scanner.nextInt();
if (a + b == 0) {
break;
}
stringBuilder.append(String.format("%d%n", upperBound(b) - lowerBound(a)));
}
System.out.print(stringBuilder);
}
}