Friday, November 8, 2013

Kotlin on problem 26

Kotlin is a language that compiles to both the JVM and javascript. It seems like a fairly reasonable language, using some features that seem similar to Scala, but overall retaining a very Java-like syntax. I had some difficulty installing Kotlin (aka my hard drive does not have enough free space for it), so I only used the free online in-browser version...as such, Kotlin would not have worked very well for the problem I am up to, problem 59, which requires file I/O. So instead, I used Kotlin to rewrite my solution to problem 26 that was in C#, and C# will be used for a solution to 59.
import java.util.Collections.*
fun biggestCycle(d: Int): Int {
  val rems: Array< int > = Array< int >(1000){i -> 0}
  var r: Int = 1
  var i: Int = 0
  while (r != 0) {
    r = (r * 10) % d
    for (j in 0..i) {
      if (rems[j] == r) {
        return i
      }
    }
    rems[i++] = r
  }
  return i
}
  
  
fun main(args : Array< string >) {
  var ans = 0
  for (d in 1..1000) {
    if (ans < biggestCycle(d)) {
      ans = d
    } 
  }
  println(ans)
}

No comments:

Post a Comment