Feigenbaum constant calculation

From Rosetta Code
Feigenbaum constant calculation is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.


Calculate the Feigenbaum constant. See details: Feigenbaum constant



C[edit]

Translation of: Ring
#include <stdio.h>
 
void feigenbaum() {
int i, j, k, max_it = 13, max_it_j = 10;
double a, x, y, d, a1 = 1.0, a2 = 0.0, d1 = 3.2;
printf(" i d\n");
for (i = 2; i <= max_it; ++i) {
a = a1 + (a1 - a2) / d1;
for (j = 1; j <= max_it_j; ++j) {
x = 0.0;
y = 0.0;
for (k = 1; k <= 1 << i; ++k) {
y = 1.0 - 2.0 * y * x;
x = a - x * x;
}
a -= x / y;
}
d = (a1 - a2) / (a - a1);
printf("%2d  %.8f\n", i, d);
d1 = d;
a2 = a1;
a1 = a;
}
}
 
int main() {
feigenbaum();
return 0;
}
Output:
 i       d
 2    3.21851142
 3    4.38567760
 4    4.60094928
 5    4.65513050
 6    4.66611195
 7    4.66854858
 8    4.66906066
 9    4.66917155
10    4.66919515
11    4.66920026
12    4.66920098
13    4.66920537

Go[edit]

Translation of: Ring
package main
 
import "fmt"
 
func feigenbaum() {
maxIt, maxItJ := 13, 10
a1, a2, d1 := 1.0, 0.0, 3.2
fmt.Println(" i d")
for i := 2; i <= maxIt; i++ {
a := a1 + (a1-a2)/d1
for j := 1; j <= maxItJ; j++ {
x, y := 0.0, 0.0
for k := 1; k <= 1<<uint(i); k++ {
y = 1.0 - 2.0*y*x
x = a - x*x
}
a -= x / y
}
d := (a1 - a2) / (a - a1)
fmt.Printf("%2d  %.8f\n", i, d)
d1, a2, a1 = d, a1, a
}
}
 
func main() {
feigenbaum()
}
Output:
 i       d
 2    3.21851142
 3    4.38567760
 4    4.60094928
 5    4.65513050
 6    4.66611195
 7    4.66854858
 8    4.66906066
 9    4.66917155
10    4.66919515
11    4.66920026
12    4.66920098
13    4.66920537

Kotlin[edit]

Translation of: Ring
// Version 1.2.40
 
fun feigenbaum() {
val maxIt = 13
val maxItJ = 10
var a1 = 1.0
var a2 = 0.0
var d1 = 3.2
println(" i d")
for (i in 2..maxIt) {
var a = a1 + (a1 - a2) / d1
for (j in 1..maxItJ) {
var x = 0.0
var y = 0.0
for (k in 1..(1 shl i)) {
y = 1.0 - 2.0 * y * x
x = a - x * x
}
a -= x / y
}
val d = (a1 - a2) / (a - a1)
println("%2d  %.8f".format(i,d))
d1 = d
a2 = a1
a1 = a
}
}
 
fun main(args: Array<String>) {
feigenbaum()
}
Output:
 i       d
 2    3.21851142
 3    4.38567760
 4    4.60094928
 5    4.65513050
 6    4.66611195
 7    4.66854858
 8    4.66906066
 9    4.66917155
10    4.66919515
11    4.66920026
12    4.66920098
13    4.66920537

Modula-2[edit]

MODULE Feigenbaum;
FROM FormatString IMPORT FormatString;
FROM LongStr IMPORT RealToStr;
FROM Terminal IMPORT WriteString,WriteLn,ReadChar;
 
VAR
buf : ARRAY[0..63] OF CHAR;
i,j,k,max_it,max_it_j : INTEGER;
a,x,y,d,a1,a2,d1 : LONGREAL;
BEGIN
max_it := 13;
max_it_j := 10;
 
a1 := 1.0;
a2 := 0.0;
d1 := 3.2;
 
WriteString(" i d");
WriteLn;
FOR i:=2 TO max_it DO
a := a1 + (a1 - a2) / d1;
FOR j:=1 TO max_it_j DO
x := 0.0;
y := 0.0;
FOR k:=1 TO INT(1 SHL i) DO
y := 1.0 - 2.0 * y * x;
x := a - x * x
END;
a := a - x / y
END;
d := (a1 - a2) / (a - a1);
FormatString("%2i ", buf, i);
WriteString(buf);
RealToStr(d, buf);
WriteString(buf);
WriteLn;
d1 := d;
a2 := a1;
a1 := a
END;
 
ReadChar
END Feigenbaum.

Perl 6[edit]

Works with: Rakudo version 2018.04.01
Translation of: Ring
my $a1 = 1;
my $a2 = 0;
my $d = 3.2;
 
say ' i d';
 
for 2 .. 13 -> $exp {
my $a = $a1 + ($a1 - $a2) / $d;
do {
my $x = 0;
my $y = 0;
for ^2 ** $exp {
$y = 1 - 2 * $y * $x;
$x = $a - $x²;
}
$a -= $x / $y;
} xx 10;
$d = ($a1 - $a2) / ($a - $a1);
($a2, $a1) = ($a1, $a);
printf "%2d %.8f\n", $exp, $d;
}
Output:
 i d
 2 3.21851142
 3 4.38567760
 4 4.60094928
 5 4.65513050
 6 4.66611195
 7 4.66854858
 8 4.66906066
 9 4.66917155
10 4.66919515
11 4.66920026
12 4.66920098
13 4.66920537

Racket[edit]

Translation of: C
#lang racket
(define (feigenbaum #:max-it (max-it 13) #:max-it-j (max-it-j 10))
(displayln " i d" (current-error-port))
(define-values (_a _a1 d)
(for/fold ((a 1) (a1 0) (d 3.2))
((i (in-range 2 (add1 max-it))))
(let* ((a′ (for/fold ((a (+ a (/ (- a a1) d))))
((j (in-range max-it-j)))
(let-values (([x y] (for/fold ((x 0) (y 0))
((k (expt 2 i)))
(values (- a (* x x))
(- 1 (* 2 y x))))))
(- a (/ x y)))))
(d′ (/ (- a a1) (- a′ a))))
(eprintf "~a ~a\n" (~a i #:width 2) (real->decimal-string d′ 8))
(values a′ a d′))))
d)
 
(module+ main
(feigenbaum))
Output:
 i       d
2    3.21851142
3    4.38567760
4    4.60094928
5    4.65513050
6    4.66611195
7    4.66854858
8    4.66906066
9    4.66917155
10   4.66919515
11   4.66920026
12   4.66920098
13   4.66920537
4.669205372040318

Ring[edit]

 
# Project : Feigenbaum constant calculation
# Date  : 2018/04/20
# Author : Gal Zsolt [~ CalmoSoft ~]
# Email  : <[email protected]>
 
decimals(8)
see "Feigenbaum constant calculation:" + nl
maxIt = 13
maxItJ = 10
a1 = 1.0
a2 = 0.0
d1 = 3.2
see "i " + "d" + nl
for i = 2 to maxIt
a = a1 + (a1 - a2) / d1
for j = 1 to maxItJ
x = 0
y = 0
for k = 1 to pow(2,i)
y = 1 - 2 * y * x
x = a - x * x
next
a = a - x / y
next
d = (a1 - a2) / (a - a1)
if i < 10
see "" + i + " " + d + nl
else
see "" + i + " " + d + nl
ok
d1 = d
a2 = a1
a1 = a
next
 

Output:

Feigenbaum constant calculation:
i  d
2  3.21851142
3  4.38567760
4  4.60094928
5  4.65513050
6  4.66611195
7  4.66854858
8  4.66906066
9  4.66917155
10 4.66919515
11 4.66920026
12 4.66920098
13 4.66920537

Sidef[edit]

Translation of: Perl 6
var a1 = 1
var a2 = 0
var δ = 3.2.float
 
say " i\tδ"
 
for i in (2..15) {
var a0 = ((a1 - a2)/δ + a1)
10.times {
var (x, y) = (0, 0)
2**i -> times {
y = (1 - 2*x*y)
x = (a0 -)
}
a0 -= x/y
}
δ = ((a1 - a2) / (a0 - a1))
(a2, a1) = (a1, a0)
printf("%2d %.8f\n", i, δ)
}
Output:
 i	δ
 2 3.21851142
 3 4.38567760
 4 4.60094928
 5 4.65513050
 6 4.66611195
 7 4.66854858
 8 4.66906066
 9 4.66917156
10 4.66919516
11 4.66920023
12 4.66920131
13 4.66920155
14 4.66920160
15 4.66920161

zkl[edit]

Translation of: Kotlin
fcn feigenbaum{
maxIt,maxItJ,a1,a2,d1,a,d := 13, 10, 1.0, 0.0, 3.2, 0, 0;
println(" i d");
foreach i in ([2..maxIt]){
a=a1 + (a1 - a2)/d1;
foreach j in ([1..maxItJ]){
x,y := 0.0, 0.0;
foreach k in ([1..(1).shiftLeft(i)]){ y,x = 1.0 - 2.0*y*x, a - x*x; }
a-=x/y
}
d=(a1 - a2)/(a - a1);
println("%2d  %.8f".fmt(i,d));
d1,a2,a1 = d,a1,a;
}
}();
Output:
 i       d
 2    3.21851142
 3    4.38567760
 4    4.60094928
 5    4.65513050
 6    4.66611195
 7    4.66854858
 8    4.66906066
 9    4.66917155
10    4.66919515
11    4.66920026
12    4.66920098
13    4.66920537