Page 1 of 1

Who will survive?

Posted: Mon Sep 08, 2014 1:50 pm
by Anuj Dhawan
100 people standing in a circle in an order 1 to 100. No.1 person has a sword. He kills next person (i.e. No. 2 )and gives sword to next to next (i.e no.3). All person does the same until only 1 survives. Which number survives at the last?

Re: Who will survive?

Posted: Mon Sep 08, 2014 2:31 pm
by William Collins
Number three would kill number one and then all 98 survivors would leg it.

Or, about half-way around, after a certain amount of frowning and lip-moving, number one would realise he'd made a bad decision in handing on the sword, and he'd leg it, Followed, in a stuttering "peel" by each of the remaining handers-on realised the same, as mental capacity allowed.

If no-one before him had so decided, for sure number 98 would work out that it would be a very bad thing to follow the rules.

To get the riddle to work, you'll need to replace the people by something unthinking, like sheep, or COBOL programmers who like to use INITIALIZE everywhere in the their COBOL programs.

Re: Who will survive?

Posted: Mon Sep 08, 2014 5:39 pm
by Anuj Dhawan
William Collins wrote:To get the riddle to work, you'll need to replace the people by something unthinking, like sheep, or COBOL programmers who like to use INITIALIZE everywhere in the their COBOL programs.
LOL - COBOL programmer might have written INITIALIZE with hands - umm.. - can use sword then - they are discarded! :lol:

Assuming sheeps, with the power to kill each other and with NO thinking capability - what should be the answer then? :)

Re: Who will survive?

Posted: Mon Sep 08, 2014 5:48 pm
by enrico-sorichetti
/-pedantic-on
the question asked is one of the many variants of the Josephus or executioner (*) problem/quiz
(*) most probably using object and the garbage similitude could be more PC

where the objective is ...
given
N object <ranked in a circle>
K objects to be kept
G Grouped by
FIRST/LAST keep the first/ the last of the group

display the remaining objects after the discard
/-pedantic-off


the solution anyway should be 73


here is the solver ( quick and dirty )

Code: Select all

#! /usr/bin/rexx

parse arg n s k w

_dbg = 0

If  n = '?' Then Do
    call __log 'Invoke the program with the following arguments:'
    call __log 'n  - number of objects (default 41)'
    call __log 's  - stepping count    (default  3)'
    call __log 'k  - number of objects to keep (default  1)'
    call __log 'FIRST/LAST first or last of the group'
    Exit
End

if  w = "" then w = "last"
           else w = lower(w)

if  k = "" then k = 1

if  s = "" then s = 3

if  n = "" then n = 41

if  w = "first" then ,
    next = 1
else ,
    next = s

if  _dbg = 1 then ,
    call __log n s k w next

do  i = 1 to n
    stak.i = 1
end

/* first off-loop discard */
stak.next = 0
disc = 1
kept = n - 1
if _dbg = 1 then ,
    call __log "discarding "right(next,3) right(stak.next,3) right(disc,3) right(kept,3)

do until kept = k
    skip = 0
    do  until skip = s
        next = next + 1
        if  next > n then ,
            next = 1
        skip = skip + stak.next
    end
    if  stak.next = 0 then ,
        signal logic_error

    stak.next = 0
    disc = disc + 1
    kept = kept - 1
    if _dbg = 1 then ,
        call __log "discarding "right(next,3) right(stak.next,3) right(disc,3) right(kept,3)
end

if  ( disc + kept ) \= n then ,
    signal logic_error

do  i = 1 to n
    if  stak.i = 1 then ,
        call __log "kept "right(i,3)
end

exit

__log:
    say arg(1)
    return

/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
logic_error:
call __log  "** - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -"
call __log  "** "
call __log  "** Logic error at line '"sigl"' "
call __log  "** "
call __log  "** - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -"
exit

/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
novalue:
call __log  "** - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -"
call __log  "** "
call __log  "** Novalue trapped, line '"sigl"' var '"condition("D")"' "
call __log  "** "
call __log  "** - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -"
exit


it starts numbering by 1, while most of the Josephus solver start numbering by 0

I will leave to the other avid readers how to write it as an edit macro :geek:

Re: Who will survive?

Posted: Mon Sep 08, 2014 7:35 pm
by Anuj Dhawan
Well done Enrico! :)

Re: Who will survive?

Posted: Mon Sep 08, 2014 9:15 pm
by zprogrammer
I solved this in an excel ;)

Re: Who will survive?

Posted: Mon Sep 08, 2014 9:39 pm
by enrico-sorichetti
Pandora,

I regret to say that Your solution is less than perfect

for every <iteration> you start from 1 , but ...

here is the sequence of discards for 100 2

Code: Select all

disc   2  4  6 .. .. .. 100
kept   1  3  5 .. .. .. 95 97 99
disc   3  7 11 15 19 23 27 31 35 39 43 47 51 55 59 63 67 71 75 79 83 87 91 95 99
kept   1  5  9 13 17 21 25 29 33 37 41 45 49 53 57 61 65 69 73 77 81 85 89 93 97
disc   5 13 21 29 37 45 53 61 69 77 85 93
kept   1  9 17 25 33 41 49 57 65 73 81 89 97
disc   1 17 33 49 65 81 97
kept   9 25 41 57 73 89
disc  25 57 89
kept   9 41 73
disc  41
kept   9 73


Re: Who will survive?

Posted: Mon Sep 08, 2014 10:14 pm
by Anuj Dhawan
I regret to say that Your solution is less than perfect
Blame it on Micr*soft! ;)

Re: Who will survive?

Posted: Mon Sep 08, 2014 11:35 pm
by zprogrammer
Sob sob need to build my own compiler or Intepreter to be the master of my game :D

Re: Who will survive?

Posted: Wed Sep 10, 2014 1:23 pm
by enrico-sorichetti
and here is a PURE algorithmic solution

Code: Select all

#! /usr/bin/rexx

parse arg n k

r = 0
i = 1
do while i <= n
    r = ( r + k ) // i
    i = i + 1
end

say r+1

exit

quick and dirty

edited to use a REXX construct good also for TSO REXX

Re: Who will survive?

Posted: Wed Sep 10, 2014 1:36 pm
by zprogrammer
Great work Enrico!! hail hail

Re: Who will survive?

Posted: Wed Sep 10, 2014 2:01 pm
by enrico-sorichetti
Thank You, but I just did the coding ,
internet was the source of the mathematics behind it

and if You run this

Code: Select all

#! /usr/bin/rexx

parse arg n k

r = 0
i = 1
do while i <= n
    r = ( r + k ) // i
    say right( i, 3) r
    i = i + 1
end

say r+1

exit



the number sequence is pretty similar ( even if in reverse order )
to the sequence of discards of the BRUTE FORCE method
I will have to find out why number/positions greeted than 72 do not come out
( quirks of math )

edited to use a REXX construct good also for TSO REXX

Re: Who will survive?

Posted: Thu May 26, 2016 4:02 pm
by abhi88920
[font=Helvetica, Arial, Sens-serif]Answer is 73.[/font]

[font=Helvetica, Arial, Sens-serif]The formula to find the no. that will survive in a list (of numbers) is as below:[/font]
[font=Helvetica, Arial, Sens-serif]If the total count of the list is in exponents of 2, then it’s the 1st no. will survive.[/font]
[font=Helvetica, Arial, Sens-serif]If the total count is not an exponent of 2, then subtract the total count from the next exponent of 2. If you call the result as ‘x’, then the number that survives is the xth number of the list counted backwards.[/font]

[font=Helvetica, Arial, Sens-serif]Credit:-Which number survives at the last[/font]

Re: Who will survive?

Posted: Thu May 26, 2016 4:59 pm
by Anuj Dhawan
Yup, answer is 73. See if you can write a COBOL code for that algorithm you talked about! ;)

Re: Who will survive?

Posted: Thu May 26, 2016 8:04 pm
by enrico-sorichetti
abhi88920 wrote:[font=Helvetica, Arial, Sens-serif]Answer is 73.[/font]

[font=Helvetica, Arial, Sens-serif]The formula to find the no. that will survive in a list (of numbers) is as below:[/font]
[font=Helvetica, Arial, Sens-serif]If the total count of the list is in exponents of 2, then it’s the 1st no. will survive.[/font]
[font=Helvetica, Arial, Sens-serif]If the total count is not an exponent of 2, then subtract the total count from the next exponent of 2. If you call the result as ‘x’, then the number that survives is the xth number of the list counted backwards.[/font]

[font=Helvetica, Arial, Sens-serif]Credit:-Which number survives at the last[/font]
just giving the answers and where it came from is not enough on a mainframe/programming forum
show the code and after that we can talk about it  8-)

Re: Who will survive?

Posted: Fri May 27, 2016 10:50 am
by Chandan Yadav
Well done Enrico :)