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.
View Poll Results: Do you do any programming or scripting?
I have no idea what you're talking about.
1
1.47%
I get dizzy just looking at code.
3
4.41%
I've played around with both, but I don't do either regularly.
10
14.71%
I can write great scripts, but I leave programming to others.
10
14.71%
I do programming, but I'm not too good at scripting.
11
16.18%
I am a whiz at scripting, programming, and French toast.
11
16.18%
I refuse to recognize a difference between the two.
I make excellent french toast, with cinnamon, vanilla, and walnut/pecan bits in it. Mmmm
And, as much as I'd like to (and keep telling myself I will) learn C well enough to write something useful, I usually stick to bash these days; it's good brain exercise, and I really enjoy trying to make bash do everything.
I make excellent french toast, with cinnamon, vanilla, and walnut/pecan bits in it. Mmmm
And, as much as I'd like to (and keep telling myself I will) learn C well enough to write something useful, I usually stick to bash these days; it's good brain exercise, and I really enjoy trying to make bash do everything.
Sasha
That sounds like an interesting challenge. Have you tried doing the prime numbers example with bash? I should think that would be fun
I usually use that as my first test in every new language (and on every system)
I don't know what an SVD is, and no, I haven't (bothered) to try calculating prime numbers using bash alone. But I *did* a couple years ago write a great PHP webpage for doing prime numbers. I started it because my roommate told me that way back when she was in some computer course many years ago, they were asked to come up with code to calculate prime numbers, and the code could be no longer than XX number of lines (I can't recall what XX was..) so I decided since I was doing web developing at that time and very much into PHP, I would see if I could do it.
I was quite impressed with the result, and it ran very fast on my local Apache installation. Unfortunately, about the same time my old 80Gib HDD died and shat its contents (including all my PHP codes) out in a million pieces, the free hosting provider I had been using got hacked and went down; when it resurfaced, all user data was gone. So, I no longer have that code :/
Note that this all happened before I learned about Linux, and regular backups
I don't know what an SVD is, and no, I haven't (bothered) to try calculating prime numbers using bash alone. But I *did* a couple years ago write a great PHP webpage for doing prime numbers. I started it because my roommate told me that way back when she was in some computer course many years ago, they were asked to come up with code to calculate prime numbers, and the code could be no longer than XX number of lines (I can't recall what XX was..) so I decided since I was doing web developing at that time and very much into PHP, I would see if I could do it.
I was quite impressed with the result, and it ran very fast on my local Apache installation. Unfortunately, about the same time my old 80Gib HDD died and shat its contents (including all my PHP codes) out in a million pieces, the free hosting provider I had been using got hacked and went down; when it resurfaced, all user data was gone. So, I no longer have that code :/
Note that this all happened before I learned about Linux, and regular backups
Sasha
Ahh - I've given out that assignment a few times. I don't limit lines, however. I just award points for speed, clarity, and accuracy - usually sorting all primes under (some random limit).
I don't typically tell people *how* to do it, but nearly everyone uses the Sieve of Eratosthenes. It's probably the simplest method possible.
Last edited by MBybee; 12-07-2009 at 03:44 PM.
Reason: typo
Better yet, calculate an SVD! Good luck with that one.
Kevin Barry
If by SVD you mean a singular value decomposition, that problem is fairly easily reduced to finding the eigenvalues of a symmetric, square matrix. That problem is fairly easily solved.
I read about the Sieve of Eratosthenes and it makes sense. I just wonder how can it be done without a list so big it takes up all your RAM?
I've done it in the opposite manner, where rather than starting with a full list I created a list of primes as I went, which uses up less memory for huge ranges:
take the current number, mod out all integers (separately) in the list of primes aleady found <= its sqrt, skip to 3 if any are == 0
add the number to the list of primes
increment and return to 1
This could be done in bash fairly easily.
Kevin Barry
If by SVD you mean a singular value decomposition, that problem is fairly easily reduced to finding the eigenvalues of a symmetric, square matrix. That problem is fairly easily solved.
It's not a difficult problem, even to do by hand; however, it also requires finding eigenvectors, multiplying matrices, and possibly finding nullspaces. I'm not quite sure how you'd do that all in bash, unless you piped it to MATLAB or some other program. Granted, it would take a while to write something by hand in C/C++ without using a 3rd-party library, vs. forever in bash. The point is that multi-dimensional manipulation is somewhat difficult in bash.
Kevin Barry
I'm not sure I understand. Maybe you can write it out more clearly?
C++-ish pseudocode:
Code:
list primes
int current = 1
int maximum = 1000000
while ++current < maximum
{
int root = sqrt(current)
bool prime = true
for I = 0; I < primes.size && primes[ I ] <= root; I++
if current % primes[ I ] == 0
{
prime = false
break
}
if prime
primes.push_back(current)
}
list primes
int current = 1
int maximum = 1000000
while ++current < maximum
{
int root = sqrt(current)
bool prime = true
for I = 0; I < primes.size && primes[ I ] <= root; I++
if current % primes[ I ] == 0
{
prime = false
break
}
if prime
primes.push_back(current)
}
Kevin Barry
I still don't understand why this works, but I'll try it in Python.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.