Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
3
Task/Josephus-problem/00-META.yaml
Normal file
3
Task/Josephus-problem/00-META.yaml
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
---
|
||||
from: http://rosettacode.org/wiki/Josephus_problem
|
||||
note: Puzzles
|
||||
33
Task/Josephus-problem/00-TASK.txt
Normal file
33
Task/Josephus-problem/00-TASK.txt
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
[[wp:Josephus problem|Josephus problem]] is a math puzzle with a grim description: <math>n</math> prisoners are standing on a circle, sequentially numbered from <math>0</math> to <math>n-1</math>.
|
||||
|
||||
An executioner walks along the circle, starting from prisoner <math>0</math>,
|
||||
removing every <math>k</math>-th prisoner and killing him.
|
||||
|
||||
As the process goes on, the circle becomes smaller and smaller, until only one prisoner remains, who is then freed. >
|
||||
|
||||
For example, if there are <math>n=5</math> prisoners and <math>k=2</math>, the order the prisoners are killed in (let's call it the "killing sequence") will be 1, 3, 0, and 4, and the survivor will be #2.
|
||||
|
||||
|
||||
;Task:
|
||||
Given any <big><math>n, k > 0</math></big>, find out which prisoner will be the final survivor.
|
||||
|
||||
In one such incident, there were 41 prisoners and every 3<sup>rd</sup> prisoner was being killed (<big><math>k=3</math></big>).
|
||||
|
||||
Among them was a clever chap name Josephus who worked out the problem, stood at the surviving position, and lived on to tell the tale.
|
||||
|
||||
Which number was he?
|
||||
|
||||
|
||||
;Extra:
|
||||
The captors may be especially kind and let <math>m</math> survivors free,
|
||||
<br>and Josephus might just have <big><math>m-1</math></big> friends to save.
|
||||
|
||||
Provide a way to calculate which prisoner is at any given position on the killing sequence.
|
||||
|
||||
|
||||
;Notes:
|
||||
# You can always play the executioner and follow the procedure exactly as described, walking around the circle, counting (and cutting off) heads along the way. This would yield the complete killing sequence and answer the above questions, with a complexity of probably <math>O(kn)</math>. However, individually it takes no more than <math>O(m)</math> to find out which prisoner is the <math>m</math>-th to die.
|
||||
# If it's more convenient, you can number prisoners from <math>1</math> to <math>n</math> instead. If you choose to do so, please state it clearly.
|
||||
# An alternative description has the people committing assisted suicide instead of being executed, and the last person simply walks away. These details are not relevant, at least not mathematically.
|
||||
<br><br>
|
||||
|
||||
11
Task/Josephus-problem/11l/josephus-problem.11l
Normal file
11
Task/Josephus-problem/11l/josephus-problem.11l
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
F j(n, k)
|
||||
V p = Array(0 .< n)
|
||||
V i = 0
|
||||
[Int] seq
|
||||
L !p.empty
|
||||
i = (i + k - 1) % p.len
|
||||
seq.append(p.pop(i))
|
||||
R "Prisoner killing order: #..\nSurvivor: #.".format(seq[0 .< (len)-1].join(‘, ’), seq.last)
|
||||
|
||||
print(j(5, 2))
|
||||
print(j(41, 3))
|
||||
82
Task/Josephus-problem/360-Assembly/josephus-problem.360
Normal file
82
Task/Josephus-problem/360-Assembly/josephus-problem.360
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
* Josephus problem 10/02/2017
|
||||
JOSEPH CSECT
|
||||
USING JOSEPH,R13 base register
|
||||
B 72(R15) skip savearea
|
||||
DC 17F'0' savearea
|
||||
STM R14,R12,12(R13) prolog
|
||||
ST R13,4(R15) " <-
|
||||
ST R15,8(R13) " ->
|
||||
LR R13,R15 " addressability
|
||||
LA R7,1 m=1
|
||||
DO WHILE=(C,R7,LE,=A(NPROB)) do m=1 to nprob
|
||||
LR R1,R7 m
|
||||
MH R1,=H'6' *6
|
||||
LH R2,PROB-6(R1)
|
||||
ST R2,N n=prob(m,1)
|
||||
LH R2,PROB-4(R1)
|
||||
ST R2,W w=prob(m,2)
|
||||
LH R2,PROB-2(R1)
|
||||
ST R2,S s=prob(m,3)
|
||||
MVC PG,=CL80'josephus' init buffer
|
||||
L R1,N n
|
||||
XDECO R1,DEC edit
|
||||
MVC PG+8(4),DEC+8 output
|
||||
L R1,W w
|
||||
XDECO R1,DEC edit
|
||||
MVC PG+12(4),DEC+8 output
|
||||
L R1,S s
|
||||
XDECO R1,DEC edit
|
||||
MVC PG+16(4),DEC+8 output
|
||||
XPRNT PG,L'PG print buffer
|
||||
MVI DEAD,X'00' dead(1)='0'B;
|
||||
MVC DEAD+1(255),DEAD dead(*)='0'B;
|
||||
L R11,N nx=n
|
||||
L R8,=F'-1' p=-1
|
||||
DO UNTIL=(C,R11,EQ,S) do until n=s
|
||||
SR R9,R9 found=0
|
||||
DO UNTIL=(C,R9,EQ,W) do until found=w
|
||||
LA R8,1(R8) p=p+1
|
||||
IF C,R8,EQ,N THEN if p=nn then
|
||||
SR R8,R8 p=0
|
||||
ENDIF , end if
|
||||
LA R2,DEAD(R8) @dead(p+1)
|
||||
IF CLI,0(R2),EQ,X'00' THEN if not dead(p+1) then
|
||||
LA R9,1(R9) found=found+1
|
||||
ENDIF , end if
|
||||
ENDDO , end do
|
||||
LA R2,DEAD(R8) @dead(p+1)
|
||||
MVI 0(R2),X'01' dead(p+1)='1'B
|
||||
BCTR R11,0 nx=nx-1
|
||||
ENDDO , end do
|
||||
MVC PG,=CL80' ' clear buffer
|
||||
LA R10,PG ipg=0
|
||||
L R9,N nn
|
||||
BCTR R9,0 nn-1
|
||||
SR R6,R6 i=0
|
||||
DO WHILE=(CR,R6,LE,R9) do i=0 to nn-1
|
||||
LA R2,DEAD(R6) @dead(i+1)
|
||||
IF CLI,0(R2),EQ,X'00' THEN if not dead(i+1) then
|
||||
XDECO R6,DEC edit i
|
||||
MVC 0(4,R10),DEC+8 output
|
||||
LA R10,4(R10) ipg=ipg+4
|
||||
ENDIF , end if
|
||||
LA R6,1(R6) i=i+1
|
||||
ENDDO , end do
|
||||
XPRNT PG,L'PG print buffer
|
||||
LA R7,1(R7) m=m+1
|
||||
ENDDO , end do
|
||||
L R13,4(0,R13) epilog
|
||||
LM R14,R12,12(R13) " restore
|
||||
XR R15,R15 " rc=0
|
||||
BR R14 exit
|
||||
PROB DC H'41',H'3',H'1' round 1
|
||||
DC H'41',H'3',H'3' round 2
|
||||
NPROB EQU (*-PROB)/6 number of rounds
|
||||
N DS F n number of prisoners
|
||||
W DS F w killing count
|
||||
S DS F s number of prisoners to survive
|
||||
PG DS CL80 buffer
|
||||
DEC DS CL12 temp for xdeco
|
||||
DEAD DS 256X n max
|
||||
YREGS
|
||||
END JOSEPH
|
||||
34
Task/Josephus-problem/6502-Assembly/josephus-problem.6502
Normal file
34
Task/Josephus-problem/6502-Assembly/josephus-problem.6502
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
JSEPHS: STA $D0 ; n
|
||||
STX $D1 ; k
|
||||
LDA #$FF
|
||||
LDX #$00
|
||||
SETUP: STA $1000,X ; populate array with hex FF
|
||||
INX
|
||||
CPX $D0
|
||||
BEQ KILL
|
||||
JMP SETUP
|
||||
KILL: LDA #$00 ; number killed so far
|
||||
STA $D2
|
||||
LDX #$00 ; position within array
|
||||
LDY #$01 ; counting up to k
|
||||
FIND: INY
|
||||
SCAN: INX
|
||||
CPX $D0
|
||||
BMI TEST
|
||||
LDX #$00 ; circle back around
|
||||
TEST: LDA $1000,X
|
||||
CMP #$FF
|
||||
BNE SCAN ; already been killed
|
||||
CPY $D1
|
||||
BMI FIND ; if y < k keep going round
|
||||
LDA $D2
|
||||
STA $1000,X ; mark as dead
|
||||
CLC
|
||||
ADC #$01
|
||||
STA $D2
|
||||
CMP $D0 ; have we killed all but 1?
|
||||
BPL RETURN
|
||||
LDY #$00
|
||||
JMP FIND
|
||||
RETURN: TXA ; a <- index of survivor
|
||||
RTS
|
||||
190
Task/Josephus-problem/AArch64-Assembly/josephus-problem.aarch64
Normal file
190
Task/Josephus-problem/AArch64-Assembly/josephus-problem.aarch64
Normal file
|
|
@ -0,0 +1,190 @@
|
|||
/* ARM assembly AARCH64 Raspberry PI 3B */
|
||||
/* program josephus64.s */
|
||||
/* run with josephus64 maxi intervalle */
|
||||
/* example : josephus64 41 3
|
||||
|
||||
/*******************************************/
|
||||
/* Constantes file */
|
||||
/*******************************************/
|
||||
/* for this file see task include a file in language AArch64 assembly*/
|
||||
.include "../includeConstantesARM64.inc"
|
||||
|
||||
.equ FIRSTNODE, 0 //identification first node
|
||||
|
||||
/*******************************************/
|
||||
/* Structures */
|
||||
/********************************************/
|
||||
/* structure linkedlist*/
|
||||
.struct 0
|
||||
llist_next: // next element
|
||||
.struct llist_next + 8
|
||||
llist_value: // element value
|
||||
.struct llist_value + 8
|
||||
llist_fin:
|
||||
/*********************************/
|
||||
/* Initialized data */
|
||||
/*********************************/
|
||||
.data
|
||||
szMessDebutPgm: .asciz "Start program.\n"
|
||||
szMessFinPgm: .asciz "Program End ok.\n"
|
||||
szRetourLigne: .asciz "\n"
|
||||
szMessValElement: .asciz "Value : @ \n"
|
||||
szMessListeVide: .asciz "List empty.\n"
|
||||
szMessImpElement: .asciz "Node display: @ Value : @ Next @ \n"
|
||||
szMessErrComm: .asciz "Incomplete Command line : josephus64 <maxi> <intervalle>\n"
|
||||
/*********************************/
|
||||
/* UnInitialized data */
|
||||
/*********************************/
|
||||
.bss
|
||||
sZoneConv: .skip 100
|
||||
.align 4
|
||||
qDebutListe1: .skip llist_fin
|
||||
/*********************************/
|
||||
/* code section */
|
||||
/*********************************/
|
||||
.text
|
||||
.global main
|
||||
main: // entry of program
|
||||
mov fp,sp // copy stack address register x29 fp
|
||||
ldr x0,qAdrszMessDebutPgm
|
||||
bl affichageMess
|
||||
ldr x0,[fp] // parameter number command line
|
||||
cmp x0,#2 // correct ?
|
||||
ble erreurCommande // error
|
||||
|
||||
add x0,fp,#16 // address parameter 2
|
||||
ldr x0,[x0]
|
||||
bl conversionAtoD
|
||||
add x22,x0,FIRSTNODE // save maxi
|
||||
add x0,fp,#24 // address parameter 3
|
||||
ldr x0,[x0]
|
||||
bl conversionAtoD
|
||||
mov x21,x0 // save gap
|
||||
|
||||
mov x0,FIRSTNODE // create first node
|
||||
mov x1,0
|
||||
bl createNode
|
||||
mov x25,x0 // first node address
|
||||
mov x26,x0
|
||||
mov x24,FIRSTNODE + 1
|
||||
mov x23,1
|
||||
1: // loop create others nodes
|
||||
mov x0,x24 // key value
|
||||
mov x1,0
|
||||
bl createNode
|
||||
str x0,[x26,llist_next] // store current node address in prev node
|
||||
mov x26,x0
|
||||
add x24,x24,1
|
||||
add x23,x23,1
|
||||
cmp x23,x22 // maxi ?
|
||||
blt 1b
|
||||
str x25,[x26,llist_next] // store first node address in last pointer
|
||||
mov x24,x26
|
||||
2:
|
||||
mov x20,1 // counter for gap
|
||||
3:
|
||||
ldr x24,[x24,llist_next]
|
||||
add x20,x20,1
|
||||
cmp x20,x21 // intervalle ?
|
||||
blt 3b
|
||||
ldr x25,[x24,llist_next] // removing the node from the list
|
||||
ldr x22,[x25,llist_value]
|
||||
ldr x27,[x25,llist_next] // load pointer next
|
||||
str x27,[x24,llist_next] // ans store in prev node
|
||||
//mov x0,x25
|
||||
//bl displayNode
|
||||
cmp x27,x24
|
||||
csel x24,x24,x27,ne // next node address
|
||||
bne 2b // and loop
|
||||
|
||||
mov x0,x24
|
||||
bl displayNode // display last node
|
||||
|
||||
b 100f
|
||||
erreurCommande:
|
||||
ldr x0,qAdrszMessErrComm
|
||||
bl affichageMess
|
||||
mov x0,#1 // error code
|
||||
b 100f
|
||||
100: // program end standard
|
||||
ldr x0,qAdrszMessFinPgm
|
||||
bl affichageMess
|
||||
mov x0,0 // return code Ok
|
||||
mov x8,EXIT // system call "Exit"
|
||||
svc #0
|
||||
|
||||
qAdrszMessDebutPgm: .quad szMessDebutPgm
|
||||
qAdrszMessFinPgm: .quad szMessFinPgm
|
||||
qAdrszRetourLigne: .quad szRetourLigne
|
||||
qAdrqDebutListe1: .quad qDebutListe1
|
||||
qAdrszMessErrComm: .quad szMessErrComm
|
||||
|
||||
/******************************************************************/
|
||||
/* create node */
|
||||
/******************************************************************/
|
||||
/* x0 contains key */
|
||||
/* x1 contains zero or address next node */
|
||||
/* x0 returns address heap node */
|
||||
createNode:
|
||||
stp x20,lr,[sp,-16]! // save registres
|
||||
stp x21,x22,[sp,-16]! // save registres
|
||||
mov x20,x0 // save key
|
||||
mov x21,x1 // save key
|
||||
mov x0,#0 // allocation place heap
|
||||
mov x8,BRK // call system 'brk'
|
||||
svc #0
|
||||
mov x22,x0 // save address heap for node
|
||||
add x0,x0,llist_fin // reservation place node length
|
||||
mov x8,BRK // call system 'brk'
|
||||
svc #0
|
||||
cmp x0,#-1 // allocation error
|
||||
beq 100f
|
||||
|
||||
str x20,[x22,llist_value]
|
||||
str x21,[x22,llist_next]
|
||||
mov x0,x22
|
||||
100:
|
||||
ldp x21,x22,[sp],16 // restaur des 2 registres
|
||||
ldp x20,lr,[sp],16 // restaur des 2 registres
|
||||
ret // retour adresse lr x30
|
||||
|
||||
/******************************************************************/
|
||||
/* display infos node */
|
||||
/******************************************************************/
|
||||
/* x0 contains node address */
|
||||
displayNode:
|
||||
stp x1,lr,[sp,-16]! // save registres
|
||||
stp x2,x3,[sp,-16]! // save registres
|
||||
mov x2,x0
|
||||
ldr x1,qAdrsZoneConv
|
||||
bl conversion16
|
||||
ldr x0,qAdrszMessImpElement
|
||||
ldr x1,qAdrsZoneConv
|
||||
bl strInsertAtCharInc
|
||||
mov x3,x0
|
||||
ldr x0,[x2,llist_value]
|
||||
ldr x1,qAdrsZoneConv
|
||||
bl conversion10S
|
||||
mov x0,x3
|
||||
ldr x1,qAdrsZoneConv
|
||||
bl strInsertAtCharInc
|
||||
mov x3,x0
|
||||
ldr x0,[x2,llist_next]
|
||||
ldr x1,qAdrsZoneConv
|
||||
bl conversion16
|
||||
mov x0,x3
|
||||
ldr x1,qAdrsZoneConv
|
||||
bl strInsertAtCharInc
|
||||
bl affichageMess
|
||||
|
||||
100:
|
||||
ldp x2,x3,[sp],16 // restaur des 2 registres
|
||||
ldp x1,lr,[sp],16 // restaur des 2 registres
|
||||
ret // retour adresse lr x30
|
||||
qAdrsZoneConv: .quad sZoneConv
|
||||
qAdrszMessImpElement: .quad szMessImpElement
|
||||
/********************************************************/
|
||||
/* File Include fonctions */
|
||||
/********************************************************/
|
||||
/* for this file see task include a file in language AArch64 assembly */
|
||||
.include "../includeARM64.inc"
|
||||
12
Task/Josephus-problem/ALGOL-68/josephus-problem.alg
Normal file
12
Task/Josephus-problem/ALGOL-68/josephus-problem.alg
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
BEGIN
|
||||
PROC josephus = (INT n, k, m) INT :
|
||||
CO Return m-th on the reversed kill list; m=0 is final survivor. CO
|
||||
BEGIN
|
||||
INT lm := m; CO Local copy of m CO
|
||||
FOR a FROM m+1 WHILE a <= n DO lm := (lm+k) %* a OD;
|
||||
lm
|
||||
END;
|
||||
INT n = 41, k=3;
|
||||
printf (($"n = ", g(0), ", k = ", g(0), ", final survivor: ", g(0)l$,
|
||||
n, k, josephus (n, k, 0)))
|
||||
END
|
||||
12
Task/Josephus-problem/ANSI-BASIC/josephus-problem.basic
Normal file
12
Task/Josephus-problem/ANSI-BASIC/josephus-problem.basic
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
100 FUNCTION josephus (n, k, m)
|
||||
110 ! Return m-th on the reversed kill list; m=0 is final survivor.
|
||||
120 LET lm = m ! Local copy OF m
|
||||
130 FOR a = m+1 TO n
|
||||
140 LET lm = MOD(lm+k, a)
|
||||
150 NEXT a
|
||||
160 LET josephus = lm
|
||||
170 END FUNCTION
|
||||
180 LET n = 41
|
||||
190 LET k=3
|
||||
200 PRINT "n =";n, "k =";k,"final survivor =";josephus(n, k, 0)
|
||||
210 END
|
||||
198
Task/Josephus-problem/ARM-Assembly/josephus-problem.arm
Normal file
198
Task/Josephus-problem/ARM-Assembly/josephus-problem.arm
Normal file
|
|
@ -0,0 +1,198 @@
|
|||
/* ARM assembly AARCH64 Raspberry PI 3B */
|
||||
/* ARM assembly Raspberry PI */
|
||||
/* program josephus.s */
|
||||
|
||||
/* REMARK 1 : this program use routines in a include file
|
||||
see task Include a file language arm assembly
|
||||
for the routine affichageMess conversion10
|
||||
see at end of this program the instruction include */
|
||||
|
||||
/*******************************************/
|
||||
/* Constantes */
|
||||
/*******************************************/
|
||||
.equ STDOUT, 1 @ Linux output console
|
||||
.equ EXIT, 1 @ Linux syscall
|
||||
.equ WRITE, 4 @ Linux syscall
|
||||
.equ BRK, 0x2d @ Linux syscall
|
||||
.equ CHARPOS, '@'
|
||||
|
||||
.equ FIRSTNODE, 0 //identification first node
|
||||
|
||||
/*******************************************/
|
||||
/* Structures */
|
||||
/********************************************/
|
||||
/* structure linkedlist*/
|
||||
.struct 0
|
||||
llist_next: // next element
|
||||
.struct llist_next + 4
|
||||
llist_value: // element value
|
||||
.struct llist_value + 4
|
||||
llist_fin:
|
||||
/*********************************/
|
||||
/* Initialized data */
|
||||
/*********************************/
|
||||
.data
|
||||
szMessDebutPgm: .asciz "Start program.\n"
|
||||
szMessFinPgm: .asciz "Program End ok.\n"
|
||||
szRetourLigne: .asciz "\n"
|
||||
szMessValElement: .asciz "Value : @ \n"
|
||||
szMessListeVide: .asciz "List empty.\n"
|
||||
szMessImpElement: .asciz "Node display: @ Value : @ Next @ \n"
|
||||
szMessErrComm: .asciz "Incomplete Command line : josephus <maxi> <intervalle>\n"
|
||||
/*********************************/
|
||||
/* UnInitialized data */
|
||||
/*********************************/
|
||||
.bss
|
||||
sZoneConv: .skip 24
|
||||
.align 4
|
||||
qDebutListe1: .skip llist_fin
|
||||
/*********************************/
|
||||
/* code section */
|
||||
/*********************************/
|
||||
.text
|
||||
.global main
|
||||
main: // entry of program
|
||||
mov fp,sp // copy stack address register r29 fp
|
||||
ldr r0,iAdrszMessDebutPgm
|
||||
bl affichageMess
|
||||
ldr r0,[fp] // parameter number command line
|
||||
cmp r0,#2 // correct ?
|
||||
ble erreurCommande // error
|
||||
|
||||
add r0,fp,#8 // address parameter 2
|
||||
ldr r0,[r0]
|
||||
bl conversionAtoD
|
||||
add r2,r0,#FIRSTNODE // save maxi
|
||||
add r0,fp,#12 // address parameter 3
|
||||
ldr r0,[r0]
|
||||
bl conversionAtoD
|
||||
mov r8,r0 // save gap
|
||||
|
||||
mov r0,#FIRSTNODE // create first node
|
||||
mov r1,#0
|
||||
bl createNode
|
||||
mov r5,r0 // first node address
|
||||
mov r6,r0
|
||||
mov r4,#FIRSTNODE + 1
|
||||
mov r3,#1
|
||||
1: // loop create others nodes
|
||||
mov r0,r4 // key value
|
||||
mov r1,#0
|
||||
bl createNode
|
||||
str r0,[r6,#llist_next] // store current node address in prev node
|
||||
mov r6,r0
|
||||
add r4,r4,#1
|
||||
add r3,r3,#1
|
||||
cmp r3,r2 // maxi ?
|
||||
blt 1b
|
||||
str r5,[r6,#llist_next] // store first node address in last pointer
|
||||
mov r4,r6
|
||||
2:
|
||||
mov r2,#1 // counter for gap
|
||||
3:
|
||||
ldr r4,[r4,#llist_next]
|
||||
add r2,r2,#1
|
||||
cmp r2,r8 // intervalle ?
|
||||
blt 3b
|
||||
ldr r5,[r4,#llist_next] // removing the node from the list
|
||||
ldr r2,[r5,#llist_value]
|
||||
ldr r7,[r5,#llist_next] // load pointer next
|
||||
str r7,[r4,#llist_next] // ans store in prev node
|
||||
//mov r0,r25
|
||||
//bl displayNode
|
||||
cmp r7,r4
|
||||
moveq r4,r7
|
||||
bne 2b // and loop
|
||||
|
||||
mov r0,r4
|
||||
bl displayNode // display last node
|
||||
|
||||
b 100f
|
||||
erreurCommande:
|
||||
ldr r0,iAdrszMessErrComm
|
||||
bl affichageMess
|
||||
mov r0,#1 // error code
|
||||
b 100f
|
||||
100: // program end standard
|
||||
ldr r0,iAdrszMessFinPgm
|
||||
bl affichageMess
|
||||
mov r0,#0 // return code Ok
|
||||
mov r7,#EXIT // system call "Exit"
|
||||
svc #0
|
||||
|
||||
iAdrszMessDebutPgm: .int szMessDebutPgm
|
||||
iAdrszMessFinPgm: .int szMessFinPgm
|
||||
iAdrszRetourLigne: .int szRetourLigne
|
||||
iAdrqDebutListe1: .int qDebutListe1
|
||||
iAdrszMessErrComm: .int szMessErrComm
|
||||
|
||||
/******************************************************************/
|
||||
/* create node */
|
||||
/******************************************************************/
|
||||
/* r0 contains key */
|
||||
/* r1 contains zero or address next node */
|
||||
/* r0 returns address heap node */
|
||||
createNode:
|
||||
push {r1-r11,lr} // save registers
|
||||
mov r9,r0 // save key
|
||||
mov r10,r1 // save key
|
||||
mov r0,#0 // allocation place heap
|
||||
mov r7,#BRK // call system 'brk'
|
||||
svc #0
|
||||
mov r11,r0 // save address heap for node
|
||||
add r0,r0,#llist_fin // reservation place node length
|
||||
mov r7,#BRK // call system 'brk'
|
||||
svc #0
|
||||
cmp r0,#-1 // allocation error
|
||||
beq 100f
|
||||
|
||||
str r9,[r11,#llist_value]
|
||||
str r10,[r11,#llist_next]
|
||||
mov r0,r11
|
||||
100:
|
||||
pop {r1-r11,lr} // restaur registers
|
||||
bx lr // return
|
||||
|
||||
/******************************************************************/
|
||||
/* display infos node */
|
||||
/******************************************************************/
|
||||
/* r0 contains node address */
|
||||
displayNode:
|
||||
push {r1-r4,lr} // save registers
|
||||
mov r2,r0
|
||||
ldr r1,iAdrsZoneConv
|
||||
bl conversion16
|
||||
mov r4,#0
|
||||
strb r4,[r1,r0] // store zero final
|
||||
ldr r0,iAdrszMessImpElement
|
||||
ldr r1,iAdrsZoneConv
|
||||
bl strInsertAtCharInc
|
||||
mov r3,r0
|
||||
ldr r0,[r2,#llist_value]
|
||||
ldr r1,iAdrsZoneConv
|
||||
bl conversion10S
|
||||
mov r4,#0
|
||||
strb r4,[r1,r0] // store zero final
|
||||
mov r0,r3
|
||||
ldr r1,iAdrsZoneConv
|
||||
bl strInsertAtCharInc
|
||||
mov r3,r0
|
||||
ldr r0,[r2,#llist_next]
|
||||
ldr r1,iAdrsZoneConv
|
||||
bl conversion16
|
||||
mov r4,#0
|
||||
strb r4,[r1,#8] // store zero final
|
||||
mov r0,r3
|
||||
ldr r1,iAdrsZoneConv
|
||||
bl strInsertAtCharInc
|
||||
bl affichageMess
|
||||
|
||||
100:
|
||||
pop {r1-r4,lr} // restaur registers
|
||||
bx lr // return
|
||||
iAdrsZoneConv: .int sZoneConv
|
||||
iAdrszMessImpElement: .int szMessImpElement
|
||||
/***************************************************/
|
||||
/* ROUTINES INCLUDE */
|
||||
/***************************************************/
|
||||
.include "../affichage.inc"
|
||||
37
Task/Josephus-problem/AWK/josephus-problem.awk
Normal file
37
Task/Josephus-problem/AWK/josephus-problem.awk
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
# syntax: GAWK -f JOSEPHUS_PROBLEM.AWK
|
||||
# converted from PL/I
|
||||
BEGIN {
|
||||
main(5,2,1)
|
||||
main(41,3,1)
|
||||
main(41,3,3)
|
||||
exit(0)
|
||||
}
|
||||
function main(n,k,s, dead,errors,found,i,killed,nn,p,survived) {
|
||||
# n - number of prisoners
|
||||
# k - kill every k'th prisoner
|
||||
# s - number of survivors
|
||||
printf("\nn=%d k=%d s=%d\n",n,k,s) # show arguments
|
||||
if (s > n) { print("s>n"); errors++ }
|
||||
if (k <= 0) { print("k<=0"); errors++ }
|
||||
if (errors > 0) { return(0) }
|
||||
nn = n # wrap around boundary
|
||||
p = -1 # start here
|
||||
while (n != s) { # until survivor count is met
|
||||
found = 0 # start looking
|
||||
while (found != k) { # until we have the k-th prisoner
|
||||
if (++p == nn) { p = 0 } # wrap around
|
||||
if (dead[p] != 1) { found++ } # if prisoner is alive increment found
|
||||
}
|
||||
dead[p] = 1 # kill the unlucky one
|
||||
killed = killed p " " # build killed list
|
||||
n-- # reduce size of circle
|
||||
}
|
||||
for (i=0; i<=nn-1; i++) {
|
||||
if (dead[i] != 1) {
|
||||
survived = survived i " " # build survivor list
|
||||
}
|
||||
}
|
||||
printf("killed: %s\n",killed)
|
||||
printf("survived: %s\n",survived)
|
||||
return(1)
|
||||
}
|
||||
43
Task/Josephus-problem/Ada/josephus-problem.ada
Normal file
43
Task/Josephus-problem/Ada/josephus-problem.ada
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
with Ada.Command_Line, Ada.Text_IO;
|
||||
|
||||
procedure Josephus is
|
||||
|
||||
function Arg(Idx, Default: Positive) return Positive is -- read Argument(Idx)
|
||||
(if Ada.Command_Line.Argument_Count >= Index
|
||||
then Positive'Value(Ada.Command_Line.Argument(Index)) else Default);
|
||||
|
||||
Prisoners: constant Positive := Arg(Idx => 1, Default => 41);
|
||||
Steps: constant Positive := Arg(Idx => 2, Default => 3);
|
||||
Survivors: constant Positive := Arg(Idx => 3, Default => 1);
|
||||
Print: Boolean := (Arg(Idx => 4, Default => 1) = 1);
|
||||
|
||||
subtype Index_Type is Natural range 0 .. Prisoners-1;
|
||||
Next: array(Index_Type) of Index_Type;
|
||||
X: Index_Type := (Steps-2) mod Prisoners;
|
||||
|
||||
begin
|
||||
Ada.Text_IO.Put_Line
|
||||
("N =" & Positive'Image(Prisoners) & ", K =" & Positive'Image(Steps) &
|
||||
(if Survivors > 1 then ", #survivors =" & Positive'Image(Survivors)
|
||||
else ""));
|
||||
for Idx in Next'Range loop -- initialize Next
|
||||
Next(Idx) := (Idx+1) mod Prisoners;
|
||||
end loop;
|
||||
if Print then
|
||||
Ada.Text_IO.Put("Executed: ");
|
||||
end if;
|
||||
for Execution in reverse 1 .. Prisoners loop
|
||||
if Execution = Survivors then
|
||||
Ada.Text_IO.New_Line;
|
||||
Ada.Text_IO.Put("Surviving: ");
|
||||
Print := True;
|
||||
end if;
|
||||
if Print then
|
||||
Ada.Text_IO.Put(Positive'Image(Next(X)));
|
||||
end if;
|
||||
Next(X) := Next(Next(X)); -- "delete" a prisoner
|
||||
for Prisoner in 1 .. Steps-1 loop
|
||||
X := Next(X);
|
||||
end loop;
|
||||
end loop;
|
||||
end Josephus;
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
on josephus(n, k)
|
||||
set m to 0
|
||||
repeat with i from 2 to n
|
||||
set m to (m + k) mod i
|
||||
end repeat
|
||||
|
||||
return m + 1
|
||||
end josephus
|
||||
|
||||
josephus(41, 3) --> 31
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
on josephus(n, k, s)
|
||||
script o
|
||||
property living : {}
|
||||
end script
|
||||
|
||||
repeat with i from 1 to n
|
||||
set end of o's living to i
|
||||
end repeat
|
||||
|
||||
set startPosition to k
|
||||
repeat until (n = s) -- Keep going round the circle until only s prisoners remain.
|
||||
set circleSize to n
|
||||
if (n < k) then
|
||||
set i to (startPosition - 1) mod circleSize + 1
|
||||
set item i of o's living to missing value
|
||||
set n to n - 1
|
||||
else
|
||||
repeat with i from startPosition to circleSize by k
|
||||
set item i of o's living to missing value
|
||||
set n to n - 1
|
||||
if (n = s) then exit repeat
|
||||
end repeat
|
||||
end if
|
||||
set startPosition to i + k - circleSize
|
||||
set o's living to o's living's integers
|
||||
end repeat
|
||||
|
||||
return o's living
|
||||
end josephus
|
||||
|
||||
josephus(41, 3, 1) --> {31}
|
||||
josephus(41, 3, 6) --> {2, 4, 16, 22, 31, 35}
|
||||
142
Task/Josephus-problem/AppleScript/josephus-problem-3.applescript
Normal file
142
Task/Josephus-problem/AppleScript/josephus-problem-3.applescript
Normal file
|
|
@ -0,0 +1,142 @@
|
|||
-- josephusSurvivor :: Int -> Int -> Int
|
||||
on josephusSurvivor(n, k)
|
||||
script go
|
||||
on |λ|(x, a)
|
||||
(k + x) mod a
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
foldl(go, 0, enumFromTo(1, n))
|
||||
end josephusSurvivor
|
||||
|
||||
|
||||
-- josephusSequence :: Int -> Int -> [Int]
|
||||
on josephusSequence(n, k)
|
||||
script josephus
|
||||
on |λ|(m, xs)
|
||||
if 0 ≠ m then
|
||||
set {l, r} to splitAt((k - 1) mod m, xs)
|
||||
{item 1 of r} & |λ|(m - 1, rest of r & l)
|
||||
else
|
||||
{}
|
||||
end if
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
|λ|(n, enumFromTo(0, n - 1)) of josephus
|
||||
end josephusSequence
|
||||
|
||||
|
||||
--------------------------- TEST ---------------------------
|
||||
on run
|
||||
unlines({"Josephus survivor -> " & str(josephusSurvivor(41, 3)), ¬
|
||||
"Josephus sequence ->" & linefeed & tab & ¬
|
||||
showList(josephusSequence(41, 3))})
|
||||
end run
|
||||
|
||||
|
||||
---------------- REUSABLE GENERIC FUNCTIONS ----------------
|
||||
|
||||
-- enumFromTo :: Int -> Int -> [Int]
|
||||
on enumFromTo(m, n)
|
||||
if m ≤ n then
|
||||
set lst to {}
|
||||
repeat with i from m to n
|
||||
set end of lst to i
|
||||
end repeat
|
||||
lst
|
||||
else
|
||||
{}
|
||||
end if
|
||||
end enumFromTo
|
||||
|
||||
-- foldl :: (a -> b -> a) -> a -> [b] -> a
|
||||
on foldl(f, startValue, xs)
|
||||
tell mReturn(f)
|
||||
set v to startValue
|
||||
set lng to length of xs
|
||||
repeat with i from 1 to lng
|
||||
set v to |λ|(v, item i of xs, i, xs)
|
||||
end repeat
|
||||
return v
|
||||
end tell
|
||||
end foldl
|
||||
|
||||
-- map :: (a -> b) -> [a] -> [b]
|
||||
on map(f, xs)
|
||||
-- The list obtained by applying f
|
||||
-- to each element of xs.
|
||||
tell mReturn(f)
|
||||
set lng to length of xs
|
||||
set lst to {}
|
||||
repeat with i from 1 to lng
|
||||
set end of lst to |λ|(item i of xs, i, xs)
|
||||
end repeat
|
||||
return lst
|
||||
end tell
|
||||
end map
|
||||
|
||||
-- mReturn :: First-class m => (a -> b) -> m (a -> b)
|
||||
on mReturn(f)
|
||||
-- 2nd class handler function lifted into 1st class script wrapper.
|
||||
if script is class of f then
|
||||
f
|
||||
else
|
||||
script
|
||||
property |λ| : f
|
||||
end script
|
||||
end if
|
||||
end mReturn
|
||||
|
||||
-- intercalate :: String -> [String] -> String
|
||||
on intercalate(delim, xs)
|
||||
set {dlm, my text item delimiters} to ¬
|
||||
{my text item delimiters, delim}
|
||||
set str to xs as text
|
||||
set my text item delimiters to dlm
|
||||
str
|
||||
end intercalate
|
||||
|
||||
-- showList :: [a] -> String
|
||||
on showList(xs)
|
||||
script show
|
||||
on |λ|(x)
|
||||
x as text
|
||||
end |λ|
|
||||
end script
|
||||
"[" & intercalate(",", map(show, xs)) & "]"
|
||||
end showList
|
||||
|
||||
-- splitAt :: Int -> [a] -> ([a], [a])
|
||||
on splitAt(n, xs)
|
||||
if n > 0 and n < length of xs then
|
||||
if class of xs is text then
|
||||
{items 1 thru n of xs as text, ¬
|
||||
items (n + 1) thru -1 of xs as text}
|
||||
else
|
||||
{items 1 thru n of xs, items (n + 1) thru -1 of xs}
|
||||
end if
|
||||
else
|
||||
if n < 1 then
|
||||
{{}, xs}
|
||||
else
|
||||
{xs, {}}
|
||||
end if
|
||||
end if
|
||||
end splitAt
|
||||
|
||||
-- str :: a -> String
|
||||
on str(x)
|
||||
x as string
|
||||
end str
|
||||
|
||||
-- unlines :: [String] -> String
|
||||
on unlines(xs)
|
||||
-- A single string formed by the intercalation
|
||||
-- of a list of strings with the newline character.
|
||||
set {dlm, my text item delimiters} to ¬
|
||||
{my text item delimiters, linefeed}
|
||||
set str to xs as text
|
||||
set my text item delimiters to dlm
|
||||
str
|
||||
end unlines
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
10 DEF FN MOD(X) = X - INT (X / A) * A
|
||||
20 LM = 0: INPUT "GIVE N AND K (N,K): ";N,K
|
||||
30 IF N < 1 or K < 1 THEN GOTO 20
|
||||
40 FOR A = 1 TO N: LM = FN MOD(LM + K): NEXT A
|
||||
50 PRINT "N = ";N;", K = ";K;", SURVIVOR: ";LM
|
||||
20
Task/Josephus-problem/Arturo/josephus-problem.arturo
Normal file
20
Task/Josephus-problem/Arturo/josephus-problem.arturo
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
josephus: function [n,k][
|
||||
p: new 0..n-1
|
||||
i: 0
|
||||
seq: []
|
||||
|
||||
while [0 < size p][
|
||||
i: (i+k-1) % size p
|
||||
append 'seq p\[i]
|
||||
remove 'p .index i
|
||||
]
|
||||
print ["Prisoner killing order:" chop seq]
|
||||
print ["Survivor:" last seq]
|
||||
print ""
|
||||
]
|
||||
|
||||
print "josephus 5 2 =>"
|
||||
josephus 5 2
|
||||
|
||||
print "josephus 41 3 =>"
|
||||
josephus 41 3
|
||||
24
Task/Josephus-problem/AutoHotkey/josephus-problem-1.ahk
Normal file
24
Task/Josephus-problem/AutoHotkey/josephus-problem-1.ahk
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
; Since AutoHotkey is 1-based, we're numbering prisoners 1-41.
|
||||
nPrisoners := 41
|
||||
kth := 3
|
||||
|
||||
; Build a list, purposefully ending with a separator
|
||||
Loop % nPrisoners
|
||||
list .= A_Index . "|"
|
||||
|
||||
; iterate and remove from list
|
||||
i := 1
|
||||
Loop
|
||||
{
|
||||
; Step by 2; the third step was done by removing the previous prisoner
|
||||
i += kth - 1
|
||||
if (i > nPrisoners)
|
||||
i := Mod(i, nPrisoners)
|
||||
; Remove from list
|
||||
end := InStr(list, "|", 0, 1, i)
|
||||
bgn := InStr(list, "|", 0, 1, i-1)
|
||||
list := SubStr(list, 1, bgn) . SubStr(list, end+1)
|
||||
nPrisoners--
|
||||
}
|
||||
Until (nPrisoners = 1)
|
||||
MsgBox % RegExReplace(list, "\|") ; remove the final separator
|
||||
20
Task/Josephus-problem/AutoHotkey/josephus-problem-2.ahk
Normal file
20
Task/Josephus-problem/AutoHotkey/josephus-problem-2.ahk
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
nPrisoners := 41
|
||||
kth := 3
|
||||
list := []
|
||||
|
||||
; Build a list of 41 items
|
||||
Loop % nPrisoners
|
||||
list.insert(A_Index)
|
||||
|
||||
; iterate and remove from list
|
||||
i := 1
|
||||
Loop
|
||||
{
|
||||
; Step by 3
|
||||
i += kth - 1
|
||||
if (i > list.MaxIndex())
|
||||
i := Mod(i, list.MaxIndex())
|
||||
list.remove(i)
|
||||
}
|
||||
Until (list.MaxIndex() = 1)
|
||||
MsgBox % list.1 ; there is only 1 element left
|
||||
7
Task/Josephus-problem/BASIC/josephus-problem.basic
Normal file
7
Task/Josephus-problem/BASIC/josephus-problem.basic
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
10 N=41
|
||||
20 K=3
|
||||
30 M=0
|
||||
40 FOR I=M+1 TO N
|
||||
50 M=INT(I*((M+K)/I-INT((M+K)/I))+0.5)
|
||||
60 NEXT I
|
||||
70 PRINT "Survivor is number";M
|
||||
13
Task/Josephus-problem/BASIC256/josephus-problem.basic
Normal file
13
Task/Josephus-problem/BASIC256/josephus-problem.basic
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
n = 41 #prisoners
|
||||
k = 3 #order of execution
|
||||
|
||||
print "n = "; n, "k = "; k, "final survivor = "; Josephus(n, k, 0)
|
||||
end
|
||||
|
||||
function Josephus(n, k, m)
|
||||
lm = m
|
||||
for i = m + 1 to n
|
||||
lm = (lm + k) mod i
|
||||
next
|
||||
return lm
|
||||
end function
|
||||
10
Task/Josephus-problem/BBC-BASIC/josephus-problem.basic
Normal file
10
Task/Josephus-problem/BBC-BASIC/josephus-problem.basic
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
REM >josephus
|
||||
PRINT "Survivor is number "; FNjosephus(41, 3, 0)
|
||||
END
|
||||
:
|
||||
DEF FNjosephus(n%, k%, m%)
|
||||
LOCAL i%
|
||||
FOR i% = m% + 1 TO n%
|
||||
m% = (m% + k%) MOD i%
|
||||
NEXT
|
||||
= m%
|
||||
31
Task/Josephus-problem/Batch-File/josephus-problem.bat
Normal file
31
Task/Josephus-problem/Batch-File/josephus-problem.bat
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
@echo off
|
||||
setlocal enabledelayedexpansion
|
||||
|
||||
set "prison=41" %== Number of prisoners ==%
|
||||
set "step=3" %== The step... ==%
|
||||
set "survive=1" %== Number of survivors ==%
|
||||
call :josephus
|
||||
|
||||
set "prison=41"
|
||||
set "step=3"
|
||||
set "survive=3"
|
||||
call :josephus
|
||||
pause
|
||||
exit /b 0
|
||||
|
||||
%== The Procedure ==%
|
||||
:josephus
|
||||
set "surv_list="
|
||||
for /l %%S in (!survive!,-1,1) do (
|
||||
set /a "m = %%S - 1"
|
||||
for /l %%X in (%%S,1,!prison!) do (
|
||||
set /a "m = (m + step) %% %%X"
|
||||
)
|
||||
if defined surv_list (
|
||||
set "surv_list=!surv_list! !m!"
|
||||
) else (
|
||||
set "surv_list=!m!"
|
||||
)
|
||||
)
|
||||
echo !surv_list!
|
||||
goto :EOF
|
||||
4
Task/Josephus-problem/Befunge/josephus-problem.bf
Normal file
4
Task/Josephus-problem/Befunge/josephus-problem.bf
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
>0" :srenosirP">:#,_&>>00p>>v
|
||||
v0p01<&_,#!>#:<"Step size: "<
|
||||
>1+:20p00g`!#v_0" :rovivru"v
|
||||
^g02%g02+g01<<@.$_,#!>#:<"S"<
|
||||
77
Task/Josephus-problem/C++/josephus-problem.cpp
Normal file
77
Task/Josephus-problem/C++/josephus-problem.cpp
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
using namespace std;
|
||||
typedef unsigned long long bigint;
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
class josephus
|
||||
{
|
||||
public:
|
||||
bigint findSurvivors( bigint n, bigint k, bigint s = 0 )
|
||||
{
|
||||
bigint i = s + 1;
|
||||
for( bigint x = i; x <= n; x++, i++ )
|
||||
s = ( s + k ) % i;
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
void getExecutionList( bigint n, bigint k, bigint s = 1 )
|
||||
{
|
||||
cout << endl << endl << "Execution list: " << endl;
|
||||
|
||||
prisoners.clear();
|
||||
for( bigint x = 0; x < n; x++ )
|
||||
prisoners.push_back( x );
|
||||
|
||||
bigint index = 0;
|
||||
while( prisoners.size() > s )
|
||||
{
|
||||
index += k - 1;
|
||||
if( index >= prisoners.size() ) index %= prisoners.size();
|
||||
cout << prisoners[static_cast<unsigned int>( index )] << ", ";
|
||||
|
||||
vector<bigint>::iterator it = prisoners.begin() + static_cast<unsigned int>( index );
|
||||
prisoners.erase( it );
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
vector<bigint> prisoners;
|
||||
};
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
int main( int argc, char* argv[] )
|
||||
{
|
||||
josephus jo;
|
||||
bigint n, k, s;
|
||||
while( true )
|
||||
{
|
||||
system( "cls" );
|
||||
cout << "Number of prisoners( 0 to QUIT ): "; cin >> n;
|
||||
if( !n ) return 0;
|
||||
cout << "Execution step: "; cin >> k;
|
||||
cout << "How many survivors: "; cin >> s;
|
||||
|
||||
cout << endl << "Survivor";
|
||||
if( s == 1 )
|
||||
{
|
||||
cout << ": " << jo.findSurvivors( n, k );
|
||||
jo.getExecutionList( n, k );
|
||||
}
|
||||
else
|
||||
{
|
||||
cout << "s: ";
|
||||
for( bigint x = 0; x < s; x++ )
|
||||
cout << jo.findSurvivors( n, k, x ) << ", ";
|
||||
|
||||
jo.getExecutionList( n, k, s );
|
||||
}
|
||||
|
||||
cout << endl << endl;
|
||||
system( "pause" );
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
80
Task/Josephus-problem/C-sharp/josephus-problem.cs
Normal file
80
Task/Josephus-problem/C-sharp/josephus-problem.cs
Normal file
|
|
@ -0,0 +1,80 @@
|
|||
namespace Josephus
|
||||
{
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public class Program
|
||||
{
|
||||
public static int[] JosephusProblem(int n, int m)
|
||||
{
|
||||
var circle = new List<int>();
|
||||
var order = new int[n];
|
||||
|
||||
for (var i = 0; i < n; ++i)
|
||||
{
|
||||
circle.Add(i);
|
||||
}
|
||||
|
||||
var l = 0;
|
||||
var j = 0;
|
||||
var k = 0;
|
||||
|
||||
while (circle.Count != 0)
|
||||
{
|
||||
j++;
|
||||
if (j == m)
|
||||
{
|
||||
order[k] = circle[l];
|
||||
circle.RemoveAt(l);
|
||||
|
||||
k++;
|
||||
l--;
|
||||
j = 0;
|
||||
}
|
||||
|
||||
if (k == n - 1)
|
||||
{
|
||||
order[k] = circle[0];
|
||||
circle.RemoveAt(0);
|
||||
}
|
||||
|
||||
if (l == circle.Count - 1)
|
||||
{
|
||||
l = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
l++;
|
||||
}
|
||||
}
|
||||
|
||||
return order;
|
||||
}
|
||||
|
||||
static void Main(string[] args)
|
||||
{
|
||||
try
|
||||
{
|
||||
var n = 7;
|
||||
var m = 2;
|
||||
|
||||
var result = JosephusProblem(n, m);
|
||||
|
||||
for (var i = 0; i < result.Length; i++)
|
||||
{
|
||||
Console.WriteLine(result[i]);//1 3 5 0 4 2 6
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Console.ReadLine();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
46
Task/Josephus-problem/C/josephus-problem.c
Normal file
46
Task/Josephus-problem/C/josephus-problem.c
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
#include <stdio.h>
|
||||
|
||||
// m-th on the reversed kill list; m = 0 is final survivor
|
||||
int jos(int n, int k, int m) {
|
||||
int a;
|
||||
for (a = m + 1; a <= n; a++)
|
||||
m = (m + k) % a;
|
||||
return m;
|
||||
}
|
||||
|
||||
typedef unsigned long long xint;
|
||||
|
||||
// same as jos(), useful if n is large and k is not
|
||||
xint jos_large(xint n, xint k, xint m) {
|
||||
if (k <= 1) return n - m - 1;
|
||||
|
||||
xint a = m;
|
||||
while (a < n) {
|
||||
xint q = (a - m + k - 2) / (k - 1);
|
||||
|
||||
if (a + q > n) q = n - a;
|
||||
else if (!q) q = 1;
|
||||
|
||||
m = (m + q * k) % (a += q);
|
||||
}
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
xint n, k, i;
|
||||
|
||||
n = 41;
|
||||
k = 3;
|
||||
printf("n = %llu, k = %llu, final survivor: %d\n", n, k, jos(n, k, 0));
|
||||
|
||||
n = 9876543210987654321ULL;
|
||||
k = 12031;
|
||||
printf("n = %llu, k = %llu, three survivors:", n, k);
|
||||
|
||||
for (i = 3; i--; )
|
||||
printf(" %llu", jos_large(n, k, i));
|
||||
putchar('\n');
|
||||
|
||||
return 0;
|
||||
}
|
||||
11
Task/Josephus-problem/Chipmunk-Basic/josephus-problem.basic
Normal file
11
Task/Josephus-problem/Chipmunk-Basic/josephus-problem.basic
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
100 n = 41
|
||||
110 k = 3
|
||||
120 print "n = ";n,"k = ";k,"final survivor = ";josephus(n,k,0)
|
||||
130 end
|
||||
140 function josephus(n,k,m)
|
||||
150 lm = m
|
||||
160 for i = m+1 to n
|
||||
170 lm = (lm+k) mod i
|
||||
180 next
|
||||
190 josephus = lm
|
||||
200 end function
|
||||
13
Task/Josephus-problem/Clojure/josephus-problem.clj
Normal file
13
Task/Josephus-problem/Clojure/josephus-problem.clj
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
(defn rotate [n s] (lazy-cat (drop n s) (take n s)))
|
||||
|
||||
(defn josephus [n k]
|
||||
(letfn [(survivor [[ h & r :as l] k]
|
||||
(cond (empty? r) h
|
||||
:else (survivor (rest (rotate (dec k) l)) k)))]
|
||||
(survivor (range n) k)))
|
||||
|
||||
(let [n 41 k 3]
|
||||
(println (str "Given " n " prisoners in a circle numbered 1.." n
|
||||
", an executioner moving around the"))
|
||||
(println (str "circle " k " at a time will leave prisoner number "
|
||||
(inc (josephus n k)) " as the last survivor.")))
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
(defun kill (n k &aux (m 0))
|
||||
(loop for a from (1+ m) upto n do
|
||||
(setf m (mod (+ m k) a)))
|
||||
m)
|
||||
22
Task/Josephus-problem/Common-Lisp/josephus-problem-2.lisp
Normal file
22
Task/Josephus-problem/Common-Lisp/josephus-problem-2.lisp
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
(defun make-circular-list (n)
|
||||
(let* ((list (loop for i below n
|
||||
collect i))
|
||||
(last (last list)))
|
||||
(setf (cdr last) list)
|
||||
list))
|
||||
|
||||
(defun kill (n d)
|
||||
(let ((list (make-circular-list n)))
|
||||
(flet ((one-element-clist-p (list)
|
||||
(eq list (cdr list)))
|
||||
(move-forward ()
|
||||
(loop repeat (1- d)
|
||||
until (eq list (cdr list))
|
||||
do (setf list (cdr list))))
|
||||
(kill-item ()
|
||||
(setf (car list) (cadr list)
|
||||
(cdr list) (cddr list))))
|
||||
(loop until (one-element-clist-p list) do
|
||||
(move-forward)
|
||||
(kill-item))
|
||||
(first list))))
|
||||
28
Task/Josephus-problem/Craft-Basic/josephus-problem.basic
Normal file
28
Task/Josephus-problem/Craft-Basic/josephus-problem.basic
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
'using 1 to n
|
||||
|
||||
define prisoners = 0, step = 0, killcount = 0, survivor = 0
|
||||
define fn (josephus) as ( survivor + step ) % killcount
|
||||
|
||||
do
|
||||
|
||||
input "Prisoners", prisoners
|
||||
input "Step", step
|
||||
|
||||
gosub executioner
|
||||
|
||||
loop
|
||||
|
||||
sub executioner
|
||||
|
||||
let killcount = 1
|
||||
|
||||
do
|
||||
|
||||
let killcount = killcount + 1
|
||||
let survivor = (josephus)
|
||||
|
||||
loop killcount < prisoners
|
||||
|
||||
print "survivor = ", survivor
|
||||
|
||||
return
|
||||
6
Task/Josephus-problem/Crystal/josephus-problem.crystal
Normal file
6
Task/Josephus-problem/Crystal/josephus-problem.crystal
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
n = ARGV.fetch(0, 41).to_i # n default is 41 or ARGV[0]
|
||||
k = ARGV.fetch(1, 3).to_i # k default is 3 or ARGV[1]
|
||||
|
||||
prisoners = (0...n).to_a
|
||||
while prisoners.size > 1; prisoners.rotate!(k-1).shift end
|
||||
puts "From #{n} prisoners, eliminating each prisoner #{k} leaves prisoner #{prisoners.first}."
|
||||
27
Task/Josephus-problem/D/josephus-problem.d
Normal file
27
Task/Josephus-problem/D/josephus-problem.d
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
import std.stdio, std.algorithm, std.array, std.string, std.range;
|
||||
|
||||
T pop(T)(ref T[] items, in size_t i) pure /*nothrow*/ @safe /*@nogc*/ {
|
||||
auto aux = items[i];
|
||||
items = items.remove(i);
|
||||
return aux;
|
||||
}
|
||||
|
||||
string josephus(in int n, in int k) pure /*nothrow*/ @safe {
|
||||
auto p = n.iota.array;
|
||||
int i;
|
||||
immutable(int)[] seq;
|
||||
while (!p.empty) {
|
||||
i = (i + k - 1) % p.length;
|
||||
seq ~= p.pop(i);
|
||||
}
|
||||
|
||||
return format("Prisoner killing order:\n%(%(%d %)\n%)." ~
|
||||
"\nSurvivor: %d",
|
||||
seq[0 .. $ - 1].chunks(20), seq[$ - 1]);
|
||||
}
|
||||
|
||||
void main() /*@safe*/ {
|
||||
josephus(5, 2).writeln;
|
||||
writeln;
|
||||
josephus(41, 3).writeln;
|
||||
}
|
||||
54
Task/Josephus-problem/Delphi/josephus-problem-1.delphi
Normal file
54
Task/Josephus-problem/Delphi/josephus-problem-1.delphi
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
type TIntArray = array of integer;
|
||||
|
||||
procedure GetJosephusSequence(N,K: integer; var IA: TIntArray);
|
||||
{Analyze sequence of deleting every K of N numbers}
|
||||
{Retrun result in Integer Array}
|
||||
var LS: TList;
|
||||
var I,J: integer;
|
||||
begin
|
||||
SetLength(IA,N);
|
||||
LS:=TList.Create;
|
||||
try
|
||||
{Store number 0..N-1 in list}
|
||||
for I:=0 to N-1 do LS.Add(Pointer(I));
|
||||
J:=0;
|
||||
for I:=0 to N-1 do
|
||||
begin
|
||||
{Advance J by K-1 because iterms are deleted}
|
||||
{And wrapping around if it J exceed the count }
|
||||
J:=(J+K-1) mod LS.Count;
|
||||
{Caption the sequence}
|
||||
IA[I]:=Integer(LS[J]);
|
||||
{Delete (kill) one item}
|
||||
LS.Delete(J);
|
||||
end;
|
||||
finally LS.Free; end;
|
||||
end;
|
||||
|
||||
procedure ShowJosephusProblem(Memo: TMemo; N,K: integer);
|
||||
{Analyze and display one Josephus Problem}
|
||||
var IA: TIntArray;
|
||||
var I: integer;
|
||||
var S: string;
|
||||
const CRLF = #$0D#$0A;
|
||||
begin
|
||||
GetJosephusSequence(N,K,IA);
|
||||
S:='';
|
||||
for I:=0 to High(IA) do
|
||||
begin
|
||||
if I>0 then S:=S+',';
|
||||
if (I mod 12)=11 then S:=S+CRLF+' ';
|
||||
S:=S+IntToStr(IA[I]);
|
||||
end;
|
||||
Memo.Lines.Add('N='+IntToStr(N)+' K='+IntToStr(K));
|
||||
Memo.Lines.Add('Sequence: ['+S+']');
|
||||
Memo.Lines.Add('Survivor: '+IntToStr(IA[High(IA)]));
|
||||
Memo.Lines.Add('');
|
||||
end;
|
||||
|
||||
procedure TestJosephusProblem(Memo: TMemo);
|
||||
{Test suite of Josephus Problems}
|
||||
begin
|
||||
ShowJosephusProblem(Memo,5,2);
|
||||
ShowJosephusProblem(Memo,41,3);
|
||||
end;
|
||||
17
Task/Josephus-problem/Delphi/josephus-problem-2.delphi
Normal file
17
Task/Josephus-problem/Delphi/josephus-problem-2.delphi
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
import std.stdio, std.algorithm, std.range;
|
||||
|
||||
int[][] Josephus(in int n, int k, int s=1) {
|
||||
int[] ks, ps = n.iota.array;
|
||||
for (int i=--k; ps.length>s; i=(i+k)%ps.length) {
|
||||
ks ~= ps[i];
|
||||
ps = remove(ps, i);
|
||||
}
|
||||
writefln("Josephus(%d,%d,%d) -> %(%d %) / %(%d %)%s", n, k, s, ps, ks[0..min($,45)], ks.length<45 ? "" : " ..." );
|
||||
return [ps, ks];
|
||||
}
|
||||
|
||||
void main() {
|
||||
Josephus(5, 2);
|
||||
Josephus(41, 3);
|
||||
Josephus(23482, 3343, 3);
|
||||
}}
|
||||
53
Task/Josephus-problem/ERRE/josephus-problem.erre
Normal file
53
Task/Josephus-problem/ERRE/josephus-problem.erre
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
PROGRAM JOSEPHUS
|
||||
|
||||
!
|
||||
! for rosettacode.org
|
||||
!
|
||||
|
||||
!$INTEGER
|
||||
|
||||
DIM DEAD[100]
|
||||
|
||||
PROCEDURE MAIN(N,K,S->ERRORS)
|
||||
! n - number of prisoners
|
||||
! k - kill every k'th prisoner
|
||||
! s - number of survivors
|
||||
LOCAL KILLED$,SURVIVED$,FOUND,P,NN,I
|
||||
ERRORS=0
|
||||
FOR I=0 TO 100 DO
|
||||
DEAD[I]=0
|
||||
END FOR ! prepare array
|
||||
PRINT("N=";N,"K=";K,"S=";S) ! show arguments
|
||||
IF S>N THEN PRINT("S>N";) ERRORS+=1 END IF
|
||||
IF K<=0 THEN PRINT("K<=0";) ERRORS+=1 END IF
|
||||
IF ERRORS>0 THEN EXIT PROCEDURE END IF
|
||||
NN=N ! wrap around boundary
|
||||
P=-1 ! start here
|
||||
WHILE N<>S DO ! until survivor count is met
|
||||
FOUND=0 ! start looking
|
||||
WHILE FOUND<>K DO ! until we have the k-th prisoner
|
||||
P+=1
|
||||
IF P=NN THEN P=0 END IF ! wrap around
|
||||
IF DEAD[P]<>1 THEN
|
||||
FOUND+=1
|
||||
END IF ! if prisoner is alive increment found
|
||||
END WHILE
|
||||
DEAD[P]=1 ! kill the unlucky one
|
||||
KILLED$=KILLED$+STR$(P) ! build killed list
|
||||
N-=1 ! reduce size of circle
|
||||
END WHILE
|
||||
FOR I=0 TO NN-1 DO
|
||||
IF DEAD[I]<>1 THEN
|
||||
SURVIVED$=SURVIVED$+STR$(I) ! build survivor list
|
||||
END IF
|
||||
END FOR
|
||||
PRINT("Killed:";KILLED$)
|
||||
PRINT("Survived:";SURVIVED$)
|
||||
END PROCEDURE
|
||||
|
||||
BEGIN
|
||||
ERRORS=0
|
||||
MAIN(5,2,1->ERRORS)
|
||||
MAIN(41,3,1->ERRORS)
|
||||
MAIN(41,3,3->ERRORS)
|
||||
END PROGRAM
|
||||
8
Task/Josephus-problem/EasyLang/josephus-problem.easy
Normal file
8
Task/Josephus-problem/EasyLang/josephus-problem.easy
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
n = 41
|
||||
k = 3
|
||||
print "prisoners: " & n
|
||||
print "step size: " & k
|
||||
for i = 1 to n
|
||||
lm = (lm + k) mod i
|
||||
.
|
||||
print "final survivor: " & lm
|
||||
13
Task/Josephus-problem/EchoLisp/josephus-problem-1.l
Normal file
13
Task/Josephus-problem/EchoLisp/josephus-problem-1.l
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
;; input
|
||||
(define N 41)
|
||||
(define K 3)
|
||||
(define prisoners (apply circular-list (iota N)))
|
||||
(define last-one prisoners) ; current position
|
||||
|
||||
;; kill returns current position = last killed
|
||||
(define (kill lst skip)
|
||||
(cond
|
||||
((eq? (mark? lst) '🔫 )(kill (cdr lst) skip)) ;; dead ? goto next
|
||||
((zero? skip) (mark lst '🔫)) ;; all skipped ? kill
|
||||
(else (mark lst '😥 ) ;; relieved face
|
||||
(kill (cdr lst ) (1- skip))))) ;; skip 1 and goto next
|
||||
22
Task/Josephus-problem/EchoLisp/josephus-problem-2.l
Normal file
22
Task/Josephus-problem/EchoLisp/josephus-problem-2.l
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
;; kill N-1
|
||||
(for ((i (1- N) )) (set! last-one (kill last-one (1- K))))
|
||||
;; look at prisoners
|
||||
prisoners
|
||||
→ ( 🔄 🔫 0 🔫 1 🔫 2 🔫 3 🔫 4 🔫 5 🔫 6 🔫 7 🔫 8 🔫 9 🔫 10 🔫 11 🔫 12 🔫 13 🔫 14 🔫 15 🔫 16
|
||||
🔫 17 🔫 18 🔫 19 🔫 20 🔫 21 🔫 22 🔫 23 🔫 24 🔫 25 🔫 26 🔫 27 🔫 28 🔫 29 😥 30 🔫 31 🔫 32
|
||||
🔫 33 🔫 34 🔫 35 🔫 36 🔫 37 🔫 38 🔫 39 🔫 40 🔫 0 🔫 1 … ∞)
|
||||
|
||||
;; #30 seems happy
|
||||
;; kill last
|
||||
(set! last-one (kill last-one (1- K)))
|
||||
last-one
|
||||
→ ( 🔫 30 🔫 31 🔫 32 …🔃 ) ;; #30 was the last
|
||||
|
||||
;; extra : we want more survivors
|
||||
(define SURVIVORS 3)
|
||||
(for ((i (- N SURVIVORS) )) (set! last-one (kill last-one (1- K))))
|
||||
|
||||
prisoners
|
||||
→ ( 🔄 🔫 0 🔫 1 🔫 2 🔫 3 🔫 4 🔫 5 🔫 6 🔫 7 🔫 8 🔫 9 🔫 10 🔫 11 🔫 12 🔫 13 🔫 14 😥 15 🔫 16
|
||||
🔫 17 🔫 18 🔫 19 🔫 20 🔫 21 🔫 22 🔫 23 🔫 24 🔫 25 🔫 26 🔫 27 🔫 28 🔫 29 😥 30 🔫 31 🔫 32
|
||||
🔫 33 😥 34 🔫 35 🔫 36 🔫 37 🔫 38 🔫 39 🔫 40 🔫 0 🔫 1 🔫 0 … ∞)
|
||||
52
Task/Josephus-problem/Eiffel/josephus-problem.e
Normal file
52
Task/Josephus-problem/Eiffel/josephus-problem.e
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
class
|
||||
APPLICATION
|
||||
|
||||
create
|
||||
make
|
||||
|
||||
feature
|
||||
|
||||
make
|
||||
do
|
||||
io.put_string ("Survivor is prisoner: " + execute (12, 4).out)
|
||||
end
|
||||
|
||||
execute (n, k: INTEGER): INTEGER
|
||||
-- Survivor of 'n' prisoners, when every 'k'th is executed.
|
||||
require
|
||||
n_positive: n > 0
|
||||
k_positive: k > 0
|
||||
n_larger: n > k
|
||||
local
|
||||
killidx: INTEGER
|
||||
prisoners: LINKED_LIST [INTEGER]
|
||||
do
|
||||
create prisoners.make
|
||||
across
|
||||
0 |..| (n - 1) as c
|
||||
loop
|
||||
prisoners.extend (c.item)
|
||||
end
|
||||
io.put_string ("Prisoners are executed in the order:%N")
|
||||
killidx := 1
|
||||
from
|
||||
until
|
||||
prisoners.count <= 1
|
||||
loop
|
||||
killidx := killidx + k - 1
|
||||
from
|
||||
until
|
||||
killidx <= prisoners.count
|
||||
loop
|
||||
killidx := killidx - prisoners.count
|
||||
end
|
||||
io.put_string (prisoners.at (killidx).out + "%N")
|
||||
prisoners.go_i_th (killidx)
|
||||
prisoners.remove
|
||||
end
|
||||
Result := prisoners.at (1)
|
||||
ensure
|
||||
Result_in_range: Result >= 0 and Result < n
|
||||
end
|
||||
|
||||
end
|
||||
15
Task/Josephus-problem/Elixir/josephus-problem.elixir
Normal file
15
Task/Josephus-problem/Elixir/josephus-problem.elixir
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
defmodule Josephus do
|
||||
def find(n,k) do
|
||||
find(Enum.to_list(0..n-1),0..k-2,k..n)
|
||||
end
|
||||
|
||||
def find([_|[r|_]],_,_..d) when d < 3 do
|
||||
IO.inspect r
|
||||
end
|
||||
|
||||
def find(arr,a..c,b..d) when length(arr) >= 3 do
|
||||
find(Enum.slice(arr,b..d) ++ Enum.slice(arr,a..c),a..c,b..d-1)
|
||||
end
|
||||
end
|
||||
|
||||
Josephus.find(41,3)
|
||||
9
Task/Josephus-problem/Emacs-Lisp/josephus-problem.l
Normal file
9
Task/Josephus-problem/Emacs-Lisp/josephus-problem.l
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
(defun jo (n k)
|
||||
(if (= 1 n)
|
||||
1
|
||||
(1+ (% (+ (1- k)
|
||||
(jo (1- n) k))
|
||||
n))))
|
||||
|
||||
(message "%d" (jo 50 2))
|
||||
(message "%d" (jo 60 3))
|
||||
26
Task/Josephus-problem/Erlang/josephus-problem.erl
Normal file
26
Task/Josephus-problem/Erlang/josephus-problem.erl
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
-module( josephus_problem ).
|
||||
|
||||
-export( [general_solution/3, task/0] ).
|
||||
|
||||
general_solution( Prisoners, Kill, Survive ) -> general_solution( Prisoners, Kill, Survive, erlang:length(Prisoners), [] ).
|
||||
|
||||
task() -> general_solution( lists:seq(0, 40), 3, 1 ).
|
||||
|
||||
|
||||
|
||||
general_solution( Prisoners, _Kill, Survive, Survive, Kills ) ->
|
||||
{Prisoners, lists:reverse(Kills)};
|
||||
general_solution( Prisoners, Kill, Survive, Prisoners_length, Kills ) ->
|
||||
{Skipped, [Killed | Rest]} = kill( Kill, Prisoners, Prisoners_length ),
|
||||
general_solution( Rest ++ Skipped, Kill, Survive, Prisoners_length - 1, [Killed | Kills] ).
|
||||
|
||||
kill( Kill, Prisoners, Prisoners_length ) when Kill < Prisoners_length ->
|
||||
lists:split( Kill - 1, Prisoners );
|
||||
kill( Kill, Prisoners, Prisoners_length ) ->
|
||||
kill_few( Kill rem Prisoners_length, Prisoners ).
|
||||
|
||||
kill_few( 0, Prisoners ) ->
|
||||
[Last | Rest] = lists:reverse( Prisoners ),
|
||||
{lists:reverse( Rest ), [Last]};
|
||||
kill_few( Kill, Prisoners ) ->
|
||||
lists:split( Kill - 1, Prisoners ).
|
||||
32
Task/Josephus-problem/FTCBASIC/josephus-problem.basic
Normal file
32
Task/Josephus-problem/FTCBASIC/josephus-problem.basic
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
define prisoners = 0, step = 0, killcount = 0
|
||||
define survivor = 0, remainder = 0
|
||||
|
||||
do
|
||||
|
||||
print "Prisoners: " \
|
||||
input prisoners
|
||||
|
||||
print "Step: " \
|
||||
input step
|
||||
|
||||
gosub executioner
|
||||
|
||||
loop
|
||||
|
||||
sub executioner
|
||||
|
||||
let killcount = 1
|
||||
|
||||
do
|
||||
|
||||
let killcount = killcount + 1
|
||||
let survivor = survivor + step
|
||||
let survivor = survivor / killcount
|
||||
carry survivor
|
||||
|
||||
loop killcount < prisoners
|
||||
|
||||
print "survivor = " \
|
||||
print survivor
|
||||
|
||||
return
|
||||
5
Task/Josephus-problem/Factor/josephus-problem.factor
Normal file
5
Task/Josephus-problem/Factor/josephus-problem.factor
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
USING: kernel locals math math.ranges sequences ;
|
||||
IN: josephus
|
||||
|
||||
:: josephus ( k n -- m )
|
||||
n [1,b] 0 [ [ k + ] dip mod ] reduce ;
|
||||
1
Task/Josephus-problem/Forth/josephus-problem.fth
Normal file
1
Task/Josephus-problem/Forth/josephus-problem.fth
Normal file
|
|
@ -0,0 +1 @@
|
|||
: josephus 0 1 begin dup 41 <= while swap 3 + over mod swap 1+ repeat drop ;
|
||||
22
Task/Josephus-problem/Fortran/josephus-problem.f
Normal file
22
Task/Josephus-problem/Fortran/josephus-problem.f
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
program josephus
|
||||
implicit none
|
||||
integer :: n, i, k, p
|
||||
integer, allocatable :: next(:)
|
||||
read *, n, k
|
||||
allocate(next(0:n - 1))
|
||||
do i = 0, n - 2
|
||||
next(i) = i + 1
|
||||
end do
|
||||
next(n - 1) = 0
|
||||
p = 0
|
||||
do while(next(p) /= p)
|
||||
do i = 1, k - 2
|
||||
p = next(p)
|
||||
end do
|
||||
print *, "Kill", next(p)
|
||||
next(p) = next(next(p))
|
||||
p = next(p)
|
||||
end do
|
||||
print *, "Alive", p
|
||||
deallocate(next)
|
||||
end program
|
||||
12
Task/Josephus-problem/FreeBASIC/josephus-problem.basic
Normal file
12
Task/Josephus-problem/FreeBASIC/josephus-problem.basic
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
Function Josephus (n As Integer, k As Integer, m As Integer) As Integer
|
||||
Dim As Integer lm = m
|
||||
For i As Integer = m + 1 To n
|
||||
lm = (lm + k) Mod i
|
||||
Next i
|
||||
Josephus = lm
|
||||
End Function
|
||||
|
||||
Dim As Integer n = 41 'prisioneros
|
||||
Dim As Integer k = 3 'orden de ejecución
|
||||
|
||||
Print "n ="; n, "k ="; k, "superviviente = "; Josephus(n, k, 0)
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
function execute
|
||||
# If the list is empty, don't do anything.
|
||||
test (count $argv) -ge 2; or return
|
||||
# If the list has only one element, return it
|
||||
if test (count $argv) -eq 2
|
||||
echo $argv[2]
|
||||
return
|
||||
end
|
||||
# Rotate prisoners
|
||||
for i in (seq 2 $argv[1])
|
||||
set argv $argv[1 3..-1 2]
|
||||
end
|
||||
# Mention killed prisoner
|
||||
echo $argv[2]
|
||||
# Kill rest recursively
|
||||
execute $argv[1 3..-1]
|
||||
end
|
||||
|
||||
echo Prisoner (execute 3 (seq 0 40))[-1] survived.
|
||||
|
|
@ -0,0 +1 @@
|
|||
echo Prisoners (execute 3 (seq 0 40))[-3..-1] survived.
|
||||
|
|
@ -0,0 +1 @@
|
|||
echo Prisoner (execute 2 Joe Jack William Averell Rantanplan)[-1] survived.
|
||||
21
Task/Josephus-problem/Frink/josephus-problem.frink
Normal file
21
Task/Josephus-problem/Frink/josephus-problem.frink
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
killingCycle[prisonerCount,killStep = 2] :=
|
||||
{
|
||||
i = 0
|
||||
killed = new array
|
||||
prisoners = array[0 to prisonerCount - 1]
|
||||
while length[prisoners] > 1
|
||||
{
|
||||
i = (i + killStep - 1) mod length[prisoners]
|
||||
killed.push[prisoners.remove[i]] // Remove the killed prisoner from the prisoners array and add it to the killed array.
|
||||
}
|
||||
killedResult = "Killed:"
|
||||
for kill = killed // Loop through the killed array to format it nicely.
|
||||
{
|
||||
killedResult = killedResult + " " + kill
|
||||
}
|
||||
aliveResult = "Alive: " + prisoners@0 // Get the only item left in the array
|
||||
return """$killedResult
|
||||
$aliveResult"""
|
||||
}
|
||||
|
||||
println[killingCycle[41,3]] // Enter in total number of prisoners and the number to skip each cycle
|
||||
13
Task/Josephus-problem/GW-BASIC/josephus-problem.basic
Normal file
13
Task/Josephus-problem/GW-BASIC/josephus-problem.basic
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
10 LET N = 41
|
||||
20 LET K = 3
|
||||
30 LET M = 0
|
||||
40 GOSUB 100
|
||||
50 PRINT "n ="; N, "k ="; K, "final survivor ="; LM
|
||||
60 END
|
||||
100 REM Josephus
|
||||
110 REM Return m-th on the reversed kill list; m=0 is final survivor.
|
||||
120 LET LM = M : REM Local copy of m
|
||||
130 FOR A = M+1 TO N
|
||||
140 LET LM = (LM+K) MOD A: REM MOD function
|
||||
150 NEXT A
|
||||
160 RETURN
|
||||
19
Task/Josephus-problem/Gambas/josephus-problem.gambas
Normal file
19
Task/Josephus-problem/Gambas/josephus-problem.gambas
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
Public Sub Main()
|
||||
|
||||
Dim n As Integer = 41 'prisoners
|
||||
Dim k As Integer = 3 'order of execution
|
||||
|
||||
Print "n = "; n, "k = "; k, "final survivor = "; Josephus(n, k, 0)
|
||||
|
||||
End
|
||||
|
||||
Function Josephus(n As Integer, k As Integer, m As Integer) As Integer
|
||||
|
||||
Dim lm As Integer = m
|
||||
|
||||
For i As Integer = m + 1 To n
|
||||
lm = (lm + k) Mod i
|
||||
Next
|
||||
Return lm
|
||||
|
||||
End Function
|
||||
49
Task/Josephus-problem/Go/josephus-problem.go
Normal file
49
Task/Josephus-problem/Go/josephus-problem.go
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
// basic task function
|
||||
func finalSurvivor(n, k int) int {
|
||||
// argument validation omitted
|
||||
circle := make([]int, n)
|
||||
for i := range circle {
|
||||
circle[i] = i
|
||||
}
|
||||
k--
|
||||
exPos := 0
|
||||
for len(circle) > 1 {
|
||||
exPos = (exPos + k) % len(circle)
|
||||
circle = append(circle[:exPos], circle[exPos+1:]...)
|
||||
}
|
||||
return circle[0]
|
||||
}
|
||||
|
||||
// extra
|
||||
func position(n, k, pos int) int {
|
||||
// argument validation omitted
|
||||
circle := make([]int, n)
|
||||
for i := range circle {
|
||||
circle[i] = i
|
||||
}
|
||||
k--
|
||||
exPos := 0
|
||||
for len(circle) > 1 {
|
||||
exPos = (exPos + k) % len(circle)
|
||||
if pos == 0 {
|
||||
return circle[exPos]
|
||||
}
|
||||
pos--
|
||||
circle = append(circle[:exPos], circle[exPos+1:]...)
|
||||
}
|
||||
return circle[0]
|
||||
}
|
||||
|
||||
func main() {
|
||||
// show basic task function on given test case
|
||||
fmt.Println(finalSurvivor(41, 3))
|
||||
// show extra function on all positions of given test case
|
||||
fmt.Println("Position Prisoner")
|
||||
for i := 0; i < 41; i++ {
|
||||
fmt.Printf("%5d%10d\n", i, position(41, 3, i))
|
||||
}
|
||||
}
|
||||
36
Task/Josephus-problem/Groovy/josephus-problem.groovy
Normal file
36
Task/Josephus-problem/Groovy/josephus-problem.groovy
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
int[] Josephus (int size, int kill, int survivors) {
|
||||
// init user pool
|
||||
def users = new int[size];
|
||||
|
||||
// give initial values such that [0] = 1 (first person) [1] = 2 (second person) etc
|
||||
users.eachWithIndex() {obj, i -> users[i] = i + 1};
|
||||
|
||||
// keep track of which person we are on (ranging from 1 to kill)
|
||||
def person = 1;
|
||||
|
||||
// keep going until we have the desired number of survivors
|
||||
while (users.size() > survivors)
|
||||
{
|
||||
// for each person, if they are the kill'th person, set them to -1 to show eliminated
|
||||
users.eachWithIndex() {obj, i ->
|
||||
if (person++ % kill == 0) {
|
||||
users[i] = -1;
|
||||
}
|
||||
|
||||
// if person overflowed kill then reset back to 1
|
||||
if (person > kill) {person = 1;}
|
||||
}
|
||||
|
||||
// clear out all eliminated persons
|
||||
users = users.findAll{w -> w >= 0};
|
||||
}
|
||||
|
||||
// resulting set is the safe positions
|
||||
return users;
|
||||
}
|
||||
|
||||
// Run some test cases
|
||||
|
||||
println "Final survivor for n = 10201 and k = 17: " + Josephus(10201,17,1)[0];
|
||||
|
||||
println "4 safe spots for n = 10201 and k = 17: " + Josephus(10201,17,4);
|
||||
40
Task/Josephus-problem/Haskell/josephus-problem-1.hs
Normal file
40
Task/Josephus-problem/Haskell/josephus-problem-1.hs
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
import Data.List ((\\))
|
||||
import System.Environment (getArgs)
|
||||
|
||||
prisoners :: Int -> [Int]
|
||||
prisoners n = [0 .. n - 1]
|
||||
|
||||
counter :: Int -> [Int]
|
||||
counter k = cycle [k, k-1 .. 1]
|
||||
|
||||
killList :: [Int] -> [Int] -> ([Int], [Int], [Int])
|
||||
killList xs cs = (killed, survivors, newCs)
|
||||
where
|
||||
(killed, newCs) = kill xs cs []
|
||||
survivors = xs \\ killed
|
||||
kill [] cs rs = (rs, cs)
|
||||
kill (x:xs) (c:cs) rs
|
||||
| c == 1 =
|
||||
let ts = rs ++ [x]
|
||||
in kill xs cs ts
|
||||
| otherwise =
|
||||
kill xs cs rs
|
||||
|
||||
killRecursive :: [Int] -> [Int] -> Int -> ([Int], [Int])
|
||||
killRecursive xs cs m = killR ([], xs, cs)
|
||||
where
|
||||
killR (killed, remaining, counter)
|
||||
| length remaining <= m = (killed, remaining)
|
||||
| otherwise =
|
||||
let (newKilled, newRemaining, newCounter) =
|
||||
killList remaining counter
|
||||
allKilled = killed ++ newKilled
|
||||
in killR (allKilled, newRemaining, newCounter)
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
args <- getArgs
|
||||
case args of
|
||||
[n, k, m] -> print $ snd $ killRecursive (prisoners (read n))
|
||||
(counter (read k)) (read m)
|
||||
_ -> print $ snd $ killRecursive (prisoners 41) (counter 3) 1
|
||||
16
Task/Josephus-problem/Haskell/josephus-problem-2.hs
Normal file
16
Task/Josephus-problem/Haskell/josephus-problem-2.hs
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
jseq :: Int -> Int -> [Int]
|
||||
jseq n k = f n [1 .. n]
|
||||
where
|
||||
f 0 _ = []
|
||||
f m s = x : f (m - 1) (right ++ left)
|
||||
where
|
||||
(left, x:right) = splitAt (mod (k - 1) m) s
|
||||
|
||||
-- the final survivor is ((k + ...((k + ((k + 0)`mod` 1)) `mod` 2) ... ) `mod` n)
|
||||
jos :: Int -> Int -> Int
|
||||
jos n k = 1 + foldl (mod . (k +)) 0 [2 .. n]
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
print $ jseq 41 3
|
||||
print $ jos 10000 100
|
||||
14
Task/Josephus-problem/IS-BASIC/josephus-problem.basic
Normal file
14
Task/Josephus-problem/IS-BASIC/josephus-problem.basic
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
100 PROGRAM "Josephus.bas"
|
||||
110 INPUT PROMPT "Number of prisoners: ":NP
|
||||
120 INPUT PROMPT "Execution step: ":EX
|
||||
130 INPUT PROMPT "How many survivors: ":SU
|
||||
140 PRINT "Survivors:";
|
||||
150 FOR S=0 TO SU-1
|
||||
160 PRINT JOSEPHUS(NP,EX,S);
|
||||
170 NEXT
|
||||
180 DEF JOSEPHUS(N,K,M)
|
||||
190 FOR I=M+1 TO N
|
||||
200 LET M=MOD((M+K),I)
|
||||
210 NEXT
|
||||
220 LET JOSEPHUS=M
|
||||
230 END DEF
|
||||
9
Task/Josephus-problem/Icon/josephus-problem-1.icon
Normal file
9
Task/Josephus-problem/Icon/josephus-problem-1.icon
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
procedure main(A)
|
||||
m := integer(A[1]) | 41
|
||||
c := integer(A[2]) | 3
|
||||
write("With ",m," men, counting to ",c," last position is: ", j(m,c))
|
||||
end
|
||||
|
||||
procedure j(m,c)
|
||||
return if m==1 then 0 else (j(m-1,c)+c)%m
|
||||
end
|
||||
27
Task/Josephus-problem/Icon/josephus-problem-2.icon
Normal file
27
Task/Josephus-problem/Icon/josephus-problem-2.icon
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
procedure main(args)
|
||||
n := total := integer(args[1]) | 41 # Number of people
|
||||
k := count := integer(args[2]) | 3 # Count
|
||||
s := integer(args[3])-1 | 0 # Number to save
|
||||
write("With ",n," people, counting by ",k,", the ",s+1," safe places are:")
|
||||
every write("\t",j(n,k,(n-s) to n))
|
||||
end
|
||||
|
||||
procedure j(n,k,s)
|
||||
a := k*(n-s) + 1
|
||||
q := k/(k-1.0)
|
||||
nk := n*k
|
||||
olda := a
|
||||
while a <= nk do {
|
||||
olda := a
|
||||
a := ceil(a,q)
|
||||
}
|
||||
t := nk - olda
|
||||
return t
|
||||
end
|
||||
|
||||
procedure ceil(a,q)
|
||||
n := a*q
|
||||
if n = integer(n) then return integer(n)
|
||||
n ?:= integer(tab(upto('.'))) + 1
|
||||
return n
|
||||
end
|
||||
2
Task/Josephus-problem/J/josephus-problem-1.j
Normal file
2
Task/Josephus-problem/J/josephus-problem-1.j
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
3 ([ (1 }. <:@[ |. ])^:(1 < #@])^:_ i.@]) 41
|
||||
30
|
||||
7
Task/Josephus-problem/J/josephus-problem-2.j
Normal file
7
Task/Josephus-problem/J/josephus-problem-2.j
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
DropNext=. 1 }. <:@[ |. ]
|
||||
MoreThanOne=. 1 < #@]
|
||||
WhileMoreThanOne=. (^:MoreThanOne f.) (^:_)
|
||||
prisoners=. i.@]
|
||||
|
||||
[ DropNext WhileMoreThanOne prisoners f.
|
||||
[ (1 }. <:@[ |. ])^:(1 < #@])^:_ i.@]
|
||||
15
Task/Josephus-problem/J/josephus-problem-3.j
Normal file
15
Task/Josephus-problem/J/josephus-problem-3.j
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
Josephus =: dyad define NB. explicit form, assume executioner starts at position 0
|
||||
NB. use: SKIP josephus NUMBER_OF_PRISONERS
|
||||
N =: y
|
||||
K =: N | x
|
||||
EXECUTIONER =: 0
|
||||
PRISONERS =: i. N
|
||||
kill =: ] #~ (~: ([: i. #))
|
||||
while. 1 (< #) PRISONERS do.
|
||||
EXECUTIONER =: (# PRISONERS) | <: K + EXECUTIONER
|
||||
PRISONERS =: EXECUTIONER kill PRISONERS
|
||||
end.
|
||||
)
|
||||
|
||||
3 Josephus 41
|
||||
30
|
||||
5
Task/Josephus-problem/J/josephus-problem-4.j
Normal file
5
Task/Josephus-problem/J/josephus-problem-4.j
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
NB. this is a direct translation of the algo from C code above.
|
||||
Josephus2 =: 4 : '(| x&+)/i. - 1+y'
|
||||
|
||||
3 Josephus2 41
|
||||
30
|
||||
40
Task/Josephus-problem/Java/josephus-problem-1.java
Normal file
40
Task/Josephus-problem/Java/josephus-problem-1.java
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
import java.util.ArrayList;
|
||||
|
||||
public class Josephus {
|
||||
public static int execute(int n, int k){
|
||||
int killIdx = 0;
|
||||
ArrayList<Integer> prisoners = new ArrayList<Integer>(n);
|
||||
for(int i = 0;i < n;i++){
|
||||
prisoners.add(i);
|
||||
}
|
||||
System.out.println("Prisoners executed in order:");
|
||||
while(prisoners.size() > 1){
|
||||
killIdx = (killIdx + k - 1) % prisoners.size();
|
||||
System.out.print(prisoners.get(killIdx) + " ");
|
||||
prisoners.remove(killIdx);
|
||||
}
|
||||
System.out.println();
|
||||
return prisoners.get(0);
|
||||
}
|
||||
|
||||
public static ArrayList<Integer> executeAllButM(int n, int k, int m){
|
||||
int killIdx = 0;
|
||||
ArrayList<Integer> prisoners = new ArrayList<Integer>(n);
|
||||
for(int i = 0;i < n;i++){
|
||||
prisoners.add(i);
|
||||
}
|
||||
System.out.println("Prisoners executed in order:");
|
||||
while(prisoners.size() > m){
|
||||
killIdx = (killIdx + k - 1) % prisoners.size();
|
||||
System.out.print(prisoners.get(killIdx) + " ");
|
||||
prisoners.remove(killIdx);
|
||||
}
|
||||
System.out.println();
|
||||
return prisoners;
|
||||
}
|
||||
|
||||
public static void main(String[] args){
|
||||
System.out.println("Survivor: " + execute(41, 3));
|
||||
System.out.println("Survivors: " + executeAllButM(41, 3, 3));
|
||||
}
|
||||
}
|
||||
37
Task/Josephus-problem/Java/josephus-problem-2.java
Normal file
37
Task/Josephus-problem/Java/josephus-problem-2.java
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Josephus {
|
||||
|
||||
public static void main(String[] args) {
|
||||
execute(5, 1);
|
||||
execute(41, 2);
|
||||
execute(23482, 3342, 3);
|
||||
}
|
||||
|
||||
public static int[][] execute(int n, int k) {
|
||||
return execute(n, k, 1);
|
||||
}
|
||||
|
||||
public static int[][] execute(int n, int k, int s) {
|
||||
List<Integer> ps = new ArrayList<Integer>(n);
|
||||
for (int i=0; i<n; i+=1) ps.add(i);
|
||||
List<Integer> ks = new ArrayList<Integer>(n-s);
|
||||
for (int i=k; ps.size()>s; i=(i+k)%ps.size()) ks.add(ps.remove(i));
|
||||
System.out.printf("Josephus(%d,%d,%d) -> %s / %s\n", n, k, s, toString(ps), toString(ks));
|
||||
return new int[][] {
|
||||
ps.stream().mapToInt(Integer::intValue).toArray(),
|
||||
ks.stream().mapToInt(Integer::intValue).toArray()
|
||||
};
|
||||
}
|
||||
|
||||
private static String toString(List <Integer> ls) {
|
||||
String dot = "";
|
||||
if (ls.size() >= 45) {
|
||||
dot = ", ...";
|
||||
ls = ls.subList(0, 45);
|
||||
}
|
||||
String s = ls.toString();
|
||||
return s.substring(1, s.length()-1) + dot;
|
||||
}
|
||||
}
|
||||
27
Task/Josephus-problem/JavaScript/josephus-problem-1.js
Normal file
27
Task/Josephus-problem/JavaScript/josephus-problem-1.js
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
var Josephus = {
|
||||
init: function(n) {
|
||||
this.head = {};
|
||||
var current = this.head;
|
||||
for (var i = 0; i < n-1; i++) {
|
||||
current.label = i+1;
|
||||
current.next = {prev: current};
|
||||
current = current.next;
|
||||
}
|
||||
current.label = n;
|
||||
current.next = this.head;
|
||||
this.head.prev = current;
|
||||
return this;
|
||||
},
|
||||
kill: function(spacing) {
|
||||
var current = this.head;
|
||||
while (current.next !== current) {
|
||||
for (var i = 0; i < spacing-1; i++) {
|
||||
current = current.next;
|
||||
}
|
||||
current.prev.next = current.next;
|
||||
current.next.prev = current.prev;
|
||||
current = current.next;
|
||||
}
|
||||
return current.label;
|
||||
}
|
||||
}
|
||||
7
Task/Josephus-problem/JavaScript/josephus-problem-2.js
Normal file
7
Task/Josephus-problem/JavaScript/josephus-problem-2.js
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
function Josephus(n, k, s) {
|
||||
s = s | 1
|
||||
for (var ps=[], i=n; i--; ) ps[i]=i
|
||||
for (var ks=[], i=--k; ps.length>s; i=(i+k)%ps.length) ks.push(ps.splice(i, 1))
|
||||
document.write((arguments.callee+'').split(/\s|\(/)[1], '(', [].slice.call(arguments, 0), ') -> ', ps, ' / ', ks.length<45?ks:ks.slice(0,45)+',...' , '<br>')
|
||||
return [ps, ks]
|
||||
}
|
||||
12
Task/Josephus-problem/Jq/josephus-problem-1.jq
Normal file
12
Task/Josephus-problem/Jq/josephus-problem-1.jq
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
# A control structure, for convenience:
|
||||
# as soon as "condition" is true, then emit . and stop:
|
||||
def do_until(condition; next):
|
||||
def u: if condition then . else (next|u) end;
|
||||
u;
|
||||
|
||||
# n is the initial number; every k-th prisoner is removed until m remain.
|
||||
# Solution by simulation
|
||||
def josephus(n;k;m):
|
||||
reduce range(0;n) as $i ([]; . + [$i]) # Number the prisoners from 0 to (n-1)
|
||||
| do_until( length < k or length <= m; .[k:] + .[0:k-1] )
|
||||
| do_until( length <= m; (k % length) as $i | .[$i:] + .[0:$i-1] );
|
||||
5
Task/Josephus-problem/Jq/josephus-problem-2.jq
Normal file
5
Task/Josephus-problem/Jq/josephus-problem-2.jq
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
def task(n;k;m):
|
||||
"Survivors for n=\(n), k=\(k), m=\(m): \( josephus(n;k;m) )";
|
||||
|
||||
task(41;3;1),
|
||||
task(23482; 3343; 3)
|
||||
5
Task/Josephus-problem/Julia/josephus-problem-1.julia
Normal file
5
Task/Josephus-problem/Julia/josephus-problem-1.julia
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
using Memoize
|
||||
@memoize josephus(n::Integer, k::Integer, m::Integer=1) = n == m ? collect(0:m .- 1) : mod.(josephus(n - 1, k, m) + k, n)
|
||||
|
||||
@show josephus(41, 3)
|
||||
@show josephus(41, 3, 5)
|
||||
14
Task/Josephus-problem/Julia/josephus-problem-2.julia
Normal file
14
Task/Josephus-problem/Julia/josephus-problem-2.julia
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
function josephus(n::Integer, k::Integer, m::Integer=1)
|
||||
p, i, seq = collect(0:n-1), 0, Vector{typeof(n)}(0)
|
||||
while length(p) > m
|
||||
i = (i + k - 1) % length(p)
|
||||
push!(seq, splice!(p, i + 1))
|
||||
end
|
||||
return seq, p
|
||||
end
|
||||
|
||||
seq, surv = josephus(41, 3)
|
||||
println("Prisoner killing in order: $seq\nSurvivor: $surv")
|
||||
|
||||
seq, surv = josephus(41, 3, 3)
|
||||
println("Prisoner killing in order: $seq\nSurvivor: $surv")
|
||||
33
Task/Josephus-problem/Kotlin/josephus-problem.kotlin
Normal file
33
Task/Josephus-problem/Kotlin/josephus-problem.kotlin
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
// version 1.1.3
|
||||
|
||||
fun josephus(n: Int, k: Int, m: Int): Pair<List<Int>, List<Int>> {
|
||||
require(k > 0 && m > 0 && n > k && n > m)
|
||||
val killed = mutableListOf<Int>()
|
||||
val survived = MutableList(n) { it }
|
||||
var start = k - 1
|
||||
outer@ while (true) {
|
||||
val end = survived.size - 1
|
||||
var i = start
|
||||
var deleted = 0
|
||||
while (i <= end) {
|
||||
killed.add(survived.removeAt(i - deleted))
|
||||
if (survived.size == m) break@outer
|
||||
deleted++
|
||||
i += k
|
||||
}
|
||||
start = i - end - 1
|
||||
}
|
||||
return Pair(survived, killed)
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val triples = listOf(Triple(5, 2, 1), Triple(41, 3, 1), Triple(41, 3, 3))
|
||||
for (triple in triples) {
|
||||
val(n, k, m) = triple
|
||||
println("Prisoners = $n, Step = $m, Survivors = $m")
|
||||
val (survived, killed) = josephus(n, k, m)
|
||||
println("Survived : $survived")
|
||||
println("Kill order : $killed")
|
||||
println()
|
||||
}
|
||||
}
|
||||
24
Task/Josephus-problem/Lua/josephus-problem.lua
Normal file
24
Task/Josephus-problem/Lua/josephus-problem.lua
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
function josephus(n, k, m)
|
||||
local positions={}
|
||||
for i=1,n do
|
||||
table.insert(positions, i-1)
|
||||
end
|
||||
local i,j=1,1
|
||||
local s='Execution order: '
|
||||
while #positions>m do
|
||||
if j==k then
|
||||
s=s .. positions[i] .. ', '
|
||||
table.remove(positions, i)
|
||||
i=i-1
|
||||
end
|
||||
i=i+1
|
||||
j=j+1
|
||||
if i>#positions then i=1 end
|
||||
if j>k then j=1 end
|
||||
end
|
||||
print(s:sub(1,#s-2) .. '.')
|
||||
local s='Survivors: '
|
||||
for _,v in pairs(positions) do s=s .. v .. ', ' end
|
||||
print(s:sub(1,#s-2) .. '.')
|
||||
end
|
||||
josephus(41,3, 1)
|
||||
38
Task/Josephus-problem/MATLAB/josephus-problem.m
Normal file
38
Task/Josephus-problem/MATLAB/josephus-problem.m
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
function [indAlive] = josephus(numPeople,count)
|
||||
% Josephus: Given a circle of numPeople individuals, with a count of count,
|
||||
% find the index (starting at 1) of the survivor [see Josephus Problem]
|
||||
|
||||
%% Definitions:
|
||||
% 0 = dead position
|
||||
% 1 = alive position
|
||||
% index = # of person
|
||||
|
||||
%% Setting up
|
||||
arrPeople = ones(1, numPeople);
|
||||
currInd = 0;
|
||||
|
||||
%% Counting
|
||||
while (length(arrPeople(arrPeople == 1)) > 1) % While more than 1 person is alive
|
||||
counter = 0;
|
||||
while counter ~= count % Counting until we hit the count
|
||||
currInd = currInd + 1; % Move to the next person
|
||||
|
||||
if currInd > numPeople % If overflow, wraparound
|
||||
currInd = currInd - numPeople;
|
||||
end
|
||||
|
||||
if arrPeople(currInd) % If the current person is alive
|
||||
counter = counter + 1; % Add 1 person to the count
|
||||
%fprintf("Index: %d \t| Counter: %d\n", currInd, counter) % Uncomment to display index and counter location
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
arrPeople(currInd) = 0; % Kill the person we reached
|
||||
%fprintf("Killed person %d \n", currInd) % Uncomment to display order of killing
|
||||
%disp(arrPeople) % Uncomment to display current status of people
|
||||
end
|
||||
|
||||
indAlive = find(arrPeople);
|
||||
|
||||
end
|
||||
2
Task/Josephus-problem/Mathematica/josephus-problem.math
Normal file
2
Task/Josephus-problem/Mathematica/josephus-problem.math
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
survivor[n_, k_] := Nest[Most[RotateLeft[#, k]] &, Range[0, n - 1], n - 1]
|
||||
survivor[41, 3]
|
||||
12
Task/Josephus-problem/Minimal-BASIC/josephus-problem.basic
Normal file
12
Task/Josephus-problem/Minimal-BASIC/josephus-problem.basic
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
10 LET N = 41
|
||||
20 LET K = 3
|
||||
30 LET M = 0
|
||||
40 GOSUB 100
|
||||
50 PRINT "N ="; N, "K ="; K, "FINAL SURVIVOR ="; S
|
||||
60 GOTO 150
|
||||
100 LET S = M
|
||||
110 FOR A = M+1 TO N
|
||||
120 LET S = INT(A * ((S+K) / A - INT((S+K) / A)) + 0.5)
|
||||
130 NEXT A
|
||||
140 RETURN
|
||||
150 END
|
||||
26
Task/Josephus-problem/Modula-2/josephus-problem.mod2
Normal file
26
Task/Josephus-problem/Modula-2/josephus-problem.mod2
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
MODULE Josephus;
|
||||
FROM FormatString IMPORT FormatString;
|
||||
FROM Terminal IMPORT WriteString,WriteLn,ReadChar;
|
||||
|
||||
PROCEDURE Josephus(n,k : INTEGER) : INTEGER;
|
||||
VAR a,m : INTEGER;
|
||||
BEGIN
|
||||
m := 0;
|
||||
FOR a:=1 TO n DO
|
||||
m := (m + k) MOD a;
|
||||
END;
|
||||
RETURN m
|
||||
END Josephus;
|
||||
|
||||
VAR
|
||||
buf : ARRAY[0..63] OF CHAR;
|
||||
n,k,i : INTEGER;
|
||||
nl,kl,il : LONGCARD;
|
||||
BEGIN
|
||||
n := 41;
|
||||
k := 3;
|
||||
FormatString("n = %i, k = %i, final survivor: %i\n", buf, n, k, Josephus(n, k));
|
||||
WriteString(buf);
|
||||
|
||||
ReadChar
|
||||
END Josephus.
|
||||
16
Task/Josephus-problem/Nanoquery/josephus-problem.nanoquery
Normal file
16
Task/Josephus-problem/Nanoquery/josephus-problem.nanoquery
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
def j(n, k)
|
||||
p = list(range(0, n-1))
|
||||
i = 0
|
||||
seq = {}
|
||||
while len(p) > 0
|
||||
i = (i+k-1) % len(p)
|
||||
seq.append(p[i])
|
||||
p.remove(i)
|
||||
end
|
||||
sur = seq[len(seq) - 1]; seq.remove(len(seq) - 1)
|
||||
return format("Prisoner killing order: %s\nSurvivor: %d", seq, sur)
|
||||
end
|
||||
|
||||
println j(5,2)
|
||||
println
|
||||
println j(41,3)
|
||||
33
Task/Josephus-problem/NetRexx/josephus-problem.netrexx
Normal file
33
Task/Josephus-problem/NetRexx/josephus-problem.netrexx
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
/* NetRexx */
|
||||
options replace format comments java crossref symbols nobinary
|
||||
|
||||
/* REXX **************************************************************
|
||||
* 15.11.2012 Walter Pachl - my own solution
|
||||
* 16.11.2012 Walter Pachl generalized n prisoners + w killing distance
|
||||
* and s=number of survivors
|
||||
**********************************************************************/
|
||||
dead = 0 /* nobody's dead yet */
|
||||
n = 41 /* number of alive prisoners */
|
||||
nn = n /* wrap around boundary */
|
||||
w = 3 /* killing count */
|
||||
s = 1 /* nuber of survivors */
|
||||
p = -1 /* start here */
|
||||
killed = '' /* output of killings */
|
||||
Loop until n = s /* until one alive prisoner */
|
||||
found = 0 /* start looking */
|
||||
Loop Until found = w /* until we have the third */
|
||||
p = p + 1 /* next position */
|
||||
If p = nn Then p = 0 /* wrap around */
|
||||
If dead[p] = 0 Then /* a prisoner who is alive */
|
||||
found = found + 1 /* increment found count */
|
||||
End
|
||||
dead[p] = 1
|
||||
n = n - 1 /* shoot the one on this pos. */
|
||||
killed = killed p /* add to output */
|
||||
End /* End of main loop */
|
||||
Say 'killed:'killed.subword(1, 20) /* output killing sequence */
|
||||
Say ' 'killed.subword(21) /* output killing sequence */
|
||||
Say 'Survivor(s):' /* show */
|
||||
Loop i = 0 To 40 /* look for the surviving p's */
|
||||
If dead[i] = 0 Then Say i /* found one */
|
||||
End
|
||||
20
Task/Josephus-problem/Nim/josephus-problem-1.nim
Normal file
20
Task/Josephus-problem/Nim/josephus-problem-1.nim
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
import sequtils, strutils, sugar
|
||||
|
||||
proc j(n, k: int): string =
|
||||
var
|
||||
p = toSeq(0 ..< n)
|
||||
i = 0
|
||||
s = newSeq[int]()
|
||||
|
||||
while p.len > 0:
|
||||
i = (i + k - 1) mod p.len
|
||||
s.add p[i]
|
||||
system.delete(p, i)
|
||||
|
||||
result = "Prisoner killing order: "
|
||||
result.add s.map((x: int) => $x).join(", ")
|
||||
result.add ".\nSurvivor: "
|
||||
result.add($s[s.high])
|
||||
|
||||
echo j(5,2)
|
||||
echo j(41,3)
|
||||
10
Task/Josephus-problem/Nim/josephus-problem-2.nim
Normal file
10
Task/Josephus-problem/Nim/josephus-problem-2.nim
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
func prisonerPos(n, k: Positive): int =
|
||||
## The result is computed backwards. We start from the winner at
|
||||
## position 0 on last round and compute its position on previous rounds.
|
||||
var pos = 0
|
||||
for i in 2..n:
|
||||
pos = (pos + k) mod i
|
||||
result = pos
|
||||
|
||||
echo "Survivor: ", prisonerPos(5, 2)
|
||||
echo "Survivor: ", prisonerPos(41, 3)
|
||||
50
Task/Josephus-problem/Objeck/josephus-problem.objeck
Normal file
50
Task/Josephus-problem/Objeck/josephus-problem.objeck
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
class Josephus {
|
||||
function : Execute(n : Int, k : Int) ~ Int {
|
||||
killIdx := 0;
|
||||
prisoners := Collection.IntVector->New();
|
||||
for(i := 0;i < n;i+=1;){
|
||||
prisoners->AddBack(i);
|
||||
};
|
||||
|
||||
"Prisoners executed in order:"->PrintLine();
|
||||
while(prisoners->Size() > 1){
|
||||
killIdx := (killIdx + k - 1) % prisoners->Size();
|
||||
executed := prisoners->Get(killIdx);
|
||||
"{$executed} "->Print();
|
||||
prisoners->Remove(killIdx);
|
||||
};
|
||||
'\n'->Print();
|
||||
return prisoners->Get(0);
|
||||
}
|
||||
|
||||
function : ExecuteAllButM(n : Int, k : Int, m : Int) ~ Collection.IntVector {
|
||||
killIdx := 0;
|
||||
prisoners := Collection.IntVector->New();
|
||||
for(i := 0;i < n;i+=1;){
|
||||
prisoners->AddBack(i);
|
||||
};
|
||||
"Prisoners executed in order:"->PrintLine();
|
||||
while(prisoners->Size() > m){
|
||||
killIdx := (killIdx + k - 1) % prisoners->Size();
|
||||
executed := prisoners->Get(killIdx);
|
||||
"{$executed} "->Print();
|
||||
prisoners->Remove(killIdx);
|
||||
};
|
||||
'\n'->Print();
|
||||
return prisoners;
|
||||
}
|
||||
|
||||
function : Main(args : String[]) ~ Nil {
|
||||
result := Execute(41, 3);
|
||||
"Survivor: {$result}"->PrintLine();
|
||||
|
||||
results := ExecuteAllButM(41, 3, 3);
|
||||
"Survivors: "->Print();
|
||||
each(i : results) {
|
||||
results->Get(i)->Print();
|
||||
if(i + 1 < results->Size()) {
|
||||
' '->Print();
|
||||
};
|
||||
};
|
||||
}
|
||||
}
|
||||
12
Task/Josephus-problem/Oforth/josephus-problem.fth
Normal file
12
Task/Josephus-problem/Oforth/josephus-problem.fth
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
: josephus(n, k)
|
||||
| prisoners killed i |
|
||||
n seq asListBuffer ->prisoners
|
||||
ListBuffer newSize(n) ->killed
|
||||
|
||||
0 n 1- loop: i [
|
||||
k 1- + prisoners size mod dup 1+ prisoners removeAt
|
||||
killed add
|
||||
] drop
|
||||
|
||||
System.Out "Killed : " << killed << "\nSurvivor : " << prisoners << cr
|
||||
;
|
||||
23
Task/Josephus-problem/Oz/josephus-problem.oz
Normal file
23
Task/Josephus-problem/Oz/josephus-problem.oz
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
declare
|
||||
fun {Pipe Xs L H F}
|
||||
if L=<H then {Pipe {F Xs L} L+1 H F} else Xs end
|
||||
end
|
||||
fun {Josephus N K}
|
||||
fun {Victim Xs I}
|
||||
case Xs of kill(X S)|Xr then
|
||||
if S==1 then Last=I nil
|
||||
elseif X mod K==0 then
|
||||
Killed:=I-1|@Killed
|
||||
kill(X+1 S-1)|Xr
|
||||
else
|
||||
kill(X+1 S)|{Victim Xr I}
|
||||
end
|
||||
[] nil then nil end
|
||||
end
|
||||
Last Zs Killed={NewCell nil}
|
||||
in
|
||||
Zs={Pipe kill(1 N)|Zs 1 N
|
||||
fun {$ Is I} thread {Victim Is I} end end}
|
||||
result(survivor: Last-1 killed: {Reverse @Killed})
|
||||
end
|
||||
{Show {Josephus 41 3}}
|
||||
1
Task/Josephus-problem/PARI-GP/josephus-problem.parigp
Normal file
1
Task/Josephus-problem/PARI-GP/josephus-problem.parigp
Normal file
|
|
@ -0,0 +1 @@
|
|||
Josephus(n, k)=if(n<2, n>0, my(t=(Josephus(n-1, k)+k)%n); if(t, t, n))
|
||||
20
Task/Josephus-problem/PHP/josephus-problem.php
Normal file
20
Task/Josephus-problem/PHP/josephus-problem.php
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
<?php //Josephus.php
|
||||
function Jotapata($n=41,$k=3,$m=1){$m--;
|
||||
$prisoners=array_fill(0,$n,false);//make a circle of n prisoners, store false ie: dead=false
|
||||
$deadpool=1;//count to next execution
|
||||
$order=0;//death order and *dead* flag, ie. deadpool
|
||||
while((array_sum(array_count_values($prisoners))<$n)){//while sum of count of unique values dead times < n (they start as all false)
|
||||
foreach($prisoners as $thisPrisoner=>$dead){
|
||||
if(!$dead){//so yeah...if not dead...
|
||||
if($deadpool==$k){//if their time is up in the deadpool...
|
||||
$order++;
|
||||
//set the deadpool value or enumerate as survivor
|
||||
$prisoners[$thisPrisoner]=((($n-$m)>($order)?$order:(($n)==$order?'Call me *Titus Flavius* Josephus':'Joe\'s friend '.(($order)-($n-$m-1)))));
|
||||
$deadpool=1;//reset count to next execution
|
||||
}else{$duckpool++;}
|
||||
}
|
||||
}
|
||||
}
|
||||
return $prisoners;
|
||||
}
|
||||
echo '<pre>'.print_r(Jotapata(41,3,5),true).'<pre>';
|
||||
63
Task/Josephus-problem/PL-I/josephus-problem.pli
Normal file
63
Task/Josephus-problem/PL-I/josephus-problem.pli
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
*process or(!) source attributes xref;
|
||||
joseph: Proc Options(main);
|
||||
/* REXX **************************************************************
|
||||
* 15.11.2012 Walter Pachl - my own solution
|
||||
* 16.11.2012 Walter Pachl generalized n prisoners + w killing distance
|
||||
* and s=number of survivors
|
||||
* 03.05.2013 Walter Pachl Translated From REXX Version 1
|
||||
**********************************************************************/
|
||||
Dcl dead(0:100) Bit(1);
|
||||
Dcl (n,nn,w,s,p,found) Bin Fixed(15);
|
||||
Dcl pp Pic'99';
|
||||
Dcl killed Char(300) Var Init('killed: '); /* output of killings */
|
||||
Dcl survived Char(300) Var Init('Survivor(s): ');
|
||||
dead=''; /* nobody's dead yet */
|
||||
n=41; /* number of alive prisoners */
|
||||
nn=n; /* wrap around boundary */
|
||||
w=3; /* killing count */
|
||||
s=1; /* number of survivors */
|
||||
p=-1; /* start here */
|
||||
Do Until(n=s); /* until one alive prisoner */
|
||||
found=0; /* start looking */
|
||||
Do Until(found=w); /* until we have the third */
|
||||
p=p+1; /* next position */
|
||||
If p=nn Then p=0; /* wrap around */
|
||||
If ^dead(p) Then /* a prisoner who is alive */
|
||||
found=found+1; /* increment found count */
|
||||
End;
|
||||
dead(p)='1'b; /* shoot the one on this pos. */
|
||||
n=n-1;
|
||||
pp=p;
|
||||
killed=killed!!' '!!pp; /* add to output */
|
||||
End; /* End of main loop */
|
||||
Call o(killed);
|
||||
Do i=0 To nn-1; /* look for the surviving p's */
|
||||
If ^dead(i) Then Do; /* found one */
|
||||
pp=i;
|
||||
survived=survived!!' '!!pp;
|
||||
End;
|
||||
End;
|
||||
Call o(survived);
|
||||
|
||||
o: Proc(s);
|
||||
/*********************************************************************
|
||||
* Formatted Output of given string:
|
||||
* xxxxxxxxxx xxx xx xx xxx ---
|
||||
* xx xxx xxx
|
||||
* xxxxx xxx
|
||||
*********************************************************************/
|
||||
Dcl s Char(*) Var;
|
||||
Dcl p Bin Fixed(15);
|
||||
Dcl ll Bin Fixed(15) Init(72);
|
||||
Do While(length(s)>ll);
|
||||
Do p=ll+1 To 10 By -1;
|
||||
If substr(s,p,1)=' ' Then
|
||||
Leave;
|
||||
End;
|
||||
Put Edit(left(s,p))(Skip,a);
|
||||
s=repeat(' ',8)!!substr(s,p+1);
|
||||
End;
|
||||
Put Edit(s)(Skip,a);
|
||||
End;
|
||||
|
||||
End;
|
||||
8
Task/Josephus-problem/Perl/josephus-problem.pl
Normal file
8
Task/Josephus-problem/Perl/josephus-problem.pl
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
my @prisoner = 0 .. 40;
|
||||
my $k = 3;
|
||||
until (@prisoner == 1) {
|
||||
push @prisoner, shift @prisoner for 1 .. $k-1;
|
||||
shift @prisoner;
|
||||
}
|
||||
|
||||
print "Prisoner @prisoner survived.\n"
|
||||
16
Task/Josephus-problem/Phix/josephus-problem-1.phix
Normal file
16
Task/Josephus-problem/Phix/josephus-problem-1.phix
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
-->
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">skipping</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">prisoners</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">step</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">survivors</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">integer</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">prisoners</span><span style="color: #0000FF;">),</span> <span style="color: #000000;">nn</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">p</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
|
||||
<span style="color: #008080;">while</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">></span><span style="color: #000000;">survivors</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #004080;">integer</span> <span style="color: #000000;">found</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
|
||||
<span style="color: #008080;">while</span> <span style="color: #000000;">found</span><span style="color: #0000FF;"><</span><span style="color: #000000;">step</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #000000;">p</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">=</span><span style="color: #000000;">nn</span><span style="color: #0000FF;">?</span><span style="color: #000000;">1</span><span style="color: #0000FF;">:</span><span style="color: #000000;">p</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">found</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">prisoners</span><span style="color: #0000FF;">[</span><span style="color: #000000;">p</span><span style="color: #0000FF;">]!=-</span><span style="color: #000000;">1</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
|
||||
<span style="color: #000000;">prisoners</span><span style="color: #0000FF;">[</span><span style="color: #000000;">p</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span>
|
||||
<span style="color: #000000;">n</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">1</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #7060A8;">remove_all</span><span style="color: #0000FF;">(-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">prisoners</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
<span style="color: #000080;font-style:italic;">--?skipping({"Joe","Jack","William","John","James"},2,1) --> {"William"}</span>
|
||||
<!--
|
||||
17
Task/Josephus-problem/Phix/josephus-problem-2.phix
Normal file
17
Task/Josephus-problem/Phix/josephus-problem-2.phix
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
-->
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">linked_list</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">prisoners</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">step</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">survivors</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">integer</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">prisoners</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">links</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)&</span><span style="color: #000000;">1</span>
|
||||
<span style="color: #004080;">integer</span> <span style="color: #000000;">p</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">prvp</span>
|
||||
<span style="color: #008080;">while</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">></span><span style="color: #000000;">survivors</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">step</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #000000;">prvp</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">p</span>
|
||||
<span style="color: #000000;">p</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">links</span><span style="color: #0000FF;">[</span><span style="color: #000000;">p</span><span style="color: #0000FF;">]</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<span style="color: #000000;">prisoners</span><span style="color: #0000FF;">[</span><span style="color: #000000;">p</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span>
|
||||
<span style="color: #000000;">links</span><span style="color: #0000FF;">[</span><span style="color: #000000;">prvp</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">links</span><span style="color: #0000FF;">[</span><span style="color: #000000;">p</span><span style="color: #0000FF;">]</span>
|
||||
<span style="color: #000000;">n</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">1</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #7060A8;">remove_all</span><span style="color: #0000FF;">(-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">prisoners</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
<!--
|
||||
11
Task/Josephus-problem/Phix/josephus-problem-3.phix
Normal file
11
Task/Josephus-problem/Phix/josephus-problem-3.phix
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
-->
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">sliding_queue</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">prisoners</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">step</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">survivors</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">integer</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">prisoners</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">while</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">></span><span style="color: #000000;">survivors</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #004080;">integer</span> <span style="color: #000000;">k</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">step</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)+</span><span style="color: #000000;">1</span> <span style="color: #000080;font-style:italic;">-- (mostly k==step)</span>
|
||||
<span style="color: #000000;">prisoners</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">prisoners</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..$]&</span><span style="color: #000000;">prisoners</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">k</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #000080;font-style:italic;">-- rotate, dropping one.</span>
|
||||
<span style="color: #000000;">n</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">1</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #000000;">prisoners</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
<!--
|
||||
25
Task/Josephus-problem/Phix/josephus-problem-4.phix
Normal file
25
Task/Josephus-problem/Phix/josephus-problem-4.phix
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
-->
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">contractacycle</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">k</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">living</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">integer</span> <span style="color: #000000;">startPosition</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">k</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">lasti</span>
|
||||
<span style="color: #008080;">while</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">!=</span><span style="color: #000000;">s</span> <span style="color: #008080;">do</span> <span style="color: #000080;font-style:italic;">-- Keep going round the circle until only s prisoners remain.</span>
|
||||
<span style="color: #004080;">integer</span> <span style="color: #000000;">circleSize</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">n</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">n</span> <span style="color: #0000FF;"><</span> <span style="color: #000000;">k</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #000000;">i</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">startPosition</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">circleSize</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">1</span>
|
||||
<span style="color: #000000;">living</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">living</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">i</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]&</span><span style="color: #000000;">living</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..$]</span>
|
||||
<span style="color: #000000;">n</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">1</span>
|
||||
<span style="color: #000000;">lasti</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">i</span>
|
||||
<span style="color: #008080;">else</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">startPosition</span> <span style="color: #008080;">to</span> <span style="color: #000000;">circleSize</span> <span style="color: #008080;">by</span> <span style="color: #000000;">k</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #000000;">living</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span>
|
||||
<span style="color: #000000;">n</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">1</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span> <span style="color: #000080;font-style:italic;">-- Not Groovy, see note</span>
|
||||
<span style="color: #000000;">lasti</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">i</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<span style="color: #000000;">living</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">remove_all</span><span style="color: #0000FF;">(-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">living</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #000000;">startPosition</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">lasti</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">k</span> <span style="color: #0000FF;">-</span> <span style="color: #000000;">circleSize</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #000000;">living</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
<!--
|
||||
13
Task/Josephus-problem/Phix/josephus-problem-5.phix
Normal file
13
Task/Josephus-problem/Phix/josephus-problem-5.phix
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
-->
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">contractalot</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">k</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">list</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">integer</span> <span style="color: #000000;">i</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
|
||||
<span style="color: #008080;">while</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">></span><span style="color: #000000;">s</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #000000;">i</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">k</span> <span style="color: #0000FF;">-</span> <span style="color: #000000;">1</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">i</span> <span style="color: #0000FF;">></span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span> <span style="color: #000000;">i</span> <span style="color: #0000FF;">:=</span> <span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)+</span><span style="color: #000000;">1</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #000000;">list</span> <span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">..</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
|
||||
<span style="color: #000000;">n</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">1</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #000000;">list</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
<!--
|
||||
5
Task/Josephus-problem/Phix/josephus-problem-6.phix
Normal file
5
Task/Josephus-problem/Phix/josephus-problem-6.phix
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
-->
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">recursive</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">k</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span><span style="color: #0000FF;">?</span><span style="color: #000000;">1</span><span style="color: #0000FF;">:</span><span style="color: #000000;">1</span><span style="color: #0000FF;">+</span><span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">k</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">+(</span><span style="color: #000000;">recursive</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">k</span><span style="color: #0000FF;">)),</span><span style="color: #000000;">n</span><span style="color: #0000FF;">))</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
<!--
|
||||
9
Task/Josephus-problem/Phix/josephus-problem-7.phix
Normal file
9
Task/Josephus-problem/Phix/josephus-problem-7.phix
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
-->
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">iterative</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">k</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">m</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000080;font-style:italic;">-- Return m-th on the reversed kill list; m=0 is final survivor.</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">a</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">m</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">n</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #000000;">m</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">m</span><span style="color: #0000FF;">+</span><span style="color: #000000;">k</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #000000;">m</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">1</span> <span style="color: #000080;font-style:italic;">-- (make result 1-based)</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
<!--
|
||||
13
Task/Josephus-problem/Phix/josephus-problem-8.phix
Normal file
13
Task/Josephus-problem/Phix/josephus-problem-8.phix
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
-->
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">iterative2</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">k</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">integer</span> <span style="color: #000000;">a</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">k</span><span style="color: #0000FF;">*(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">-</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span>
|
||||
<span style="color: #000000;">olda</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">a</span>
|
||||
<span style="color: #004080;">atom</span> <span style="color: #000000;">q</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">k</span><span style="color: #0000FF;">/(</span><span style="color: #000000;">k</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">),</span>
|
||||
<span style="color: #000000;">nk</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">*</span><span style="color: #000000;">k</span>
|
||||
<span style="color: #008080;">while</span> <span style="color: #000000;">a</span> <span style="color: #0000FF;"><=</span> <span style="color: #000000;">nk</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #000000;">olda</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">a</span>
|
||||
<span style="color: #000000;">a</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">ceil</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">*</span><span style="color: #000000;">q</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #000000;">nk</span> <span style="color: #0000FF;">-</span> <span style="color: #000000;">olda</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">1</span> <span style="color: #000080;font-style:italic;">-- (make result 1-based)</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
<!--
|
||||
69
Task/Josephus-problem/Phix/josephus-problem-9.phix
Normal file
69
Task/Josephus-problem/Phix/josephus-problem-9.phix
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
(phixonline)-->
|
||||
<span style="color: #000080;font-style:italic;">--demo/rosetta/Josephus.exw</span>
|
||||
<span style="color: #008080;">constant</span> <span style="color: #000000;">show_all</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">true</span><span style="color: #0000FF;">,</span>
|
||||
<span style="color: #000000;">show_slow</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">false</span><span style="color: #0000FF;">,</span>
|
||||
<span style="color: #000000;">show_skipping</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">false</span><span style="color: #0000FF;">,</span>
|
||||
<span style="color: #000000;">show_linkedlist</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">false</span><span style="color: #0000FF;">,</span>
|
||||
<span style="color: #000000;">show_sliding_queue</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">false</span><span style="color: #0000FF;">,</span>
|
||||
<span style="color: #000000;">show_contractacycle</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">false</span><span style="color: #0000FF;">,</span>
|
||||
<span style="color: #000000;">show_contractalot</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">false</span><span style="color: #0000FF;">,</span>
|
||||
<span style="color: #000000;">show_recursive</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">false</span><span style="color: #0000FF;">,</span>
|
||||
<span style="color: #000000;">show_iterative</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">false</span><span style="color: #0000FF;">,</span>
|
||||
<span style="color: #000000;">show_iterative2</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">true</span>
|
||||
|
||||
<span style="color: #008080;">constant</span> <span style="color: #000000;">TAGSET</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">#01</span><span style="color: #0000FF;">,</span>
|
||||
<span style="color: #000000;">ITER</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">#02</span><span style="color: #0000FF;">,</span>
|
||||
<span style="color: #000000;">ITER2</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">#04</span><span style="color: #0000FF;">,</span>
|
||||
<span style="color: #000000;">SLOW</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">#08</span><span style="color: #0000FF;">,</span>
|
||||
<span style="color: #000000;">ONES</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">#10</span>
|
||||
|
||||
<span style="color: #008080;">constant</span> <span style="color: #000000;">tests</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{{</span><span style="color: #000000;">41</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #004600;">false</span><span style="color: #0000FF;">},</span>
|
||||
<span style="color: #0000FF;">{</span><span style="color: #000000;">41</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #004600;">false</span><span style="color: #0000FF;">},</span>
|
||||
<span style="color: #0000FF;">{</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #004600;">false</span><span style="color: #0000FF;">},</span>
|
||||
<span style="color: #0000FF;">{</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #004600;">false</span><span style="color: #0000FF;">},</span>
|
||||
<span style="color: #0000FF;">{</span><span style="color: #000000;">50</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #004600;">false</span><span style="color: #0000FF;">},</span>
|
||||
<span style="color: #0000FF;">{</span><span style="color: #000000;">60</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #004600;">false</span><span style="color: #0000FF;">},</span>
|
||||
<span style="color: #0000FF;">{</span><span style="color: #000000;">23482</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3343</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #004600;">true</span><span style="color: #0000FF;">},</span>
|
||||
<span style="color: #0000FF;">{</span><span style="color: #000000;">23482</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3343</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #004600;">true</span><span style="color: #0000FF;">},</span>
|
||||
<span style="color: #0000FF;">{</span><span style="color: #000000;">41</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">6</span><span style="color: #0000FF;">,</span><span style="color: #004600;">false</span><span style="color: #0000FF;">}}</span>
|
||||
|
||||
<span style="color: #008080;">procedure</span> <span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">name</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">flags</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">atom</span> <span style="color: #000000;">t0</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">()</span>
|
||||
<span style="color: #004080;">integer</span> <span style="color: #000000;">rid</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">routine_id</span><span style="color: #0000FF;">(</span><span style="color: #000000;">name</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tests</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #004080;">integer</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">prisoners</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">step</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">survivors</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">slow</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">tests</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #0000FF;">(</span><span style="color: #008080;">not</span> <span style="color: #7060A8;">and_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">flags</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ONES</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">or</span> <span style="color: #000000;">survivors</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">and</span> <span style="color: #0000FF;">(</span><span style="color: #008080;">not</span> <span style="color: #000000;">slow</span> <span style="color: #008080;">or</span> <span style="color: #000000;">show_slow</span> <span style="color: #008080;">or</span> <span style="color: #008080;">not</span> <span style="color: #7060A8;">and_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">flags</span><span style="color: #0000FF;">,</span><span style="color: #000000;">SLOW</span><span style="color: #0000FF;">))</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #7060A8;">and_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">flags</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ONES</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #000080;font-style:italic;">-- (recursive does not take a 3rd param)</span>
|
||||
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">rid</span><span style="color: #0000FF;">(</span><span style="color: #000000;">prisoners</span><span style="color: #0000FF;">,</span><span style="color: #000000;">step</span><span style="color: #0000FF;">)}</span>
|
||||
<span style="color: #008080;">elsif</span> <span style="color: #7060A8;">and_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">flags</span><span style="color: #0000FF;">,</span><span style="color: #000000;">TAGSET</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">rid</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">prisoners</span><span style="color: #0000FF;">),</span><span style="color: #000000;">step</span><span style="color: #0000FF;">,</span><span style="color: #000000;">survivors</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">elsif</span> <span style="color: #7060A8;">and_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">flags</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ITER</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">to</span> <span style="color: #000000;">survivors</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #000000;">res</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">rid</span><span style="color: #0000FF;">(</span><span style="color: #000000;">prisoners</span><span style="color: #0000FF;">,</span><span style="color: #000000;">step</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<span style="color: #008080;">elsif</span> <span style="color: #7060A8;">and_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">flags</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ITER2</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">=</span><span style="color: #000000;">prisoners</span><span style="color: #0000FF;">-</span><span style="color: #000000;">survivors</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">prisoners</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #000000;">res</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">rid</span><span style="color: #0000FF;">(</span><span style="color: #000000;">prisoners</span><span style="color: #0000FF;">,</span><span style="color: #000000;">step</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<span style="color: #008080;">else</span>
|
||||
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">rid</span><span style="color: #0000FF;">(</span><span style="color: #000000;">prisoners</span><span style="color: #0000FF;">,</span><span style="color: #000000;">step</span><span style="color: #0000FF;">,</span><span style="color: #000000;">survivors</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s(%d,%d,%d) = %v\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">name</span><span style="color: #0000FF;">,</span><span style="color: #000000;">prisoners</span><span style="color: #0000FF;">,</span><span style="color: #000000;">step</span><span style="color: #0000FF;">,</span><span style="color: #000000;">survivors</span><span style="color: #0000FF;">,</span><span style="color: #000000;">res</span><span style="color: #0000FF;">})</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">time</span><span style="color: #0000FF;">()-</span><span style="color: #000000;">t0</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">show_all</span> <span style="color: #008080;">or</span> <span style="color: #000000;">show_skipping</span> <span style="color: #008080;">then</span> <span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"skipping"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">TAGSET</span><span style="color: #0000FF;">+</span><span style="color: #000000;">SLOW</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">show_all</span> <span style="color: #008080;">or</span> <span style="color: #000000;">show_linkedlist</span> <span style="color: #008080;">then</span> <span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"linked_list"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">TAGSET</span><span style="color: #0000FF;">+</span><span style="color: #000000;">SLOW</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">show_all</span> <span style="color: #008080;">or</span> <span style="color: #000000;">show_sliding_queue</span> <span style="color: #008080;">then</span> <span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"sliding_queue"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">TAGSET</span><span style="color: #0000FF;">+</span><span style="color: #000000;">SLOW</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">show_all</span> <span style="color: #008080;">or</span> <span style="color: #000000;">show_contractacycle</span> <span style="color: #008080;">then</span> <span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"contractacycle"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">SLOW</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">show_all</span> <span style="color: #008080;">or</span> <span style="color: #000000;">show_contractalot</span> <span style="color: #008080;">then</span> <span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"contractalot"</span><span style="color: #0000FF;">,</span><span style="color: #004600;">NULL</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">show_all</span> <span style="color: #008080;">or</span> <span style="color: #000000;">show_recursive</span> <span style="color: #008080;">then</span> <span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"recursive"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ONES</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">show_all</span> <span style="color: #008080;">or</span> <span style="color: #000000;">show_iterative</span> <span style="color: #008080;">then</span> <span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"iterative"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ITER</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">show_all</span> <span style="color: #008080;">or</span> <span style="color: #000000;">show_iterative2</span> <span style="color: #008080;">then</span> <span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"iterative2"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ITER2</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<!--
|
||||
23
Task/Josephus-problem/PicoLisp/josephus-problem.l
Normal file
23
Task/Josephus-problem/PicoLisp/josephus-problem.l
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
#general solution
|
||||
(de jo (N K)
|
||||
(if (=1 N)
|
||||
1
|
||||
(inc
|
||||
(%
|
||||
(+ (dec K) (jo (dec N) K))
|
||||
N ) ) ) )
|
||||
|
||||
#special case when K is 2; much faster than general version.
|
||||
(de jo2(N)
|
||||
(let P 1
|
||||
(while (<= P N)
|
||||
(setq P (* 2 P))
|
||||
(+ (- (* 2 N) P) 1) ) ) )
|
||||
|
||||
# find the survivor using an optimal solution
|
||||
(de survivor (N K)
|
||||
(if (=0 (% N 2))
|
||||
(jo2 N)
|
||||
(jo N K) ) )
|
||||
(print (survivor 5 2))
|
||||
(print (survivor 41 3))
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue