Hickerson series of almost integers: Difference between revisions

Scala solution added
(→‎{{header|Kotlin}}: Updated example see https://github.com/dkandalov/rosettacode-kotlin for details)
(Scala solution added)
Line 1,053:
=={{header|Kotlin}}==
{{trans|Java}}
<lang scalakotlin>// version 1.1.4
 
import java.math.BigDecimal
Line 1,846:
n: 15 h: 230283190977853.037436039 nearly integer
n: 16 h: 5315654681981354.51307674 NOT nearly integer
n: 17 h: 130370767029135900.457985 NOT nearly integer</pre>
=={{header|Scala}}==
</pre>
===Functional Programming ♫===
<lang Scala>import scala.annotation.tailrec
 
object Hickerson extends App {
 
def almostInteger(n: Int): Boolean = {
def ln2 = BigDecimal("0.69314718055994530941723212145818")
 
def div = ln2.pow(n + 1) * BigDecimal.valueOf(2)
 
def factorial(num: Int): Long = {
@tailrec
def accumulated(acc: Long, n: Long): Long =
if (n <= 0) acc else accumulated(acc * n, n - 1)
 
accumulated(1, num)
}
 
((BigDecimal(factorial(n)) / div * 10).toBigInt() % 10).toString.matches("0|9")
}
 
val aa = (1 to 17).map(n => n -> almostInteger(n))
 
println(s"Function h(n) gives a almost integer with a n of ${aa.filter(_._2).map(_._1).mkString(", ")}.")
println(s"While h(n) gives NOT an almost integer with a n of ${aa.filter(!_._2).map(_._1).mkString(", ")}.")
 
}</lang>
{{Out}}See it in running in your browser by [https://scalafiddle.io/sf/tNNk9jB/1 (JavaScript executed in browser)]
or by [https://scastie.scala-lang.org/M5VFzmIsRzyZhAHjeTkULQ Scastie (remote JVM)].
=={{header|Seed7}}==
<lang seed7>$ include "seed7_05.s7i";
Anonymous user