-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoogle-1.java
68 lines (60 loc) · 1.63 KB
/
google-1.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
package com.google.challenges;
import java.util.ArrayList;
import java.util.List;
public class Answer {
public static int[] answer(int area) {
List<Integer> myList = new ArrayList<Integer>();
int total = 0;//Number goes up.
int max = area;//Number goes down.
while(total != area){
if(isPerfectSquare(max)) {
myList.add(max);
total = total+max;
max = area - total;
} else {
max = --max;
}
}
//TODO: Turn into a method
int size = myList.size();
int[] Arr = new int[size];
for(int i=0;i<myList.size();i++){
Arr[i] = myList.get(i);
}
return Arr;
}
//Only method I needed to cheat on...for performance reasons.
private final static boolean isPerfectSquare(long n)
{
if (n < 0)
return false;
switch((int)(n & 0x3F))
{
case 0x00: case 0x01: case 0x04: case 0x09: case 0x10: case 0x11:
case 0x19: case 0x21: case 0x24: case 0x29: case 0x31: case 0x39:
long sqrt;
if(n < 410881L)
{
//John Carmack hack, converted to Java.
// See: http://www.codemaestro.com/reviews/9
int i;
float x2, y;
x2 = n * 0.5F;
y = n;
i = Float.floatToRawIntBits(y);
i = 0x5f3759df - ( i >> 1 );
y = Float.intBitsToFloat(i);
y = y * ( 1.5F - ( x2 * y * y ) );
sqrt = (long)(1.0F/y);
}
else
{
//Carmack hack gives incorrect answer for n >= 410881.
sqrt = (long)Math.sqrt(n);
}
return sqrt*sqrt == n;
default:
return false;
}
}
}