[SOLVED] Prime Generation Code Not Working Properly
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
My code for the generation of prime numbers from 2 to a value(given at run time) as fallows is not working. Can you assist me?
Quote:
clear
echo "Enter the Maximum Number"
read max
for(( i=2; i <= $max; i++ ))
do
if [ `expr $i % 2` -eq 0 ]
then
break
else
for(( j=i; j <= $max/2; j++ ))
do
if [ `expr $i % $j` -eq 0 ]
then break
else echo -n $i;
fi
done
fi
done
read
clear
echo "Enter the Maximum Number"
read max
echo "The Prime Numbers upto $max are:"
for (( i=1; i <= $max; i++ ))
do
p=1
if [ `expr $i % 2` -eq 0 ]
then p=0;
continue
else
for (( j=2; j <= $i/2; j++ ))
do
if [ `expr $i % $j` -eq 0 ]
then p=0
break
else
p=1
fi
done
if [ `expr $p` = 1 ]
then
echo -ne "$i \c"
fi
fi
done
echo -e " \nHit Enter to Continue......"
read
clear
Using code tags is generally better for showing your own code than using quote tags.
Quote:
Originally Posted by ashok.g
if [ `expr $i % 2` -eq 0 ]
then p=0;
continue
else
When i is 2, that incorrectly excludes it from the list of primes. When i isn't 2, that unnecessarily duplicates work that the inner loop does anyway. Either way, that excess code shouldn't be there.
Quote:
for (( j=2; j <= $i/2; j++ ))
If you care about performance for moderate to large values of i, using j*j<=i is a better test than j<=i/2.
(I don't know bash scripting very well. So I don't know where the $ is needed in such tests, nor whether there are other syntax difficulties with j*j<=i. I'm commenting just on the language independent algorithm.)
Quote:
else
p=1
fi
That is wasted work. p would already be 1 whenever that could be executed. Again I don't know the language, but I expect the else is optional, so you could replace all that with just fi.
And why not look up prime numbers generation code on the WEB ? And translate it from whatever procedural language ("C", Perl, Python, etc) into 'bash' if necessary ?
Can be dangerous, yes, if you don't know what you're doing. It can be ok if you've developed habits with [ that work for you. Indeed, for those who are just starting out, [[ is definitely better.
It was mainly the "deprecated" part that I was interested in. For those who are interested, go here.
Can be dangerous, yes, if you don't know what you're doing. It can be ok if you've developed habits with [ that work for you. Indeed, for those who are just starting out, [[ is definitely better.
There's no "can" here. It's just dangerous, plain and simple. Habits don't help you when you make mistakes, and you *will* make a mistake. The *only* advantage [ has is that it's POSIX, and [[ is not. As stated very clearly by that page.
It is very much deprecated, and no well-written bash script uses [. The only reason it exists is for POSIX compatibility.
There's no "can" here. It's just dangerous, plain and simple. Habits don't help you when you make mistakes, and you *will* make a mistake. The *only* advantage [ has is that it's POSIX, and [[ is not. As stated very clearly by that page.
It is very much deprecated, and no well-written bash script uses [. The only reason it exists is for POSIX compatibility.
Wow, calm down man. I would not only disagree with you on the danger, I would disagree on the global statements there.
For one thing, there are hundreds of thousands of scripts (or more) running this way. Like, almost all of them.
For another, I have never seen so much as a single case of this 'worst case' error happening in 20 years of Unix admin.
I see the point they make on that site, but seriously, relax.
For one thing, there are hundreds of thousands of scripts (or more) running this way. Like, almost all of them.
And hundreds of thousands of scripts are wrong and are time bombs just waiting for the right trigger to go off. Most people write not-quite-POSIX scripts, not Bash scripts, because it works "good enough" for simple throwaway stuff. If you're writing a Bash script that's supposed to stick around, you should use every feature Bash offers to avoid shooting your leg off. But, that's only for Bash scripting, POSIX scripting is very different.
Quote:
For another, I have never seen so much as a single case of this 'worst case' error happening in 20 years of Unix admin.
And I see a case of it at least twice a week on #bash. I suspect you actually have seen a case yourself, you just fix it and move on without realizing it.
OP: you might run into problems with variable scope as you have your loops now. You should put for...do...done in {...;} in this case, i.e. { for...do...done; }. Otherwise, p might not be modified for the outer loop.
Kevin Barry
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.