⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 primes.sh

📁 BASH Shell 编程 经典教程 《高级SHELL脚本编程》中文版
💻 SH
字号:
#!/bin/bash# primes.sh: Generate prime numbers, without using arrays.# Script contributed by Stephane Chazelas.#  This does *not* use the classic "Sieve of Eratosthenes" algorithm,#+ but instead uses the more intuitive method of testing each candidate number#+ for factors (divisors), using the "%" modulo operator.LIMIT=1000                    # Primes 2 - 1000Primes(){ (( n = $1 + 1 ))             # Bump to next integer. shift                        # Next parameter in list.#  echo "_n=$n i=$i_"  if (( n == LIMIT )) then echo $* return fi for i; do                    # "i" gets set to "@", previous values of $n.#   echo "-n=$n i=$i-"   (( i * i > n )) && break   # Optimization.   (( n % i )) && continue    # Sift out non-primes using modulo operator.   Primes $n $@               # Recursion inside loop.   return   done   Primes $n $@ $n            # Recursion outside loop.                              # Successively accumulate positional parameters.                              # "$@" is the accumulating list of primes.}Primes 1exit 0#  Uncomment lines 16 and 24 to help figure out what is going on.#  Compare the speed of this algorithm for generating primes#+ with the Sieve of Eratosthenes (ex68.sh).#  Exercise: Rewrite this script without recursion, for faster execution.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -