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.
yaya.. that is my homewrok assignment..because i'm really new in this script,do not have any idea to do it. so need some help here..
question description:
when user enter the number, it will define the number whether primary number or not. *primary number is 1,2,3,5,7,11,13,.... (cannot be devided by some number)
hope anyone can help me..
thanks..
Last edited by dicksonmakts; 04-03-2006 at 04:30 AM.
Though I also don't know shell scripts, I would rather advise you to get the method of knowing that procedure from a maths teacher. There should be a calculation to know how to know this effectively then afterwards it will be easy to implement it in any language!
There are many algorithms for determining what you want. The most well known being the Sieve of Eratosthenes. It would do you well to understand the procedure BEFORE you try to develop code to do it for you! Once you've figured out HOW to do your own homework, you might be able to ask LQ about some script commands that will implement the algortihm.
Incidently, they are not primary numbers. They are called prime numbers.
prime number is a number that is only divisible by one and itself.
may be small flaws in below algorithm but should basically work ....
prime=0 #just using as toggle 0=default(prime), 1=not prime
divisor=2 # start at 2 because all numbers are divis by 1
get number
# in while loop divisor cant be bigger than 1/2 number because
# well basic maths gives an answer to that
while (divisor <= number/2) and (prime=0)
remainder = number%divisor # where % returns the remainder of the division
if remainder=0
prime=1
end if
increment divisor
end while
if prime=1
print that number is not prime
else
print number is prime
Quote:
I want to see the guy who codes that in bash!
I reckon I could write that (or something similar) in bash. I'm not gonna do homework for someone.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.