Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save takayahilton/cb85f69f8aa5ea3efd61 to your computer and use it in GitHub Desktop.
Save takayahilton/cb85f69f8aa5ea3efd61 to your computer and use it in GitHub Desktop.
https://codeiq.jp/q/1368 の問題をscalaで解きました
case class Fraction(numer:Int,denom:Int){
import scala.annotation._
@tailrec
private def gcd(a:Int,b:Int):Int= b match{
case 0 => a
case _ => gcd(b,a%b)
}
def toAnswer:Int = {
val n = numer/gcd(numer,denom)
val d = denom/gcd(numer,denom)
if(numer%denom==0){
n/d
}else{
if(numer<denom){
n + d
}else{
n/d + n%d + d
}
}
}
}
object Application extends App {
val answers = for{
i <- (1 to 2015)
if Fraction(i,27).toAnswer == 54
}yield i
val first5th = answers(4)
val last5th = answers.reverse.apply(4)
println(first5th+"\n"+last5th)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment