Golfscript, 59 51 50 ตัวอักษร
มนุษย์แต่ละตัวละครเป็นเรื่องยากมากที่จะสูญเสีย:
0[2.{).,2>{\.@%!},{.2$-.4$>{].p~\[}{;\;}if..}or}do
ผลผลิต :
[2 3 1]
[3 5 2]
[7 11 4]
[23 29 6]
[89 97 8]
[113 127 14]
...
คำอธิบาย :
สแต็คถูกตั้งค่าเพื่อให้การวนซ้ำแต่ละครั้งเริ่มต้นด้วยสแต็กเช่นนี้ส่วนบนเป็นด้านขวา เครื่องหมาย[
บ่งชี้อาร์เรย์ปัจจุบันซึ่งหมายถึงเมื่อล่ามพบ]
ทุกสิ่งที่อยู่ในสแต็กจากเครื่องหมายไปที่ด้านบนจะถูกใส่เข้าไปในอาร์เรย์
g [ last | cur
g
เป็นช่องว่างสูงสุดจนถึงตอนนี้ จากบนลงล่าง:
command | explanation
-----------------+----------------------------------------
0[2. | initialize vars g=0, last=2, cur=2
{...}do | loop forever...
ภายในวง:
) | cur += 1
.,2>{\.@%!}, | put all divisors of cur into a list
{...}or | if the list is empty, cur is prime, so
| the block is executed. otherwise,
| 'do' consumes the stack, sees it is truthy,
| and loops again
มันทำให้ตัวหารทั้งหมดเข้าสู่รายการได้อย่างไร ลองทำทีละขั้นตอน
Command | explanation | stack
-----------------+----------------------------------------------+----------------
| initial stack | n
., | make list of 0..n-1 | n [0,1,...,n-1]
2> | take elements at index 2 and greater | n [2,3,...,n-1]
{...}, | take list off stack, then iterate through |
| the list. on each iteration, put the current |
| element on the stack, execute the block, and |
| pop the top of the stack. if the top is |
| true then keep the element, else drop it. |
| when done, push list of all true elements |
| So, for each element... | n x
\. | Swap & dup | x n n
@ | Bring x around | n n x
% | Modulo | n (n%x)
! | Boolean not. 0->1, else->0. Thus this is 1 |
| if x divides n. | n (x divides n)
| So only the divisors of n are kept | n [divisors of n]
มันจะทำอย่างไรถ้าตัวหารว่างเปล่า
Command | explanation | stack
-----------------+----------------------------------------------+----------------
| initial stack | g [ last | cur
. | dup | g [ l | c | c
2$ | copy 3rd down | g [ l | c | c | l
- | sub. This is the current gap, cur-last | g [ l | c | c-l
. | dup | g [ l | c | c-l | c-l
4$ | copy 4th down | g [ l | c | c-l | c-l | g
> | is cur gap > max gap so far? | g [ l | c | c-l | c-l>g
{#1}{#2}if.. | #1 if c-l > g, #2 otherwise, and do ".." in | ... | g [ c | c | c
| either situation |
สองเส้นทาง: ใช่และไม่ใช่ ถ้าใช่ (โปรดทราบว่าif
ใช้ค่าสูงสุดในสแต็ก):
Command | explanation | stack
-----------------+----------------------------------------------+----------------
| initial stack. note that now the old `g` is | XX [ l | c | g
| garbage and `c-l` is the new `g`. |
] | close the array | XX [l, c, g]
.p | duplicate it and print it, consuming the dup | XX [l, c, g]
~ | pump array back onto the stack. Note now the | XX | l | c | j
| array marker [ is gone. |
\ | swap. | XX | l | g | c
[ | mark the array | XX | l | g | c [
. | this is the part after the if. dups the top, | XX | l | g [ c | c
| but it does this in two steps, first popping |
| c then putting two copies on top, so the |
| array marker moves |
. | dup again | XX | l | g [ c | c | c
ถ้าไม่:
Command | explanation | stack
-----------------+----------------------------------------------+----------------
| initial stack. In this case g is still the | g [ l | c | c-l
| max gap so far |
;\; | dump top of stack, swap, and dump again | g [ c
.. | the part after the if. dup twice | g [ c | c | c
หมายเหตุในทั้งสองกรณีตอนนี้สแต็กของเราอยู่ในรูปแบบ... | g [ c | c | c
แล้ว
ทีนี้do
ป๊อปอัพค่าสูงสุดออกจากสแต็ก - เสมอc
- และลูปถ้ามันเป็นบวก ตั้งแต่c
เพิ่มขึ้นเรื่อย ๆ สิ่งนี้เป็นจริงเสมอดังนั้นเราจึงวนซ้ำไปตลอดกาล
เมื่อมีการผุดขึ้นด้านบนของสแต็คคือg [ c | c
ความหมายที่ผ่านมาได้รับการปรับปรุงc
เครื่องหมายอาร์เรย์อยู่ในสถานที่เดียวกันและg
ยังคงเป็นที่ที่เราคาดหวัง
นี่คือการดำเนินการที่ซับซ้อนของ GolfScript ฉันหวังว่าคุณจะสนุกไปกับการติดตาม!