Mian-Chowla sequence: Difference between revisions
Content added Content deleted
m (→{{header|Wren}}: Fixed bug in S/H) |
(→{{header|RPL}}: faster sieve version) |
||
Line 1,908: | Line 1,908: | ||
<pre> |
<pre> |
||
1: { 1 2 4 8 13 21 31 45 66 81 97 123 148 182 204 252 290 361 401 475 565 593 662 775 822 916 970 1016 1159 1312 } |
1: { 1 2 4 8 13 21 31 45 66 81 97 123 148 182 204 252 290 361 401 475 565 593 662 775 822 916 970 1016 1159 1312 } |
||
</pre> |
|||
===Sieve version=== |
|||
This approach is 3 times faster and uses less memory. |
|||
« 39 DUP STWS / CEIL |
|||
« # 0b » 'x' 1 4 ROLL 1 SEQ |
|||
» '<span style="color:blue">CSV</span>' STO <span style="color:grey">''@ ( size → { sieve } )''</span> |
|||
« SWAP 1 - 39 MOD LASTARG / IP 1 + |
|||
ROT SWAP DUP2 GET 2 5 ROLL ^ R→B OR PUT |
|||
» '<span style="color:blue">SSV</span>' STO <span style="color:grey">''@ ( val 'sieve' → )''</span> |
|||
« SWAP 1 - 39 MOD LASTARG / IP 1 + |
|||
ROT SWAP GET 2 ROT ^ R→B AND # 0b ≠ |
|||
» '<span style="color:blue">SVS?</span>' STO <span style="color:grey">''@ ( val 'sieve' → boolean )''</span> |
|||
« 50000 <span style="color:blue">CSV</span> → n sums |
|||
« { 1 } 2 'sums' SSV |
|||
'''WHILE''' DUP SIZE n < '''REPEAT''' |
|||
DUP DUP SIZE GET |
|||
'''DO''' 1 + |
|||
1 SF |
|||
DUP2 ADD |
|||
1 OVER SIZE '''FOR''' j |
|||
'''IF''' DUP j GET 'sums' <span style="color:blue">SVS?</span> '''THEN''' 1 CF SIZE 'j' STO '''END''' |
|||
'''NEXT''' |
|||
'''UNTIL''' 1 FS? '''END''' |
|||
1 |
|||
1 3 PICK SIZE '''START''' GETI 'sums' <span style="color:blue">SSV</span> '''NEXT''' |
|||
DROP2 |
|||
DUP 2 * 'sums' <span style="color:blue">SSV</span> |
|||
+ |
|||
'''END''' |
|||
» » '<span style="color:blue">A5282</span>' STO |
|||
100 <span style="color:blue">A5282</span> |
|||
DUP 1 30 SUB SWAP 91 100 SUB |
|||
{{out}} |
|||
<pre> |
|||
2: { 1 2 4 8 13 21 31 45 66 81 97 123 148 182 204 252 290 361 401 475 565 593 662 775 822 916 970 1016 1159 1312 } |
|||
1: { 22526 23291 23564 23881 24596 24768 25631 26037 26255 27219 } |
|||
</pre> |
</pre> |
||