Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. J(7,1) = 7 // people are counted out in order 1 2 3 4 5 6 [7]
  2. J(7,2) = 7 // people are counted out in order 2 4 6 1 5 3 [7]
  3. J(7,3) = 4 // see above
  4. J(7,11) = 1
  5. J(77,8) = 1
  6. J(123,12) = 21
  7.  
  8. {{@+)%}+,*)}:f;
  9.  
  10. { # Function declaration
  11. # Stack: n k
  12. { # Stack: g(i-1,k) i-1 k
  13. @+)% # Stack: g(i,k)
  14. }+ # Add, giving stack: n {k block}
  15. ,* # Fold {k block} over [0 1 ... n-1]
  16. ) # Increment to move from 0-based to 1-based indexing
  17. }:f;
  18.  
  19. J=lambda n,k:n<2or(J(n-1,k)+k-1)%n+1
  20.  
  21. >>> J(7,3)
  22. 4
  23. >>> J(77,8)
  24. 1
  25. >>> J(123,12)
  26. 21
  27.  
  28. 1~f~_=1
  29. n_~f~k_:=Mod[f[n-1,k]+k,n,1]
  30.  
  31. j(n,k){return n>1?(j(n-1,k)+k-1)%n+1:1;}
  32.  
  33. j(n,k,c,i,r){char o[999];memset(o,1,n);for(c=k,i=0;r;++i)(c-=o[i%=n])||(o[i]=!r--,c=k);
  34. return i;}
  35.  
  36. [d1-d1<glk+r%1+]dsg?1-skrxp
  37.  
  38. # comment shows what is on the stack and any other effect the instructions have
  39. [ # n
  40. d # n, n
  41. 1- # n-1, n
  42. d # n-1, n-1, n
  43. 1<g # g(n-1), n ; g is executed only if n > 1, conveniently g(1) = 1
  44. lk+ # g(n-1)+(k-1), n; remember, k register holds k-1
  45. r% # g(n-1)+(k-1) mod n
  46. 1+ # (g(n-1)+(k-1) mod n)+1
  47. ]
  48. dsg # code for g; code also stored in g
  49. ? # read user input => k, n, code for g
  50. 1- # k-1, n, code for g
  51. sk # n, code for g; k-1 stored in register k
  52. r # code for g, n
  53. x # g(n)
  54. p # prints g(n)
  55.  
  56. j=.[:{.@{:]([:}.]|.~<:@[|~#@])^:(<:@#)>:@i.@[
  57.  
  58. j=.1:`(1+[|1-~]+<:@[$:])@.(1<[)
  59.  
  60. 7 j 3
  61. 4
  62. 123 j 12
  63. 21
  64.  
  65. :k;0:^;(,{))^k+%:^;}/^)
  66.  
  67. {1$1>{1$(1$^1$(+2$%)}1if@@;;}:^;
  68.  
  69. def j(n,k){n>1?(j(n-1,k)+k-1)%n+1:1}
  70.  
  71. j n=q$cycle[0..n]
  72. q l@(i:h:_)k|h/=i=q(drop(k-1)$filter(/=i)l)k|1>0=i
  73.  
  74. J=function(n,k)ifelse(n<2,1,(J(n-1,k)+k-1)%%n+1)
  75.  
  76. def?(n:Int,k:Int):Int=if(n<2)1 else(?(n-1,k)+k-1)%n+1
  77.  
  78. j(n,k){
  79. int i=0,c=k,r=n,*p=calloc(n,8);
  80. for(;p[i=i%n+1]||--c?1:--r?p[i]=c=k:0;);
  81. return i;
  82. }
  83.  
  84. 1&|.&.#:
  85.  
  86. 1&|.&.#: 10
  87. 5
  88.  
  89. 1&|.&.#: 69
  90. 11
  91.  
  92. 1&|.&.#: 123456
  93. 115841
  94.  
  95. 1&|.&.#: 123245678901234567890x NB. x keeps input integral
  96. 98917405212792722853
  97.  
  98. All credit to Roger Hui, co-inventor of J and all-round uber-genius
  99. www.jsoftware.com for free j software across many platforms
  100.  
  101. Explanation
  102. (J works right-to-left)
  103. #: convert input to binary
  104. &. a&.b <=> perform b, perform a, perform reverse of b
  105. 1&|. rotate bitwise one bit left
  106.  
  107. So
  108. 1&|.&.#: 10
  109.  
  110. a. #: convert input (10) TO binary -> 1 0 1 0
  111. b. 1&|. rotate result 1 bit left -> 0 1 0 1
  112. c. due to the &. perform convert FROM binary -> 5 (answer)
  113.  
  114. f:{$[x=1;1;1+mod[;x]f[x-1;y]+y-1]}
  115.  
  116. q)f .'(7 1;7 2;7 3;7 11;77 8;123 12)
  117. 7 7 4 1 1 21
  118.  
  119. J=->n,k{n<2?1:(J(n-1,k)+k-1)%n+1}
  120.  
  121. 1#_=1
  122. n#k=mod((n-1)#k+k-1)n+1
  123.  
  124. function f(a,b){return a<2?1:(f(a-1,b)+b-1)%a+1}
  125.  
  126. f=(a,b)=>a<2?1:(f(a-1,b)+b-1)%a+1
  127.  
  128. L[Dg#²<FÀ}¦
  129.  
  130. L # Range 1 .. n
  131. [Dg# # Until the array has a length of 1:
  132. ²<F } # k - 1 times do:
  133. À # Rotate the array
  134. ¦ # remove the first element
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement