Topswops: Difference between revisions

Add Factor
m (→‎{{header|Perl 6}}: be less specific about degree of concurrency)
(Add Factor)
Line 793:
9 30
10 38
</pre>
 
=={{header|Factor}}==
<lang factor>USING: formatting kernel math math.combinatorics math.order
math.ranges sequences ;
FROM: sequences.private => exchange-unsafe ;
IN: rosetta-code.topswops
 
! Reverse a subsequence in-place from 0 to n.
: head-reverse! ( seq n -- seq' )
dupd [ 2/ ] [ ] bi rot
[ [ over - 1 - ] dip exchange-unsafe ] 2curry each-integer ;
 
! Reverse the elements in seq according to the first element.
: swop ( seq -- seq' ) dup first head-reverse! ;
 
! Determine the number of swops until 1 is the head.
: #swops ( seq -- n )
0 swap [ dup first 1 = ] [ [ 1 + ] [ swop ] bi* ] until
drop ;
 
! Determine the maximum number of swops for a given length.
: topswops ( n -- max )
[1,b] <permutations> [ #swops ] [ max ] map-reduce ;
 
: main ( -- )
10 [1,b] [ dup topswops "%2d: %2d\n" printf ] each ;
 
MAIN: main</lang>
{{out}}
<pre>
1: 0
2: 1
3: 2
4: 4
5: 7
6: 10
7: 16
8: 22
9: 30
10: 38
</pre>
 
1,808

edits