Project Euler #245: Coresilience

  • + 0 comments

    import scala.math.BigInt

    object ProjectEuler245Coresilience { val eulerTotient:BigInt => BigInt = n => { var result: BigInt = n var i: BigInt = 2 var tmp: BigInt = n while (i * i <= tmp) { if (tmp % i == 0) { while (tmp % i == 0) { tmp /= i } result -= result / i } i = i + 1 } if (tmp > 1) result -= result / tmp result }

    val coresilience: BigInt => BigInt = n =>{ a= n-eulerTotient(n) (a / a.gcd(n-1)) }

    val sumOfUnitFractions:BigInt => BigInt = n => { var sumOf: BigInt = 0 for (i: BigInt <- BigInt(2) to n) { if (coresilience(i) == 1) { sumOf += i } } sumOf }

    var a: BigInt = 0

    def main(args: Array[String]): Unit = { val stdin = scala.io.StdIn val test = stdin.readInt println(sumOfUnitFractions(test)) } }

    Here is my solution with Scala and I couldn't go further optimization. Any help??