Skip to content

Instantly share code, notes, and snippets.

@andrewh
Last active April 13, 2016 14:56
Show Gist options
  • Save andrewh/1385770 to your computer and use it in GitHub Desktop.
Save andrewh/1385770 to your computer and use it in GitHub Desktop.
Prints the distribution of primes in the terminal using OpenSSL
#!/bin/sh
die () {
echo $1
exit $2
}
red () {
if [ -x $(which tput) ]; then
tput setaf 1
fi
}
blue () {
if [ -x $(which tput) ]; then
tput setaf 4
fi
}
yellow () {
if [ -x $(which tput) ]; then
tput setaf 3
fi
}
term_reset () {
if [ -x $(which tput) ]; then
tput sgr0
fi
}
check_openssl () {
openssl=$(which openssl)
test -x $openssl || die "can't find openssl - is it installed?" 1
}
status () {
yellow
printf "[$(basename $0)] "
term_reset
echo "${@}"
}
hide_cursor () {
tput civis
}
show_cursor () {
tput cnorm
}
main () {
check_openssl
status "print the distribution of primes using openssl!"
echo
status "enter the upper limit to count to -"
read ulimit
echo
red
printf "|"
term_reset
echo " = prime"
blue
printf "."
term_reset
echo " = composite"
echo
trap echo EXIT
hide_cursor
for num in $(seq 1 ${ulimit:-1000});
do
$openssl prime $num | grep --quiet 'is prime' 2>&1;
case $? in
0) red; /bin/echo -n '|'; term_reset ;;
*) blue; /bin/echo -n '.'; term_reset ;;
esac
done
show_cursor
}
main
@andrewh
Copy link
Author

andrewh commented Apr 13, 2016

$ ./primes.sh -h
[primes.sh] print the distribution of primes using openssl!

[primes.sh] enter the upper limit to count to -


| = prime
. = composite

.||.|.|...|.|...|.|...|.....|.|.....|...|.|...|.....|.....|.|.....|...|.|.....|...|.....|.......|...|.|...|.|...|.............|...|.....|.|.........|.|.....|.....|...|.....|.....|.|.........|.|...|.|...........|...........|...|.|...|.....|.|.........|.....|.....|.....|.|.....|...|.|.........|.............|...|.|...|.............|.....|.........|.|...|.....|.......|.....|.....|...|.....|.......|...|.......|.........|.|.........|.|.....|...|.....|.......|...|.|...|...........|.......|...|.......|...|.....|...........|.|.................|.....|.........|.....|.....|.|.....|.........|.....|.....|.|.....|.....|...|.|...........|.........|.|...|.....|.....|.|...........|...|.....|.......|.........|.......|.........|.......|.....|.....|...|.......|.....|...|.......|...|.............|.........|...........|.|.........|.|...|.|.........|.............|...|.|...|.............|...|.|...|...................|...|.......|.........|.......|...|.....|.....|.............|...|.....|.....|.......|.....|...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment