Who will survive?

Share a quote, a general thought, jokes or one liners here.
Post Reply
User avatar
Anuj Dhawan
Founder
Posts: 2802
Joined: Sun Apr 21, 2013 7:40 pm
Location: Mumbai, India
Contact:
India

Who will survive?

Post 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?
Thanks,
Anuj

Disclaimer: My comments on this website are my own and do not represent the opinions or suggestions of any other person or business entity, in any way.
William Collins
Global Moderator
Global Moderator
Posts: 490
Joined: Sun Aug 25, 2013 7:24 pm

Re: Who will survive?

Post 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.
User avatar
Anuj Dhawan
Founder
Posts: 2802
Joined: Sun Apr 21, 2013 7:40 pm
Location: Mumbai, India
Contact:
India

Re: Who will survive?

Post 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? :)
Thanks,
Anuj

Disclaimer: My comments on this website are my own and do not represent the opinions or suggestions of any other person or business entity, in any way.
enrico-sorichetti
Global Moderator
Global Moderator
Posts: 826
Joined: Wed Sep 11, 2013 3:57 pm

Re: Who will survive?

Post 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:
cheers
enrico
When I tell somebody to RTFM or STFW I usually have the page open in another tab/window of my browser,
so that I am sure that the information requested can be reached with a very small effort 8-)
User avatar
Anuj Dhawan
Founder
Posts: 2802
Joined: Sun Apr 21, 2013 7:40 pm
Location: Mumbai, India
Contact:
India

Re: Who will survive?

Post by Anuj Dhawan »

Well done Enrico! :)
Thanks,
Anuj

Disclaimer: My comments on this website are my own and do not represent the opinions or suggestions of any other person or business entity, in any way.
zprogrammer
Global Moderator
Global Moderator
Posts: 588
Joined: Wed Nov 20, 2013 11:53 am
Location: Mars

Re: Who will survive?

Post by zprogrammer »

I solved this in an excel ;)
You do not have the required permissions to view the files attached to this post.
zprogrammer
enrico-sorichetti
Global Moderator
Global Moderator
Posts: 826
Joined: Wed Sep 11, 2013 3:57 pm

Re: Who will survive?

Post 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

cheers
enrico
When I tell somebody to RTFM or STFW I usually have the page open in another tab/window of my browser,
so that I am sure that the information requested can be reached with a very small effort 8-)
User avatar
Anuj Dhawan
Founder
Posts: 2802
Joined: Sun Apr 21, 2013 7:40 pm
Location: Mumbai, India
Contact:
India

Re: Who will survive?

Post by Anuj Dhawan »

I regret to say that Your solution is less than perfect
Blame it on Micr*soft! ;)
Thanks,
Anuj

Disclaimer: My comments on this website are my own and do not represent the opinions or suggestions of any other person or business entity, in any way.
zprogrammer
Global Moderator
Global Moderator
Posts: 588
Joined: Wed Nov 20, 2013 11:53 am
Location: Mars

Re: Who will survive?

Post by zprogrammer »

Sob sob need to build my own compiler or Intepreter to be the master of my game :D
zprogrammer
enrico-sorichetti
Global Moderator
Global Moderator
Posts: 826
Joined: Wed Sep 11, 2013 3:57 pm

Re: Who will survive?

Post 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
cheers
enrico
When I tell somebody to RTFM or STFW I usually have the page open in another tab/window of my browser,
so that I am sure that the information requested can be reached with a very small effort 8-)
zprogrammer
Global Moderator
Global Moderator
Posts: 588
Joined: Wed Nov 20, 2013 11:53 am
Location: Mars

Re: Who will survive?

Post by zprogrammer »

Great work Enrico!! hail hail
zprogrammer
enrico-sorichetti
Global Moderator
Global Moderator
Posts: 826
Joined: Wed Sep 11, 2013 3:57 pm

Re: Who will survive?

Post 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
cheers
enrico
When I tell somebody to RTFM or STFW I usually have the page open in another tab/window of my browser,
so that I am sure that the information requested can be reached with a very small effort 8-)
abhi88920
New Member
Posts: 1
Joined: Thu May 26, 2016 3:52 pm

Re: Who will survive?

Post 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]
Last edited by Anuj Dhawan on Thu May 26, 2016 4:45 pm, edited 1 time in total.
User avatar
Anuj Dhawan
Founder
Posts: 2802
Joined: Sun Apr 21, 2013 7:40 pm
Location: Mumbai, India
Contact:
India

Re: Who will survive?

Post by Anuj Dhawan »

Yup, answer is 73. See if you can write a COBOL code for that algorithm you talked about! ;)
Thanks,
Anuj

Disclaimer: My comments on this website are my own and do not represent the opinions or suggestions of any other person or business entity, in any way.
enrico-sorichetti
Global Moderator
Global Moderator
Posts: 826
Joined: Wed Sep 11, 2013 3:57 pm

Re: Who will survive?

Post 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-)
cheers
enrico
When I tell somebody to RTFM or STFW I usually have the page open in another tab/window of my browser,
so that I am sure that the information requested can be reached with a very small effort 8-)
Chandan Yadav
Website Team
Website Team
Posts: 70
Joined: Wed Jul 31, 2013 10:19 pm

Re: Who will survive?

Post by Chandan Yadav »

Well done Enrico :)
Post Reply

Create an account or sign in to join the discussion

You need to be a member in order to post a reply

Create an account

Not a member? register to join our community
Members can start their own topics & subscribe to topics
It’s free and only takes a minute

Register

Sign in

Return to “Thought of the Day, General Talk & Jokes.”