Feigenbaum constant calculation: Difference between revisions
Content deleted Content added
Scala added |
|||
Line 1,050: | Line 1,050: | ||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
<lang ring> |
<lang ring># Project : Feigenbaum constant calculation |
||
⚫ | |||
decimals(8) |
decimals(8) |
||
Line 1,081: | Line 1,080: | ||
a2 = a1 |
a2 = a1 |
||
a1 = a |
a1 = a |
||
next |
next</lang> |
||
⚫ | |||
Output: |
Output: |
||
⚫ | |||
<pre> |
|||
Feigenbaum constant calculation: |
|||
i d |
i d |
||
2 3.21851142 |
2 3.21851142 |
||
Line 1,098: | Line 1,095: | ||
11 4.66920026 |
11 4.66920026 |
||
12 4.66920098 |
12 4.66920098 |
||
13 4.66920537 |
13 4.66920537</pre> |
||
=={{header|Scala}}== |
|||
</pre> |
|||
===Imperative, ugly=== |
|||
<lang Scala>object Feigenbaum1 extends App { |
|||
val (max_it, max_it_j) = (13, 10) |
|||
var (a1, a2, d1, a) = (1.0, 0.0, 3.2, 0.0) |
|||
println(" i d") |
|||
var i: Int = 2 |
|||
while (i <= max_it) { |
|||
a = a1 + (a1 - a2) / d1 |
|||
for (_ <- 0 until max_it_j) { |
|||
var (x, y) = (0.0, 0.0) |
|||
for (_ <- 0 until 1 << i) { |
|||
y = 1.0 - 2.0 * y * x |
|||
x = a - x * x |
|||
} |
|||
a -= x / y |
|||
} |
|||
val d: Double = (a1 - a2) / (a - a1) |
|||
printf("%2d %.8f\n", i, d) |
|||
d1 = d |
|||
a2 = a1 |
|||
a1 = a |
|||
i += 1 |
|||
} |
|||
⚫ | |||
===Functional Style, Tail recursive=== |
|||
{{Out}}Best seen running in your browser either by [https://scalafiddle.io/sf/OjA3sae/0 ScalaFiddle (ES aka JavaScript, non JVM)] or [https://scastie.scala-lang.org/04eS3BfCShmrA7I8ZmQfJA Scastie (remote JVM)]. |
|||
<lang Scala>object Feigenbaum2 extends App { |
|||
private val (max_it, max_it_j) = (13, 10) |
|||
private def result = { |
|||
@scala.annotation.tailrec |
|||
def outer(i: Int, d1: Double, a2: Double, a1: Double, acc: Seq[Double]): Seq[Double] = { |
|||
@scala.annotation.tailrec |
|||
def center(j: Int, a: Double): Double = { |
|||
@scala.annotation.tailrec |
|||
def inner(k: Int, end: Int, x: Double, y: Double): (Double, Double) = |
|||
if (k < end) inner(k + 1, end, a - x * x, 1.0 - 2.0 * y * x) else (x, y) |
|||
val (x, y) = inner(0, 1 << i, 0.0, 0.0) |
|||
if (j < max_it_j) { |
|||
center(j + 1, a - (x / y)) |
|||
} else a |
|||
} |
|||
if (i <= max_it) { |
|||
val a = center(0, a1 + (a1 - a2) / d1) |
|||
val d: Double = (a1 - a2) / (a - a1) |
|||
outer(i + 1, d, a1, a, acc :+ d) |
|||
} else acc |
|||
} |
|||
outer(2, 3.2, 0, 1.0, Seq[Double]()).zipWithIndex |
|||
} |
|||
println(" i ≈ δ") |
|||
result.foreach { case (δ, i) => println(f"${i + 2}%2d $δ%.8f") } |
|||
}</lang> |
|||
=={{header|Sidef}}== |
=={{header|Sidef}}== |