Forum     

Go Back   Digit Technology Discussion Forum > Software > Programming
Register FAQ Calendar Mark Forums Read

Programming The destination for developers - C, C++, Java, Python and the lot


Closed Thread
 
LinkBack Thread Tools Display Modes
Old 20-02-2008, 11:26 PM   #1 (permalink)
ex3n1us m4x1mus
 
preshit.net's Avatar
 
Join Date: Nov 2006
Location: Mumbai, India
Posts: 949
Default Shell script to find whether number is prime or not


Okay,

So we've been told to write this shell script to find whether a number taken from user is prime or not.

I know the log, just haven't been able to implement it.

Can anyone help ?

Thanks
__________________
Uzgimaga
preshit.net is offline  
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 21-02-2008, 09:36 AM   #2 (permalink)
The Smaller Bang
 
MetalheadGautham's Avatar
 
Join Date: Sep 2007
Location: Gautham City
Posts: 7,489
Default Re: Shell script to find whether number is prime or not

Quote:
Originally Posted by DARK LORD View Post
Okay,

So we've been told to write this shell script to find whether a number taken from user is prime or not.

I know the log, just haven't been able to implement it.

Can anyone help ?

Thanks
tried dividing it by all integer numbers till half of itself, and piping the resultant numbers to a log file, and checking the log file for number of lines where the content is just <0> ? If the number is 2, its a prime number.
__________________
http://TheSmallerBang.wordpress.com
eMachines E725 - T4400 2.2GHz, 1GB, 160GB
Nokia 5130XM * T-Sonic 610 2GB
Nokia 2323C * Samsung Galaxy Y
Apple iPad 2 16GB WiFi
MetalheadGautham is offline  
Old 21-02-2008, 01:16 PM   #3 (permalink)
String Phreak
 
mediator's Avatar
 
Join Date: Mar 2005
Location: In ur Evil Mind!
Posts: 2,457
Default Re: Shell script to find whether number is prime or not

Quote:
Originally Posted by DARK LORD View Post
Okay,

So we've been told to write this shell script to find whether a number taken from user is prime or not.

I know the log, just haven't been able to implement it.

Can anyone help ?

Thanks
Where r u getting the problem? Show ur script!
__________________
Bad Bad server.....No candy for u!
mediator is offline  
Old 21-02-2008, 04:24 PM   #4 (permalink)
18 Till I Die............
 
Join Date: Jul 2004
Location: India, Mumbai, Marine Lines
Posts: 5,792
Default Re: Shell script to find whether number is prime or not

I am able to get it after reading ABS and BGB, but for one problem. 1 is identified as prime. What would be the best method to avoid this?
I can certainly give a simple if statement to check if input is 1, if it is then it will directly print not prime.
Any better way?
__________________
http://www.bash.org/?258908
mehulved is offline  
Old 21-02-2008, 04:39 PM   #5 (permalink)
String Phreak
 
mediator's Avatar
 
Join Date: Mar 2005
Location: In ur Evil Mind!
Posts: 2,457
Default Re: Shell script to find whether number is prime or not

- count = 0
- for i = 1 to number,
if number % i = 0
count=count+1
- if count <=2, then print => prime

So for 1, count will be 1 and for every other prime it will be 2.
__________________
Bad Bad server.....No candy for u!
mediator is offline  
Old 21-02-2008, 05:51 PM   #6 (permalink)
Broken In
 
r2d2's Avatar
 
Join Date: Oct 2006
Posts: 174
Default Re: Shell script to find whether number is prime or not

OK, converted from one of my earlier c program


btw, prime checking can be done upto square root, which will require a little more work.

Code:
 
#!/bin/sh
 
i=2
rem=1
 
echo -e "Enter a number: \c"
read num
 
if [ $num -lt 2 ]; then
 echo -e "$num is not prime\n"
 exit 0
fi 
 
while [ $i -le `expr $num / 2` -a $rem -ne 0 ]; do
 rem=`expr $num % $i`
 i=`expr $i + 1`
done
 
if [ $rem -ne 0 ]; then
 echo -e "$num is prime\n"
else
 echo -e "$num is not prime\n"
fi
r2d2 is offline  
Old 21-02-2008, 06:26 PM   #7 (permalink)
String Phreak
 
mediator's Avatar
 
Join Date: Mar 2005
Location: In ur Evil Mind!
Posts: 2,457
Default Re: Shell script to find whether number is prime or not

Ya the running time wud be less then.
__________________
Bad Bad server.....No candy for u!
mediator is offline  
Closed Thread

Bookmarks

Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How To Find Total Number of Websites? muralikrishnan QnA (read only) 5 21-06-2006 12:46 PM
mul. in shell script legolas Open Source 4 24-05-2006 06:46 PM
Alias Does Not Work In Shell Script mannahazarika Open Source 3 24-01-2006 05:48 PM
Code to find Number of command buttons sid_b6505 QnA (read only) 3 06-12-2005 06:19 PM
Code to find number of command buttons sid_b6505 Tutorials 1 04-12-2005 09:34 PM

 
Latest Threads
- by Charan
- by Sarath
- by clmlbx

Advertisement




All times are GMT +5.5. The time now is 12:52 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2