Skip to content

Instantly share code, notes, and snippets.

@SlaunchaMan
Last active August 29, 2015 13:56
Show Gist options
  • Save SlaunchaMan/9255518 to your computer and use it in GitHub Desktop.
Save SlaunchaMan/9255518 to your computer and use it in GitHub Desktop.
Prime Factors Kata in Bash with Roundup
#!/usr/bin/env roundup
describe "prime factors"
it_succeeds_for_two() {
test "$(./primes.sh 2)" == "2"
}
it_succeeds_for_three() {
test "$(./primes.sh 3)" == "3"
}
it_succeeds_for_four() {
test "$(./primes.sh 4)" == "2 2"
}
it_succeeds_for_five() {
test "$(./primes.sh 5)" == "5"
}
it_succeeds_for_six() {
test "$(./primes.sh 6)" == "2 3"
}
it_succeeds_for_eight() {
test "$(./primes.sh 8)" == "2 2 2"
}
it_succeeds_for_nine() {
test "$(./primes.sh 9)" == "3 3"
}
it_succeeds_for_a_big_one() {
test "$(./primes.sh 245893)" == "23 10691"
}
#!/usr/bin/env bash
rem=$1
i=2
stop_at=$(echo "sqrt ( $1 )" | bc -l)
stop_at=${stop_at/.*}
while [ $i -le $stop_at ]; do
while [ $rem -gt $i -a $(($rem % $i)) == 0 ]; do
echo -n "$i "
rem=$(($rem / $i))
done
if [ $i == 2 ]; then
i=3
else
i=$((i + 2))
fi
done
echo $rem
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment