Home Forums HCL Reviews Tutorials Articles Register Search Today's Posts Mark Forums Read
 LinuxQuestions.org To get a range of prime numbers
 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

 11-14-2007, 12:35 AM #1 som_kurian Member   Registered: Oct 2007 Posts: 33 Rep: 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.
 11-14-2007, 01:00 AM #2 jschiwal LQ Guru   Registered: Aug 2001 Location: Fargo, ND Distribution: SuSE AMD64 Posts: 15,733 Rep: 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.
 11-14-2007, 01:32 AM #3 som_kurian Member   Registered: Oct 2007 Posts: 33 Original Poster Rep: for loop can you tell me what is this " help for " Is this a website
 11-14-2007, 03:17 AM #4 genix LQ Newbie   Registered: Jul 2007 Distribution: slackware Posts: 23 Rep: 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.
 11-16-2007, 02:47 AM #5 som_kurian Member   Registered: Oct 2007 Posts: 33 Original Poster Rep: 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
 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: 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
 11-17-2007, 08:28 PM #7 chrism01 LQ Guru   Registered: Aug 2004 Location: Sydney Distribution: Centos 6.9, Centos 7.3 Posts: 17,395 Rep: 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)
 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: 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...
 11-19-2007, 01:30 AM #9 chrism01 LQ Guru   Registered: Aug 2004 Location: Sydney Distribution: Centos 6.9, Centos 7.3 Posts: 17,395 Rep: Read and enjoy: http://www.tldp.org/LDP/abs/html/comparison-ops.html
 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: 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.
 11-23-2007, 03:12 AM #11 som_kurian Member   Registered: Oct 2007 Posts: 33 Original Poster Rep: Thank you,dear "Valkyrie_of_valhalla"....I got the answer.You rock!!!!!!!!!
 11-17-2009, 03:09 PM #12 kvmreddy LQ Newbie   Registered: Aug 2009 Posts: 15 Rep: 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
 11-18-2009, 04:15 AM #13 bigearsbilly Senior Member   Registered: Mar 2004 Location: england Distribution: Debian, Mint, Puppy, Raspbian Posts: 3,421 Rep: using bash for prime numbers is like trying to break a sledgehammer with a nut! hard work.

 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 Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post tarballedtux Linux - Security 19 06-28-2008 06:12 PM hollywoodb Programming 4 07-06-2006 06:09 PM jonlake Programming 13 06-26-2006 03:28 AM felixc Linux - Newbie 2 10-09-2005 05:41 PM muddlnx Programming 6 08-31-2004 10:14 PM

LinuxQuestions.org

All times are GMT -5. The time now is 12:41 PM.

 Contact Us - Advertising Info - Rules - LQ Merchandise - Donations - Contributing Member - LQ Sitemap -