0 Replies - 277 Views - Last Post: 02 October 2015 - 06:08 PM Rate Topic: -----

#1 Bunchhieng   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 02-October 15

Trying to implement count_collatz_step in Bash script

Posted 02 October 2015 - 06:08 PM

Hey guys, I just start out learning bash script. I'm trying to implement a script to take 2 user inputs then print out Collatz Conjecture step of each number between start and end.
I'm looking for any improvement and suggestion on this code.


# 
# Count steps of collatz conjecture takes, until the number reach 1.
#

count_collatz_step() {
   step=0
    if [ $1 % 2 == 0 ]
    then 
        $1=$(($1/2))
        step=$((step + 1))
    else
        $1=$((3*$n+1))
        step=$((step + 1))
    fi
    echo $step
}

echo "Input start point: "
read -n start
echo "Input end point: "
read -n end

count=0

while [ $start -lt $end ]
do
    echo $start + ":" + count_collatz_step $start
    if [ $count % 7 -eq 0 ]
    then 
        echo "\n"
    fi
    start=$((start+1))
    count=$((count+1))
done




Is This A Good Question/Topic? 0
  • +

Page 1 of 1