Hofstadter Figure-Figure sequences: Difference between revisions
Content added Content deleted
(Factor solution) |
|||
Line 222: | Line 222: | ||
true</pre> |
true</pre> |
||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
The core of this algorithm builds up a list of S and R one at a time. The solution to task 1 follows. |
|||
<lang factor>: next ( s r -- news newr ) |
<lang factor>: next ( s r -- news newr ) |
||
2dup [ last ] bi@ + suffix |
2dup [ last ] bi@ + suffix |
||
dup [ |
dup [ |
||
[ dup last 1 + dup ] dip member? [ 1 + ] when suffix |
[ dup last 1 + dup ] dip member? [ 1 + ] when suffix |
||
] dip ; |
] dip ; |
||
: SR ( n -- S(n) R(n) ) |
|||
Tests: |
|||
1 - |
|||
[ { 2 } { 1 } ] dip [ next ] times |
|||
[ last ] bi@ ; |
|||
: ffs ( n -- S(n) ) SR drop ; |
|||
: ffr ( n -- R(n) ) SR nip ; |
|||
</lang> |
|||
For tasks 3 and 4 we ignore the functions <code>ffs</code> and <code>ffr</code> and simply use <code>next</code>. |
|||
<lang factor>( scratchpad ) { 2 } { 1 } 1000 [ next ] times ! Generate the first 1000 values of S and R |
<lang factor>( scratchpad ) { 2 } { 1 } 1000 [ next ] times ! Generate the first 1000 values of S and R |
||