Latest LQ Deal: Linux Power User Bundle
 Home Forums HCL Reviews Tutorials Articles Register Search Today's Posts Mark Forums Read
 LinuxQuestions.org Facing problem while solving Josephus Problem in BASH
 Linux - Newbie This Linux forum is for members that are new to Linux. Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

 05-13-2015, 03:12 AM #1 unclesamcrazy Member   Registered: May 2013 Posts: 189 Rep: Facing problem while solving Josephus Problem in BASH I want to solve this question using BASH 100 people standing in a circle in an order 1 to 100. No. 1 has a sword. He kills the next person (i.e. No. 2) and gives the sword to the next (i.e. No. 3). All people do the same until only 1 survives. Which number survives at the last? If input is taken by keyboard, it should print the saved person number. Not only for hard coded 100 person. Suppose I enter 5 as user input, it should print "3 is safe". if I enter 11, it should print "7 is safe". Please help me to write this script. Thanks
 05-13-2015, 04:54 AM #2 chrism01 LQ Guru   Registered: Aug 2004 Location: Sydney Distribution: Centos 6.9, Centos 7.3 Posts: 17,395 Rep: We're not going to write this for you, but we WILL help you write it. Show us what you've written so far and what you think the problem is and we'll help you. Have you read any good tutorials; there are plenty on the web. You could look at this one http://rute.2038bug.com/index.html.gz
 05-14-2015, 09:35 AM #3 unclesamcrazy Member   Registered: May 2013 Posts: 189 Original Poster Rep: I am using array but I am not getting correct results. Code: ```#!/bin/bash arrayName=(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20) var=\$(echo "\${#arrayName[@]}") i=1 while : do sleep 2 unset arrayName[\$i] i=`expr \$i + 2` if [ \$i -gt \$var ] then var=\$(echo "\${#arrayName[@]}") i=1 fi done echo "\${arrayName[@]}"```
 05-14-2015, 07:28 PM #4 millgates Member Contributing Member   Registered: Feb 2009 Location: 192.168.x.x Distribution: Slackware Posts: 840 Rep: Hi, just a few minor suggestions: 1/ I would suggest that you use proper indentation to make the code more readable. 2/ When debugging shell scripts, it is a good idea to put set -xv at the begining of the script. That will allow you to follow each step the script does. 3/ When describing your problem, be more specific than "It doesn't work" or "I'm not getting correct results". What exactly does not work? What results are you getting and what results do you expect? 4/ Code: `arrayName=(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20)` You can use brace expansion here: Code: `arrayName=({1..20})` 5/ Code: `var=\$(echo "\${#arrayName[@]}")` That's a very useless use of echo. just do Code: `var="\${#arrayName[@]}"` 6/ Code: `while :` That's an infinite loop. Please make sure that the loop exits when its purpose is achieved. 7/ Code: `unset arrayName[\$i]` I would recommend putting "arrayName[\$i]" within double quotes to avoid pathname expansion. Also, keep in mind unsetting an array member will not reset numbering of the other elements. 8/ Code: `i=`expr \$i + 2`` better written as Code: `i=\$(expr \$i + 2)` or, even better Code: `((i+=2))` 9/ Code: `i=1` Don't forget array indices start at 0 in bash. 10/ I'll leave fixing the remaining flaws in the logic of the program as an exercise for the original poster. 1 members found this post helpful.
 05-15-2015, 01:53 AM #5 unclesamcrazy Member   Registered: May 2013 Posts: 189 Original Poster Rep: Thanks for guidance, my mistake I did not paste the code in my question. First I wrote this code Code: ```#!/bin/bash arrayName=({1..20}) var="\${#arrayName[@]}" echo \$var i=1 while [ \$var -gt \$i ] do unset arrayName[\$i] i=`expr \$i + 2` done echo "\${arrayName[@]}"``` but it removes the alternate array values only once, I need to keep removing alternate values until only one remains. I am facing problem in this part. I want to keep the loop alive but not for ever. Please help
05-15-2015, 02:35 AM   #6
millgates
Member

Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 840

Rep:
Quote:
 Originally Posted by unclesamcrazy until only one remains.
So, how do you say the same thing using a math expression?

Last edited by millgates; 05-15-2015 at 03:00 AM.

05-15-2015, 04:30 AM   #7
unclesamcrazy
Member

Registered: May 2013
Posts: 189

Original Poster
Rep:
I would remove them alternatively until I get the answer.
Quote:
 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 1 3 5 7 9 11 13 15 17 19 1 5 9 13 17 1 9 17 9 17 9
but not able to find, how to do it using loop.

05-15-2015, 06:02 AM   #8
rtmistler
Moderator

Registered: Mar 2011
Location: Sutton, MA. USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu
Posts: 5,595
Blog Entries: 12

Rep:
Quote:
 Originally Posted by millgates So, how do you say the same thing using a math expression?
I agree with millgates. I'd solve this problem via algorithm/math first and then determine how best to code it. Something like:
Code:
```// n[next] = n + 1 accounting for loop around
// n[next2] = n + 2 also accounting for loop around
while (n[next]) exists do
if (n[next2]) exists then
destroy (n[next])
n = (n[next2])
else
destroy (n[next])
// n stays the same
endif
endwhile```
Next thing the instructor may ask is to do this with something like 1347 entries and then maybe start heading for the concept of linked lists.

Last edited by rtmistler; 05-15-2015 at 06:04 AM.

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is Off HTML code is Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post [SOLVED] VICE problem solving kkoistinen Linux - Software 2 11-03-2013 08:48 AM badriinvenkat Linux Mint 20 08-13-2010 04:28 AM dohpaz General 8 05-25-2007 12:27 PM banner Linux - General 7 06-28-2005 06:44 PM true_atlantis Linux - General 5 10-30-2003 12:16 PM

LinuxQuestions.org

All times are GMT -5. The time now is 09:41 PM.

 Contact Us - Advertising Info - Rules - LQ Merchandise - Donations - Contributing Member - LQ Sitemap -