LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-14-2007, 12:35 AM   #1
som_kurian
Member
 
Registered: Oct 2007
Posts: 33

Rep: Reputation: 15
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.
 
Old 11-14-2007, 01:00 AM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
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.
 
Old 11-14-2007, 01:32 AM   #3
som_kurian
Member
 
Registered: Oct 2007
Posts: 33

Original Poster
Rep: Reputation: 15
for loop

can you tell me what is this " help for " Is this a website
 
Old 11-14-2007, 03:17 AM   #4
genix
LQ Newbie
 
Registered: Jul 2007
Distribution: slackware
Posts: 23

Rep: Reputation: 15
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.
 
1 members found this post helpful.
Old 11-16-2007, 02:47 AM   #5
som_kurian
Member
 
Registered: Oct 2007
Posts: 33

Original Poster
Rep: Reputation: 15
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
 
Old 11-16-2007, 11:09 AM   #6
Valkyrie_of_valhalla
Member
 
Registered: Jan 2006
Location: Romania
Distribution: Suse 12.0, Slackware 12.1, Debian, Ubuntu, Gentoo
Posts: 301

Rep: Reputation: 30
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
 
Old 11-17-2007, 08:28 PM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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)
 
Old 11-18-2007, 03:19 AM   #8
Valkyrie_of_valhalla
Member
 
Registered: Jan 2006
Location: Romania
Distribution: Suse 12.0, Slackware 12.1, Debian, Ubuntu, Gentoo
Posts: 301

Rep: Reputation: 30
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...
 
Old 11-19-2007, 01:30 AM   #9
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Read and enjoy: http://www.tldp.org/LDP/abs/html/comparison-ops.html
 
Old 11-19-2007, 03:06 PM   #10
Valkyrie_of_valhalla
Member
 
Registered: Jan 2006
Location: Romania
Distribution: Suse 12.0, Slackware 12.1, Debian, Ubuntu, Gentoo
Posts: 301

Rep: Reputation: 30
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.
 
Old 11-23-2007, 03:12 AM   #11
som_kurian
Member
 
Registered: Oct 2007
Posts: 33

Original Poster
Rep: Reputation: 15
Thank you,dear "Valkyrie_of_valhalla"....I got the answer.You rock!!!!!!!!!
 
Old 11-17-2009, 03:09 PM   #12
kvmreddy
LQ Newbie
 
Registered: Aug 2009
Posts: 15

Rep: Reputation: 3
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
 
Old 11-18-2009, 04:15 AM   #13
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
using bash for prime numbers is like
trying to break a sledgehammer with a nut!

hard work.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
RSA prime numbers? tarballedtux Linux - Security 19 06-28-2008 06:12 PM
BASH : how to act based on test against a range of numbers hollywoodb Programming 4 07-06-2006 06:09 PM
sequence of numbers, how to extract which numbers are missing jonlake Programming 13 06-26-2006 03:28 AM
Shell scripting - Random numbers within a range felixc Linux - Newbie 2 10-09-2005 05:41 PM
c++ prime numbers loop problem muddlnx Programming 6 08-31-2004 10:14 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:13 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration