To get a range of prime numbers
I need to write a shell script that will output prime numbers within a range.. what I tried is to check whether a number is prime or not set -vx echo "Enter the number" read N I=$N J=2 until [ $J == $N ] do if [ `expr $I % $J` == 0] then echo "The number is not prime" exit else J=`expr $J + 1` fi done echo "The number is not prime" I wrote the program that will output prime numbers within a range in C the program in C is int n,m,i,j,p printf ("Enter the first number") scanf ("%d",&n ) printf ("Enter the second number") scanf ("%d",&m ) for ( i=m;i<=n;i++) { p=0 for(j=2;j<=i/2;j++) { if(i%j==0) { p=1 break; } printf ("%d", i ); else { printf ("%d, is not prime" ); } } } I dont know how to write these conditions in scripting using for loop Can you help me to solve this. |
Look at "help for". The form like "for (( num=1; num<$max; num+=2));do " might be useful for you. You don't need to check even numbers of course, and the max number you need to check is the square root of the highest number you want to check.
|
for loop
can you tell me what is this " help for " Is this a website
|
it means type help for at the command prompt
genix@xion:~$ help for for: for NAME [in WORDS ... ;] do COMMANDS; done The `for' loop executes a sequence of commands for each member in a list of items. If `in WORDS ...;' is not present, then `in "$@"' is assumed. For each element in WORDS, NAME is set to that element, and the COMMANDS are executed. for ((: for (( exp1; exp2; exp3 )); do COMMANDS; done Equivalent to (( EXP1 )) while (( EXP2 )); do COMMANDS (( EXP3 )) done EXP1, EXP2, and EXP3 are arithmetic expressions. If any expression is omitted, it behaves as if it evaluates to 1. |
Thank you friend...
I tried with for loop as you said But my script has certain problems Can you guys help with this problems 1) the number 2 is not counting as a prime number 2) the numbers like 9,15...etc are counting as prime numbers echo "Enter the number" read N echo "Enter the second number" read M for (( i=$N+1; i <= $M-1; i++ )) do for (( j=2; j <= $i-1; j++ )) do if [ `expr $i % $j` = 0 ] then p=1 break else p=0 echo $i fi done done |
Try like this:
echo "Enter the number" read N echo "Enter the second number" read M for (( i=$N+1; i <= $M-1; i++ )) do p=0 for (( j=2; j <= $i-1; j++ )) do if [ `expr $i % $j` = 0 ] then p=1 break fi done if [ `expr $p` = 0 ] then echo $i fi done |
if [ `expr $p` = 0 ]
should be if [[ `expr $p` -eq 0 ]] use symbolic comparators ( = < > ) for strings, strings (-eq -lt -le ..) for numbers (yeah, i know i looks weird, but them's the rules) |
Well when you compare 2 strings, what is actually compared (from my knowledge, although I am not very experienced with bash scripting, tell me if I'm wrong) is the ascii value of the letters, one by one.... so theoretically it's still a number comparison, and when you compare 2 numbers with "=", it should provide the same result... Anyway, it seems to work, I haven't tested it throughoutly...
|
Read and enjoy: http://www.tldp.org/LDP/abs/html/comparison-ops.html
:) |
Ok thanks for the clarification.
Conclusion: although both methods work (in this case), the -eq method is more correct because it compares the actual numbers, not their ascii code. |
Thank you,dear "Valkyrie_of_valhalla"....I got the answer.You rock!!!!!!!!!
|
shell script to get range of prime numbers
Check bellow link for a shell script which finds prime numbers within
a range and between the range http://bashscript.blogspot.com/2009/...e-numbers.html |
using bash for prime numbers is like
trying to break a sledgehammer with a nut! hard work. |
All times are GMT -5. The time now is 08:54 AM. |