SHARE
TWEET

Untitled

a guest Aug 12th, 2017 49 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2.  
  3. #
  4. # generate list of primes up to given number
  5. #
  6.  
  7. set -e
  8. set -u
  9.  
  10. if [[ ${#} != 1 ]] ; then
  11.   echo "please provide a number"
  12.   exit 1
  13. fi
  14.  
  15. start=2
  16. primes="${start}"
  17.  
  18. for (( i=$((${start}+1)) ; i <= ${1} ; i++ )) ; do
  19.   test $((${i}%2)) -eq 0 && continue
  20.   broke=0
  21.   for p in ${primes} ; do
  22.   test ${p} -le $((${i}/2+1)) && {
  23.     test $((${i}%${p})) -eq 0 && {
  24.       broke=1
  25.       break
  26.     }
  27.   }
  28.   done
  29.   test ${broke} -eq 0 && primes+=" ${i}"
  30. done
  31.  
  32. echo "${primes}"
RAW Paste Data
Top