We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
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??
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Project Euler #245: Coresilience
You are viewing a single comment's thread. Return to all 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??