repo.or.cz
/
lcapit-junk-code.git
/
blob
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
log
|
graphiclog1
|
graphiclog2
|
commit
|
commitdiff
|
tree
|
refs
|
edit
|
fork
blame
|
history
|
raw
|
HEAD
Introduce old redir program
[lcapit-junk-code.git]
/
prime
/
prime.sh
blob
8e69e4a0a422b97e06067523741e99ecc9f08540
1
#!/bin/bash
2
#
3
# Check if a number is prime (brute force)
4
#
5
# Luiz Fernando N. Capitulino
6
# <lcapitulino@gmail.com>
7
8
is_prime
()
9
{
10
local
i
11
local
res
12
local
number
=
$1
13
local
max
=
$
((
${number}
-
1
))
14
15
for
((
i
=
2
;
i
<
$max
;
i
++));
do
16
res
=
$
((
$number
%
$i
))
17
if
[
$res
-eq
0
];
then
18
return
0
19
fi
20
done
21
22
return
1
23
}
24
25
if
[
$#
-ne
1
];
then
26
echo
"usage: prime.sh < number >"
27
exit
1
28
fi
29
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
37
fi
38
39
exit
$ret