Last active
July 26, 2021 19:18
-
-
Save smartwatermelon/fb3dada50015a5923d729a33723bf55c to your computer and use it in GitHub Desktop.
@cassidoo's interview question from July 25, 2021
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -eu -o pipefail | |
# Given a positive integer n, write a function that finds the number of zeros | |
# at the end of n! in base 10. | |
if [ $# -eq 0 ] || [ -n "$(printf '%s\n' "$1" | sed 's/[0-9]//g')" ]; then | |
echo "$(basename "$0"): enter a positive integer" | |
exit 1 | |
fi | |
IN=$1 | |
echo "you entered $IN" | |
F=$( BC_LINE_LENGTH=10000 bc <<< "$( seq -s '*' 1 "${IN}" | sed 's/*$//g' )" ) | |
echo "factorial $IN is ${F}" | |
RF=$( rev <<< "${F}" ) | |
COUNT=0 | |
S="" | |
for (( i=0; i<${#RF}; ++i )); do | |
if [ "${RF:i:1}" -eq "0" ]; then | |
COUNT=$(( COUNT+1 )) | |
else | |
break | |
fi | |
done | |
if [ $COUNT -ne 1 ]; then S="s"; fi | |
echo "${F} ends with ${COUNT} zero${S}." |
Author
smartwatermelon
commented
Jul 26, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment