LinuxQuestions.org
Help answer threads with 0 replies.
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 12-09-2009, 01:46 AM   #1
ashok.g
Member
 
Registered: Dec 2009
Location: Hyderabad,India
Distribution: RHEl AS 4
Posts: 215

Rep: Reputation: 32
Prime Generation Code Not Working Properly


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
 
Old 12-09-2009, 03:28 AM   #2
ashok.g
Member
 
Registered: Dec 2009
Location: Hyderabad,India
Distribution: RHEl AS 4
Posts: 215

Original Poster
Rep: Reputation: 32
Before you do I did it for myself

Before you do I did it for myself

Quote:
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
 
Old 12-09-2009, 05:46 AM   #3
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
Blog Entries: 54

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
Why did you exclude the number 2?
 
1 members found this post helpful.
Old 12-09-2009, 06:48 AM   #4
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Using code tags is generally better for showing your own code than using quote tags.

Quote:
Originally Posted by ashok.g View Post
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.
 
Old 12-09-2009, 09:48 AM   #5
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
Is this supposed to be Bash or POSIX sh?
 
Old 12-09-2009, 11:58 PM   #6
ashok.g
Member
 
Registered: Dec 2009
Location: Hyderabad,India
Distribution: RHEl AS 4
Posts: 215

Original Poster
Rep: Reputation: 32
its under bash....
 
Old 12-10-2009, 12:30 AM   #7
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
Don't use [ under bash, it's completely deprecated and very dangerous. You really should be using arithmetic expressions $(()) anyway.

And edit your posts to use [code] tags with proper indentation!
 
Old 12-10-2009, 02:36 PM   #8
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Quote:
Originally Posted by tuxdev View Post
Don't use [ under bash, it's completely deprecated and very dangerous.
Citation needed.
 
Old 12-10-2009, 02:47 PM   #9
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
http://mywiki.wooledge.org/BashGuide...ices/BashTests
 
1 members found this post helpful.
Old 12-10-2009, 03:04 PM   #10
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
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 ?
 
Old 12-10-2009, 04:16 PM   #11
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Quote:
Originally Posted by tuxdev View Post
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.
 
Old 12-10-2009, 05:08 PM   #12
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
Quote:
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.

Last edited by tuxdev; 12-10-2009 at 05:09 PM.
 
Old 12-10-2009, 05:27 PM   #13
MBybee
Member
 
Registered: Jan 2009
Location: wherever I can make a living
Distribution: OpenBSD / Debian / Ubuntu / Win7 / OpenVMS
Posts: 440

Rep: Reputation: 57
Quote:
Originally Posted by tuxdev View Post
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.
 
Old 12-10-2009, 05:38 PM   #14
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
Quote:
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.

Last edited by tuxdev; 12-10-2009 at 05:56 PM.
 
Old 12-10-2009, 08:09 PM   #15
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Back to the thread...

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
 
  


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
LXer: Freedom loving lawyers prime primer on open source code LXer Syndicated Linux News 0 10-13-2007 08:10 PM
A GCC code generation problem? dogbird Programming 4 12-09-2005 11:52 AM
tool for code generation? bcalmac Linux - Software 0 08-22-2005 10:41 AM
Code generation based on latex math ahwkong Programming 0 04-17-2005 06:28 AM
UML and C++ code generation GŠutama Linux - Software 2 11-13-2003 06:56 AM

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

All times are GMT -5. The time now is 06:03 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