Jump to content

Proofreader(s) needed for bash script [solved]


Recommended Posts

Ok, so I have this obsession with the Fibonacci sequence, so I was doing some testing. I wrote a bash script that is supposed to extend the Fibonacci rabbit sequence past 2000 digits. The method I am trying to use is this one:

http://www.mcs.surrey.ac.uk/Personal/R.Kno...rab.html#infseq

 

Here is my script:

#!/bin/bash

#newfibo.sh

fibo=10;
for ((a=1; a <= 1000000; a++))  # Double parentheses, and "LIMIT" with no "$".
 do
#   echo ${fibo:a:1}

  if [[ ${fibo:a:1} = "0" ]] 
  then
  fibo="$fibo""1"
  fi
  if [[ ${fibo:a:1} = "1" ]] 
  then
  fibo="$fibo""10"
  fi
echo "$fibo" > newertest.fibo
sleep .005s
done

 

In theory this would extend the rabbit sequence well past 1,000,000 digits, but it is very CPU-intensive and I got tired of waiting and stopped it at 776100 digits.

 

Then I have a script that breaks the sequence down into groups of four and translates each group of four into a hexadecimal number and I group 6 hexadecimal numbers together to get a color:

#!/bin/bash

#string.sh

fibo=$(cat newertest.fibo);
let limit=$1-4
let count=0
strange=0

i=0
rm -f filec
while [[ "$strange" -lt "$limit" ]] # Double parentheses, and "LIMIT" with no "$".
do
test=${fibo:strange:4}
distance=$(bc -l <<TEST
obase=16; ibase=2; $test 
TEST
)
if [[ $i -ne "6" ]]
   then
   newdistance="$newdistance""$distance"
   i=$i+1
else
   echo "$newdistance" >> filec
   i=0
   newdistance=""
   let count=count+1
fi
 let "strange += 4"
done
echo $count"

 

I pass the limit to the script as the number of digits like this:

 

./string.sh 776100

 

One thing I noticed is that the output of string.sh is only 27717 lines, when theoretically, it should be 32337:

776100/24=32337

 

So, somewhere, I lost 110,892 digits. Is there something wrong with my code? Is there a limitation to the variable size I don't know about?

Edited by Steve Scrimpshire
Link to comment
Share on other sites

It looks like your variable i is looping from 0 to 6 which means 7 iterations round that loop. So the number of lines is 776100 / 4 / 7 = 27717. Six of those distances are used, and the seventh is ignored. Change your loop from 0 to 5 or 1 to 6, and change your newdistance="" to newdistance=$distance and it should be fine.

 

However I don't understand this bit:

	distance=$(bc -l <<TEST
obase=16; ibase=2; $test
TEST
)

What does that do? What is 'distance' ? You're calculating the number from the four 1s and 0s here, right?

Link to comment
Share on other sites

Thanks for the great info. That part of the script takes four digits and translates them to hex. Then I am grouping those into groups of 6 hex numbers that represent colo(u)rs.

I've been conversing with iphitus and Tuxiscool on #musb in IRC. iphitus translated my script into Python, which runs much much faster and efficiently. Tux also translated my script into Java, which is a little slower than iphitus' Python script but also great. I need to work with Tux a little more because there's some miscommunication on how the binary and hex files should be written.

Link to comment
Share on other sites

Yeah, I understood what you said you were trying to do, I just didn't know bc and that syntax looked very weird at first glance :) However I dug a little around and found some info about bc, including this example of calling it from bash:

variable=$(bc << LIMIT_STRING
options
statements
operations
LIMIT_STRING
)

- which made a (little) bit more sense. Still looks odd.

I'm not sure but I'd imagine this call to bc for each of the 776100 times round the loop might be causing the overhead. As the result is only 0 - F, you could implement this much more efficiently with a simple lookup table.

 

PS/ a few comments and meaningful variable names make proofreading much easier! :)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...