LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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


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. 21 30.88%
I invented a coding language. 0 0%
This is a stupid poll. Get a life. 1 1.47%
Voters: 68. You may not vote on this poll

Reply
  Search this Thread
Old 12-05-2009, 01:08 PM   #16
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled

Quote:
Originally Posted by Wim Sturkenboom View Post
I just code.
Shirley, you do other things as well.
Kevin Barry

Last edited by ta0kira; 12-05-2009 at 01:10 PM. Reason: corrected spelling of "Shirley" (accidentally said "surely")
 
Old 12-05-2009, 01:15 PM   #17
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
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
 
Old 12-07-2009, 08:51 AM   #18
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 GrapefruiTgirl View Post
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)
 
Old 12-07-2009, 01:43 PM   #19
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by MBybee View Post
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)
Better yet, calculate an SVD! Good luck with that one.
Kevin Barry
 
Old 12-07-2009, 02:48 PM   #20
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
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
 
Old 12-07-2009, 03:43 PM   #21
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 GrapefruiTgirl View Post
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
 
Old 12-07-2009, 04:02 PM   #22
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
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?
 
Old 12-07-2009, 04:33 PM   #23
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Quote:
Originally Posted by ta0kira View Post
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.
 
Old 12-07-2009, 04:35 PM   #24
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by MTK358 View Post
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:
  1. 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
  2. add the number to the list of primes
  3. increment and return to 1
This could be done in bash fairly easily.
Kevin Barry

Last edited by ta0kira; 12-07-2009 at 04:38 PM.
 
Old 12-07-2009, 04:39 PM   #25
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
I'm not sure I understand. Maybe you can write it out more clearly?
 
Old 12-07-2009, 04:43 PM   #26
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by PTrenholme View Post
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
 
Old 12-07-2009, 04:46 PM   #27
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 MTK358 View Post
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?
It's possible - I've done it on everything from VAX to IA64 and all sorts of platforms in between.
Give it a try
 
Old 12-07-2009, 04:53 PM   #28
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by MTK358 View Post
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)
}
Kevin Barry
 
Old 12-07-2009, 04:58 PM   #29
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by MTK358 View Post
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?
Another way would be to do SoE with a moderately-sized array:
  1. allocate an array of X integers, starting with where the previous iteration left off
  2. remove all multiples of the primes already found
  3. perform Sieve of Eratosthenes within the current array
  4. push the remaining numbers (the primes) into a list of primes already found
  5. return to 1

You can estimate the number of primes less than a number with x/ln(x); the percentage of primes drops quite a bit with magnitude.
Kevin Barry

Last edited by ta0kira; 12-07-2009 at 05:00 PM.
 
Old 12-07-2009, 05:10 PM   #30
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by ta0kira View Post
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)
}
Kevin Barry

I still don't understand why this works, but I'll try it in Python.
 
  


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
What is this program/script? centosfan Linux - Server 3 01-12-2008 09:01 AM
See if a program is already running in a script tytower Linux - Desktop 6 01-27-2007 01:26 AM
Script to run a program bluedude2288 Programming 3 06-11-2004 05:13 PM
Start Program Script Cafrow Linux - Newbie 1 08-17-2003 04:26 AM
Keylogging script/program TomG Programming 3 08-06-2002 03:13 PM

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

All times are GMT -5. The time now is 08:13 AM.

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