Jump to content

Jacobsthal numbers: Difference between revisions

(Added 11l)
Line 2,437:
2796203
715827883
</pre>
 
=={{header|RPL}}==
===Straightforward approach===
The Jacobstahl pieces of code are macros more than programs.
≪ 2 OVER ^ -1 ROT ^ - 3 / ≫
'JCBN' STO
≪ 2 OVER ^ -1 ROT ^ + ≫
'JCBL' STO
≪ DUP JCBN SWAP 1 + JCBN * ≫
'JCBO' STO
The primality test is the only one that deserves such a name:
≪ IF DUP 5 ≤ THEN
{ 2 3 5 } SWAP POS
ELSE
IF DUP 2 MOD NOT THEN
2
ELSE
DUP √ CEIL → lim
≪ 3
WHILE DUP2 MOD OVER lim ≤ AND REPEAT 2 + END
END
MOD
END
SIGN
'PRIM?' STO
===Using binary integers===
The task is an opportunity to showcase the use of binary integers in RPL, but it's actually slower and fatter, as many RPL instructions are designed for floating point numbers only.
≪ → n
≪ # 1h 1 n START SL NEXT
IF n R→B # 1h AND B→R THEN 1 + ELSE 1 - END
3 / B→R
≫ ≫
'JCBN' STO
===Testing program===
≪ → func count
≪ { } 0 DO
DUP func IF EVAL THEN ROT SWAP + SWAP ELSE DROP END
1 +
UNTIL OVER SIZE count ≥ END
DROP
≫ ≫
'ASSRT' STO
 
≪ JCBN 1 ≫ 30 ASSRT
≪ JCBL 1 ≫ 30 ASSRT
≪ JCBO 1 ≫ 20 ASSRT
≪ DUP JCBN PRIM? ≫ 10 ASSRT
{{works with|Halcyon Calc|4.2.7}}
{{out}}
<pre>
4: { 0 1 1 3 5 11 21 43 85 171 341 683 1365 2731 5461 10923 21845 43691 87381 174763 349525 699051 1398101 2796203 5592405 11184811 22369621 44739243 89478485 178956971 }
3: { 2 1 5 7 17 31 65 127 257 511 1025 2047 4097 8191 16385 32767 65537 131071 262145 524287 1048577 2097151 4194305 8388607 16777217 33554431 67108865 134217727 268435457 536870911 }
2: { 0 1 3 15 55 231 903 3655 14535 58311 232903 932295 3727815 14913991 59650503 238612935 954429895 3817763271 15270965703 61084037575 }
1: { 3 5 11 43 683 2731 43691 174763 2796203 715827883 }
</pre>
 
1,150

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.