-
Notifications
You must be signed in to change notification settings - Fork 0
Collatz on Google Cloud Functions - Java 17 #9
Copy link
Copy link
Open
Description
package gcfv2;
import java.io.BufferedWriter;
import java.math.BigInteger;
import com.google.cloud.functions.HttpFunction;
import com.google.cloud.functions.HttpRequest;
import com.google.cloud.functions.HttpResponse;
public class HelloHttpFunction implements HttpFunction {
public static long counter = 0;
public static final BigInteger COLLATZ_2651 = new BigInteger("2367363789863971985761");
private BigInteger path = BigInteger.ZERO;
private BigInteger height = BigInteger.ZERO;
public void compute(BigInteger start) {
}
private void computeSingleThreaded(BufferedWriter writer, BigInteger start) throws Exception {
BigInteger current = start;
long startTime = System.currentTimeMillis();
while (current.compareTo(BigInteger.ONE) > 0) {
// odd integers follow
if(current.testBit(0)) {
current = current.shiftLeft(1).add(current).add(BigInteger.ONE);
} else {
current = current.shiftRight(1);
}
path = path.add(BigInteger.ONE);
if(current.compareTo(height) > 0) {
height = current;
}
//if(displayIterations) {
//System.out.println(start + "," + current + "," + path + "," + height);
//}
}
long time = System.currentTimeMillis() - startTime;
//System.out.println("start,path,max,ms");
writer.write(start + "," + path + "," + height + "," + time);
}
public void service(final HttpRequest request, final HttpResponse response) throws Exception {
final BufferedWriter writer = response.getWriter();
counter++;
//writer.write("Count: " + counter);
BigInteger start = COLLATZ_2651;//BigInteger.valueOf(27);
computeSingleThreaded(writer, start);
}
}
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels


