Skip to content

Commit 1738bd8

Browse files
authored
Merge pull request #562 from 1Conan/1conan_prime_numbers_bash
Add prime_numbers (Bash)
2 parents 9e64b75 + 877ecb1 commit 1738bd8

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

prime_number/bash/prime.sh

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/bin/bash
2+
# Bash implementation of
3+
# https://github.com/hacktoberfest17/programming/blob/master/prime_number/c/prime_number.c
4+
5+
6+
function isPrime {
7+
INPUT=$1
8+
COUNTER=3
9+
RESULT=true
10+
if (( INPUT < 2 )); then
11+
RESULT=false
12+
elif (( INPUT == 2 )); then
13+
RESULT=true
14+
elif (( INPUT % 2 == 0 )); then
15+
RESULT=false
16+
else
17+
X=$(echo "sqrt(${INPUT})" | bc)
18+
while (( COUNTER <= X )); do
19+
if (( INPUT % COUNTER == 0 )); then
20+
RESULT=false
21+
fi
22+
((COUNTER++))
23+
done
24+
fi
25+
}
26+
27+
isPrime $1
28+
if [[ "${RESULT}" == "true" ]]; then
29+
echo "It's a prime!"
30+
else
31+
echo "It's not a prime!"
32+
fi

0 commit comments

Comments
 (0)