function PERMUTE(N, VECTOR, PERMUTERS) result(LAST) ! CAUTION: N factorial permutations is a bunch for N > 9 ! The precise definition of a bunch is 3,628,800 up ! 7! = 5,040 ! 8! = 40,320 ! 9! = 362,880 ! ! INPUT: VECTOR - input and output vector that is permuted ! This must be set up befor the first call ! and then used as the next permutation on ! each return e.g. 1, 2, 3, ... ! ! PERMUTERS(1) = N on first call, then untouched by caller ! used internally ! ! OUTPUT: VECTOR - the next permuted vector ! LAST = 0 if more permutations to be returned ! = 1 if the last permutation is bein returned(done) ! First Written 1 May, 1985 by Jon Squire ! implicit none integer, intent(in) :: N integer, dimension(N), intent(inout) :: VECTOR, PERMUTERS integer :: LAST integer :: I, J, K, L, T LAST = 0 if(N .le. 1) then LAST = 1 return else if(PERMUTERS(1) .ne. 1) then ! Initialization(user must set up VECTOR befor first call) do I = 1,N PERMUTERS(I) = I end do else ! find last in sequence of permuters 1's i.e. PERMUTERS(1) ! reset permuters(k) to k as we go K = 1 do while(PERMUTERS(K+1) .eq. 1 .and. k