Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Collatz on Google Cloud Functions - Java 17 #9

Open
obriensystems opened this issue Nov 6, 2022 · 0 comments
Open

Collatz on Google Cloud Functions - Java 17 #9

obriensystems opened this issue Nov 6, 2022 · 0 comments
Assignees

Comments

@obriensystems
Copy link
Member

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

@obriensystems obriensystems self-assigned this Nov 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant