Introduce old redir program
[lcapit-junk-code.git] / prime / prime.sh
blob8e69e4a0a422b97e06067523741e99ecc9f08540
1 #!/bin/bash
3 # Check if a number is prime (brute force)
5 # Luiz Fernando N. Capitulino
6 # <lcapitulino@gmail.com>
8 is_prime()
10 local i
11 local res
12 local number=$1
13 local max=$((${number} - 1))
15 for ((i = 2; i < $max; i++)); do
16 res=$(($number % $i))
17 if [ $res -eq 0 ]; then
18 return 0
20 done
22 return 1
25 if [ $# -ne 1 ]; then
26 echo "usage: prime.sh < number >"
27 exit 1
30 is_prime $1
31 if [ $? -eq 1 ]; then
32 echo is prime
33 ret=1
34 else
35 echo is not prime
36 ret=0
39 exit $ret