Skip to content

Instantly share code, notes, and snippets.

@jagedn
Last active April 30, 2020 07:42
Show Gist options
  • Save jagedn/268826f8a00cdcf2164c1a0c38190fd2 to your computer and use it in GitHub Desktop.
Save jagedn/268826f8a00cdcf2164c1a0c38190fd2 to your computer and use it in GitHub Desktop.
kotlin collatz conjeture resolver
package com.puravida
import java.math.BigInteger
import javax.ws.rs.GET
import javax.ws.rs.Path
import javax.ws.rs.PathParam
import javax.ws.rs.Produces
import javax.ws.rs.core.MediaType
@Path("/hello")
class CollatzController {
@GET
@Path("{number}")
@Produces(MediaType.TEXT_PLAIN)
fun resolve(@PathParam("number") find : String) :String{
var current = find.toBigInteger()
var steps = 1
var zero = BigInteger.valueOf(0)
val one = BigInteger.valueOf(1)
val two = BigInteger.valueOf(2)
val three = BigInteger.valueOf(3)
while( current > one){
if( current.mod(two) == zero ){
current = current.divide(two)
}else{
current = current.multiply(three).add(one)
}
steps++
}
return "$steps"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment