lambdaway
::
fibonacci
2
|
list
|
login
|
load
|
|
_h1 fibonacci _p The definition {pre '{def fib {lambda {:n} {if {< :n 2} then 1 else {+ {fib {- :n 1}} {fib {- :n 2}}}}}} -> {def fib {lambda {:n} {if {< :n 2} then 1 else {+ {fib {- :n 1}} {fib {- :n 2}}}}}} } _p The first Fibonacci's numbers {pre '{S.map fib {S.serie 0 12}} -> {S.map fib {S.serie 0 12}} } _p The golden rectangle as a limit of spiraling squares whose sides are the sequence of Fibonacci's numbers. {pre '{def GR {def GR.square {lambda {:d} M:d T90 M:d T90 M:d T90 M:d T90 M:d T90 M:d}} {def GR.spiral {lambda {:d :n} {if {< :n 0} then else {GR.square :d} {GR.spiral {fib :n} {- :n 1}} }}} {lambda {:n} {GR.spiral 0 :n} }} -> {def GR {def GR.square {lambda {:d} M:d T90 M:d T90 M:d T90 M:d T90 M:d T90 M:d}} {def GR.spiral {lambda {:d :n} {if {< :n 0} then else {GR.square :d} {GR.spiral {fib :n} {- :n 1}} }}} {lambda {:n} {GR.spiral 0 :n} }} } _p And we display the whole in an SVG frame. {pre '{svg {@ width="600px" height="710px"} {polyline {@ points="{turtle 100 700 0 {GR 13}}" stroke="#fff" fill="#444"}} } } {svg {@ width="600px" height="710px"} {polyline {@ points="{turtle 100 650 0 {GR 13}}" stroke="#fff" fill="#444"}} } _h2 another approach {uncover http://lambdaway.fr/modulor/data/Animation_Fibonacci.gif 600 600 An animated gif showing a hinged golden rectangle} _h2 lambdatalk version {svg {@ width="580px" height="580px" style="transform:rotate(-90deg);box-shadow:0 0 8px #000;"} {circle {@ cx="333" cy="333" r="203" fill="transparent" stroke="#888" stroke-width="10"}} {polyline {@ id="gold" points="{squares 100 100 45}" fill="transparent" stroke="#fff" stroke-width="1"}} {polyline {@ points="{C}" fill="transparent" stroke="#f00" stroke-width="1"}} {polyline {@ points="{C2}" fill="transparent" stroke="#ff0" stroke-width="1"}} {polyline {@ points="{C3}" fill="transparent" stroke="#0ff" stroke-width="1"}} } _p The locus of the end of the hinged golden rectangle appears to be a circle. Prove it and find its centre and radius. _h2 code {pre '{def square {lambda {:a :d} M:d T90 M:d T90 M:d T90 M:d T90 M:d T90 M:d T:a}} -> {def square {lambda {:a :d} M:d T90 M:d T90 M:d T90 M:d T90 M:d T90 M:d T:a}} '{def F {A.new F 1 1 2 3 5 8 13 21 34 55 89 144 233}} -> {def F {A.new F 1 1 2 3 5 8 13 21 34 55 89 144 233}} '{def squares {lambda {:x :y :a} {turtle :x :y 0 {S.map {{lambda {:a :i} {square -:a {A.get :i {F}}} } :a} {S.serie 12 1 -1}} }}} -> {def squares {lambda {:x :y :a} {turtle :x :y 0 {S.map {{lambda {:a :i} {square -:a {A.get :i {F}}} } :a} {S.serie 12 1 -1}} }}} '{def V {A.new {S.map {lambda {:a} {A.new {squares 100 100 {* 5 :a}}}} {S.serie 0 72}}}} -> {def V {A.new {S.map {lambda {:a} {A.new {squares 100 100 {* 5 :a}}}} {S.serie 0 72}}}} '{svg {@ width="580px" height="580px" style="box-shadow:0 0 8px #000;"} {polyline {@ id="gold" points="" fill="#f00" stroke="#fff" stroke-width="1"}} } '{script var i = 0; var anim = function() { var str = LAMBDATALK.eval_forms( "{A.disp {A.get " + i + " {V}}}" ); var val = str.replace( /,/g, " ").replace( /(\[|\])/g, ""); document.getElementById("gold").setAttribute("points", val); i++; if (i > 71) i=0; }; setTimeout( function () { setInterval( anim, 200 ) }, 100) } } _h2 red circle _p A polyline built on extremities of the hinged golden rect. {prewrap '{A.length {V}} -> {A.length {V}} '{A.length {A.get 0 {V}}} -> {A.length {A.get 0 {V}}} '{def C {S.map {lambda {:i} {A.get 144 {A.get :i {V}}} {A.get 145 {A.get :i {V}}}} {S.serie 0 72}}} -> {def C {S.map {lambda {:i} {A.get 144 {A.get :i {V}}} {A.get 145 {A.get :i {V}}}} {S.serie 0 72}}} '{C} {def C2 {S.map {lambda {:i} {A.get 40 {A.get :i {V}}} {A.get 41 {A.get :i {V}}}} {S.serie 0 72}}} {def C3 {S.map {lambda {:i} {A.get 28 {A.get :i {V}}} {A.get 29 {A.get :i {V}}}} {S.serie 0 72}}} } _h2 the fibonacci code _p Her we forget the recursive definition and use a loop _p 1) definition: {pre f(0) = 0 f(1) = 1 f(n) = f(n-1)+f(n-2) } _p 2) trace: {pre 1: [ 0, 1] 2: [ 1, 1] 3: [ 1, 2] 4: [ 2, 3] 5: [ 3, 5] 6: [ 5, 8] 7: [ 8,13] 8: [13,21] 9: [21,34] 10: [34,55] ... } _p 3) code {pre '{def ifib {lambda {:n} {S.reduce {{lambda {:a :i} {A.set! 0 {A.last :a} // a[0] = a[1] {A.set! 1 {long_add {A.first :a} {A.last :a}} :a} // a[1] = a[0]+a[1] }} {A.new 0 1}} // a = [0,1] {S.serie 1 :n} }}} -> {def ifib {lambda {:n} {S.reduce {{lambda {:a :i} {A.set! 0 {A.last :a} {A.set! 1 {long_add {A.first :a} {A.last :a}} :a} }} {A.new 0 1}} {S.serie 1 :n} }}} } {prewrap '{S.map ifib {S.serie 2 10}} -> {S.map ifib {S.serie 2 10}} '{A.last {ifib 1000}} -> {A.last {ifib 1000}} } _p See also _ul Fibonacci, the golden section and √5 are everywhere _ul20 [[workshop/?view=fibonacci|http://lambdaway.free.fr/workshop/?view=fibonacci]] _ul20 [[lambdaspeech/?view=fibonacci|http://lambdaway.free.fr/lambdaspeech/?view=fibonacci]] _ul and even in the Mandelbrot set _ul20 [[lambdawalks/?view=mandelbrot|http://lambdaway.free.fr/lambdawalks/?view=mandelbrot]] _ul golden pairs: _ul20 [[1|https://www.researchgate.net/profile/Robert-Schneider-26/publication/233387710_A_Golden_Product_Identity_for_e/links/00b49532786af69e04000000/A-Golden-Product-Identity-for-e.pdf?origin=publication_detail]] _ul20 [[2|https://www.ms.uky.edu/~mathclub/Posters/AY_11_12/Schneider.pdf]] {iframe {@ width="580" height="315" src="https://www.youtube.com/embed/e7SnRPubg-g?si=yIHEqO5GkIFyqV9q" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen}} {script var i = 0; var anim = function() { var str = LAMBDATALK.eval_forms( "{A.disp {A.get " + i + " {V}}}" ); var val = str.replace( /,/g, " ").replace( /(\[|\])/g, ""); document.getElementById("gold").setAttribute("points", val); i++; if (i > 71) i=0; }; setTimeout( function () { setInterval( anim, 200 ) }, 100) } {style body { background:#444; } #page_frame { border:0; } #page_content { background:transparent; color:#fff; border:0;} .page_menu { background:transparent; color:#fff;} a {color:red} }
lambdaway v.20211111