📄 collatz.sh
字号:
#!/bin/bash# collatz.sh# The notorious "hailstone" or Collatz series.# -------------------------------------------# 1) Get the integer "seed" from the command line.# 2) NUMBER <--- seed# 3) Print NUMBER.# 4) If NUMBER is even, divide by 2, or# 5)+ if odd, multiply by 3 and add 1.# 6) NUMBER <--- result # 7) Loop back to step 3 (for specified number of iterations).## The theory is that every sequence,#+ no matter how large the initial value,#+ eventually settles down to repeating "4,2,1..." cycles,#+ even after fluctuating through a wide range of values.## This is an instance of an "iterate,"#+ an operation that feeds its output back into the input.# Sometimes the result is a "chaotic" series.MAX_ITERATIONS=200# For large seed numbers (>32000), increase MAX_ITERATIONS.h=${1:-$$} # Seed # Use $PID as seed, #+ if not specified as command-line arg.echoecho "C($h) --- $MAX_ITERATIONS Iterations"echofor ((i=1; i<=MAX_ITERATIONS; i++))doecho -n "$h "# ^^^^^# tab let "remainder = h % 2" if [ "$remainder" -eq 0 ] # Even? then let "h /= 2" # Divide by 2. else let "h = h*3 + 1" # Multiply by 3 and add 1. fiCOLUMNS=10 # Output 10 values per line.let "line_break = i % $COLUMNS"if [ "$line_break" -eq 0 ]then echofi doneecho# For more information on this mathematical function,#+ see _Computers, Pattern, Chaos, and Beauty_, by Pickover, p. 185 ff.,#+ as listed in the bibliography.exit 0
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -