Skip to content

Collatz on Google Cloud Functions - Java 17 #9

@obriensystems

Description

@obriensystems

see:
https://console.cloud.google.com/functions/details/us-central1/eventstream-in?env=gen2&project=eventstream-dev

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);

  
 }
}

Screen Shot 2022-11-05 at 22 47 18

Screen Shot 2022-11-05 at 22 46 49

Screen Shot 2022-11-05 at 22 52 33

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions