Last active
August 29, 2015 13:56
-
-
Save SlaunchaMan/9255518 to your computer and use it in GitHub Desktop.
Prime Factors Kata in Bash with Roundup
This file contains 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 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" | |
} |
This file contains 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 | |
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