lambdaway
::
erato3
6
|
list
|
login
|
load
|
|
°°° {canvas {@ id="matrix" width="580px" height="580px"}} {script var matrix = function() { const canvas = document.getElementById('matrix'); const ctx = canvas.getContext('2d'); const w = canvas.width; const h = canvas.height; const cols = Math.floor(w / 20) + 1; const ypos = Array(cols).fill(0); ctx.fillStyle = '#000'; ctx.fillRect(0, 0, w, h); var render = function() { ctx.fillStyle = '#0001'; ctx.fillRect(0, 0, w, h); ctx.fillStyle = '#0f0'; ctx.font = '15pt monospace'; ypos.forEach(function(y, x) { const text = String.fromCharCode(Math.random() * 128); ctx.fillText(text, 20*x, y); ypos[x] = (y > 100 + Math.random() * 10000) ? 0 : y + 20; }); }; //setInterval(render, 100); } //setTimeout( matrix, 1 ) } °°° _h1 [[erato]] | [[erato2]] | erato3 | [[erato4]] _p The [[Sieve of Ératosthène|https://fr.wikipedia.org/wiki/Crible_d%27Ératosthène]]. See also [[rosettacode.org|https://rosettacode.org/wiki/Sieve_of_Eratosthenes#Lambdatalk]]. _p Here is the process: _ul 1) create an array of natural numbers, [0,1,2,3, ... ,n-1] _ul 2) the 3rd number is 2, we set to dots all its composites by steps of 2, _ul 3) the 4th number is 3, we set to dots all its composites by steps of 3, _ul 4) the 6th number is 5, we set to dots all its composites by steps of 5, _ul 5) the remaining numbers are primes and we clean all dots. _p For instance: {pre 1: 0 0 0 0 0 0 0 0 0 9 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 2: 0 1 {{circ} 2} 3 . 5 . 7 . 9 . 1 . 3 . 5 . 7 . 9 . 1 . 3 . 5 . 7 . 9 . _ _ _ _ _ _ _ _ _ 3: 0 1 2 {{circ} 3} . 5 . 7 . . . 1 . 3 . . . 7 . 9 . . . 3 . 5 . . . 9 . _ _ _ _ _ _ 4: 0 1 2 3 . {{circ} 5} . 7 . . . 1 . 3 . . . 7 . 9 . . . 3 . . . . . 9 . | | | | | | | | | | 5: 0 0 0 0 1 1 1 1 2 2 2 3 5 7 1 3 7 9 3 9 } _p Remark: if a number {b n} is composed, then {b n=n{sub 1}*n{sub 2}}, with necessarily at least one of the divisors not smaller than {b √n}. This is why in the above sieve on {b 30} numbers we stop after finding the multiples of {b 5}, because {b 6*6=36 > 30}. _h2 1) lambdatalk recursive version {pre '{def rsieve {def rsieve.mark {lambda {:n :a :i :j} {if {< :j :n} then {rsieve.mark :n {A.set! :j . :a} // a[j] = "." , a dot :i {+ :i :j}} // j = i+j else :a}}} {def rsieve.loop {lambda {:n :a :i} {if {< {* :i :i} :n} // if i < √n then {rsieve.loop :n {if {W.equal? {A.get :i :a} .} // if a[i] = "." then :a // do nothing else {rsieve.mark :n // mark composites :a :i {* :i :i}}} // starting at i*i {+ :i 1}} else :a}}} {lambda {:n} {S.replace \s by space in // clean multiple spaces {S.replace (\[|\]|\.|,) by space in // clean [, ], dots and comas {A.disp // display array as [0,1,2,...n] {A.slice 2 -1 // avoid 0, 1 and the last one {rsieve.loop :n {A.new {S.serie 0 :n}} 2}}}}}}} // start at 2 -> {def rsieve {def rsieve.mark {lambda {:n :a :i :j} {if {< :j :n} then {rsieve.mark :n {A.set! :j . :a} :i {+ :i :j}} else :a}}} {def rsieve.loop {lambda {:n :a :i} {if {< {* :i :i} :n} then {rsieve.loop :n {if {W.equal? {A.get :i :a} .} then :a else {rsieve.mark :n :a :i {* :i :i}}} {+ :i 1}} else :a}}} {lambda {:n} {S.replace \s by space in {S.replace (\[|\]|\.|,) by space in {A.disp {A.slice 2 -1 {rsieve.loop :n {A.new {S.serie 0 :n}} 2}}}}}}} } {prewrap '{rsieve 1000} // computed in 35ms -> 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499 503 509 521 523 541 547 557 563 569 571 577 587 593 599 601 607 613 617 619 631 641 643 647 653 659 661 673 677 683 691 701 709 719 727 733 739 743 751 757 761 769 773 787 797 809 811 821 823 827 829 839 853 857 859 863 877 881 883 887 907 911 919 929 937 941 947 953 967 971 977 983 991 997 } _p The recursive version can lead to stackoverflow. _h2 2) lambdatalk iterative version {prewrap '{def isieve {def isieve.mark {lambda {:n :a :i} {S.map {{lambda {:a :j} {A.set! :j . :a} } :a} {S.serie {* :i :i} :n :i} }}} {lambda {:n} {S.replace \s by space in {S.replace (\[|\]|\.|,) by space in {A.disp {A.slice 2 -1 {S.last {S.map {{lambda {:n :a :i} {if {W.equal? {A.get :i :a} .} then else {isieve.mark :n :a :i}} } :n {A.new {S.serie 0 :n}}} {S.serie 2 {sqrt :n} 1} }}}} }}}} -> {def isieve {def isieve.mark {lambda {:n :a :i} {S.map {{lambda {:a :j} {A.set! :j . :a} } :a} {S.serie {* :i :i} :n :i} }}} {lambda {:n} {S.replace \s by space in {S.replace (\[|\]|\.|,) by space in {A.disp {A.slice 2 -1 {S.last {S.map {{lambda {:n :a :i} {if {W.equal? {A.get :i :a} .} then else {isieve.mark :n :a :i}} } :n {A.new {S.serie 0 :n}}} {S.serie 2 {sqrt :n} 1} }}}} }}}} '{isieve 1000} -> 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499 503 509 521 523 541 547 557 563 569 571 577 587 593 599 601 607 613 617 619 631 641 643 647 653 659 661 673 677 683 691 701 709 719 727 733 739 743 751 757 761 769 773 787 797 809 811 821 823 827 829 839 853 857 859 863 877 881 883 887 907 911 919 929 937 941 947 953 967 971 977 983 991 997 '{S.length {isieve 1000000}} -> 78500 // computed in 15000ms '{S.last {isieve 1000000}} -> 999983 // idem '{S.length {isieve 10000000}} -> 664579 // computed in 150000ms '{S.last {isieve 10000000}} -> 9999991 // idem } _p Note: the iterative version doesn't lead to stackoverflow, but it's still very slow. _h2 3) javascript version {pre °° LAMBDATALK.DICT["js.sieve"] = function() { var sieve_init = function(n) { var a = []; for (var i=0; i < n; i++) a[i] = 0; return a }; var sieve_jump = function(n,a,lim,m) { while (m < lim) { a[m] = 1; m += n } return a }; var sieve_loop = function(lim,a) { for (var n=2; n*n < lim; n++) { if (a[n] == 0) a = sieve_jump(n,a,lim,n*n) } return a }; var sieve_disp = function(a) { var b = []; for (var i=0; i < a.length; i++) if (a[i] === 0) b.push(i) return b.slice(2).join(" ") }; var lim = arguments[0].trim(); return sieve_disp(sieve_loop(lim,sieve_init(lim))) } °°} {prewrap '{js.sieve 1000} -> {js.sieve 1000} '{S.length {js.sieve 10000000}} -> 664579 // computed in 1 second '{S.last {js.sieve 10000000}} -> 9999991 // idem } _p The JS version is obviously much more faster. _p {i alain marty | 2023/01/09} {{hide} {def circ span {@ style="border-radius:10px; background:red; color:white; "}} } {script LAMBDATALK.DICT["js.sieve"] = function() { var sieve_init = function(n) { var a = []; for (var i=0; i < n; i++) a[i] = 0; return a }; var sieve_jump = function(n,a,lim,m) { while (m < lim) { a[m] = 1; m += n } return a }; var sieve_loop = function(lim,a) { for (var n=2; n*n < lim; n++) { if (a[n] == 0) a = sieve_jump(n,a,lim,n*n) } return a }; var sieve_disp = function(a) { var b = []; for (var i=0; i < a.length; i++) if (a[i] === 0) b.push(i) return b.slice(2).join(" ") }; var lim = arguments[0].trim(); return sieve_disp(sieve_loop(lim,sieve_init(lim))) } }
lambdaway v.20211111