Skip to content

Instantly share code, notes, and snippets.

@hugot
Created February 13, 2017 23:09
Show Gist options
  • Save hugot/49cf44c143b8994ba772968edf5b1bb4 to your computer and use it in GitHub Desktop.
Save hugot/49cf44c143b8994ba772968edf5b1bb4 to your computer and use it in GitHub Desktop.
calculating divisors the bash way
#!/usr/bin/env bash
# This script asks the user for a number and then provides the user with
# all even divisors for it
#----- functions -----
evenDivision() {
local result=$(( $((100 * $1)) / $2))
[[ "${result#*0}" == 0 ]]
}
#----- Application logic -----
evenDivisors=()
echo please provide me with a number
read number
for ((i=1; i<$number; i++)); do
if evenDivision $number $i; then
evenDivisors[${#evenDivisors[@]}]=$i
fi
done
echo these are the even divisors of your number: ${evenDivisors[*]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment