[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

golfing Eratosthenes

Daniel Baird

8/2/2006 7:23:00 AM

Hi all,

I've been golfing around with the sieve of Eratosthenes. Here's what
I've got so far:

a=(2..100).to_a;p a.each{|c|a.map!{|d|c&&d&&c<d&&d%c==0?nil:d}}.compact

It's already under 80 chars, but I'd still love to remove the
definition of the array a, and do the whole thing with no semicolons.
Any suggestions?

If you're interested, here's an indented/commented copy that might
save a few minutes..

# create an array of integers, 2 to 100
a=(2..100).to_a;
p a.each{ |c|
# for each element c in that array, if c isn't nil, it's prime. so
set multiples of c to nil
a.map!{ |d|
c && d && c<d && d%c == 0 ? nil : d
}
}.compact # finally, print the array minus the nils


I'd appreciate any comments.

Cheers

;Daniel


--
Daniel Baird
http://tidd... (free, effortless TiddlyWiki hosting)
http://danie... (TiddlyW;nks! :: Whiteboard Koala :: Blog ::
Things That Suck)

13 Answers

Daniel Baird

8/2/2006 7:54:00 AM

0

On 8/2/06, Daniel Baird <danielbaird@gmail.com> wrote:
> Hi all,
>
> I've been golfing around with the sieve of Eratosthenes. Here's what
> I've got so far:
>
> a=(2..100).to_a;p a.each{|c|a.map!{|d|c&&d&&c<d&&d%c==0?nil:d}}.compact
>
> It's already under 80 chars, but I'd still love to remove the
> definition of the array a, and do the whole thing with no semicolons.
> Any suggestions?
>

Improvement:

a=(2..100).to_a;p a.each{|c|a.reject!{|d|c<d&&d%c==0}}

..swapped to reject, and now I don't have to test for c and d being
nil, or do the final compact. Seems ok even though I'm editing the
array I'm looping through..

;D

--
Daniel Baird
http://tidd... (free, effortless TiddlyWiki hosting)
http://danie... (TiddlyW;nks! :: Whiteboard Koala :: Blog ::
Things That Suck)

Farrel Lifson

8/2/2006 7:58:00 AM

0

On 02/08/06, Daniel Baird <danielbaird@gmail.com> wrote:
> Hi all,
>
> I've been golfing around with the sieve of Eratosthenes. Here's what
> I've got so far:
>
> a=(2..100).to_a;p a.each{|c|a.map!{|d|c&&d&&c<d&&d%c==0?nil:d}}.compact
>
> It's already under 80 chars, but I'd still love to remove the
> definition of the array a, and do the whole thing with no semicolons.
> Any suggestions?
>
> If you're interested, here's an indented/commented copy that might
> save a few minutes..
>
> # create an array of integers, 2 to 100
> a=(2..100).to_a;
> p a.each{ |c|
> # for each element c in that array, if c isn't nil, it's prime. so
> set multiples of c to nil
> a.map!{ |d|
> c && d && c<d && d%c == 0 ? nil : d
> }
> }.compact # finally, print the array minus the nils
>
>
> I'd appreciate any comments.
>
> Cheers
>
> ;Daniel
>
>
> --
> Daniel Baird
> http://tidd... (free, effortless TiddlyWiki hosting)
> http://danie... (TiddlyW;nks! :: Whiteboard Koala :: Blog ::
> Things That Suck)

Here's my attempt. Note that I have made it a bit more general by
assigning s = 100 beforehand.

(2..(s**0.5)).inject((2..s).to_a){|a,c|a.map!{|d|c&&d&&c<d&&d%c==0?nil:d}}.compact

I got rid of the semi-colons and assigning the arrays (although
technically it's still 'assigned' in the inject) although it might has
made it a bit loner.

Farrel

Farrel Lifson

8/2/2006 8:00:00 AM

0

On 02/08/06, Farrel Lifson <farrel.lifson@gmail.com> wrote:
> On 02/08/06, Daniel Baird <danielbaird@gmail.com> wrote:
> > Hi all,
> >
> > I've been golfing around with the sieve of Eratosthenes. Here's what
> > I've got so far:
> >
> > a=(2..100).to_a;p a.each{|c|a.map!{|d|c&&d&&c<d&&d%c==0?nil:d}}.compact
> >
> > It's already under 80 chars, but I'd still love to remove the
> > definition of the array a, and do the whole thing with no semicolons.
> > Any suggestions?
> >
> > If you're interested, here's an indented/commented copy that might
> > save a few minutes..
> >
> > # create an array of integers, 2 to 100
> > a=(2..100).to_a;
> > p a.each{ |c|
> > # for each element c in that array, if c isn't nil, it's prime. so
> > set multiples of c to nil
> > a.map!{ |d|
> > c && d && c<d && d%c == 0 ? nil : d
> > }
> > }.compact # finally, print the array minus the nils
> >
> >
> > I'd appreciate any comments.
> >
> > Cheers
> >
> > ;Daniel
> >
> >
> > --
> > Daniel Baird
> > http://tidd... (free, effortless TiddlyWiki hosting)
> > http://danie... (TiddlyW;nks! :: Whiteboard Koala :: Blog ::
> > Things That Suck)
>
> Here's my attempt. Note that I have made it a bit more general by
> assigning s = 100 beforehand.
>
> (2..(s**0.5)).inject((2..s).to_a){|a,c|a.map!{|d|c&&d&&c<d&&d%c==0?nil:d}}.compact
>
> I got rid of the semi-colons and assigning the arrays (although
> technically it's still 'assigned' in the inject) although it might has
> made it a bit loner.
>
> Farrel
>

Gah! Excuse my atrocious spelling and grammar. "...although it has
made it a bit longer".

William James

8/2/2006 9:20:00 AM

0

Daniel Baird wrote:
> On 8/2/06, Daniel Baird <danielbaird@gmail.com> wrote:
> > Hi all,
> >
> > I've been golfing around with the sieve of Eratosthenes. Here's what
> > I've got so far:
> >
> > a=(2..100).to_a;p a.each{|c|a.map!{|d|c&&d&&c<d&&d%c==0?nil:d}}.compact
> >
> > It's already under 80 chars, but I'd still love to remove the
> > definition of the array a, and do the whole thing with no semicolons.
> > Any suggestions?
> >
>
> Improvement:
>
> a=(2..100).to_a;p a.each{|c|a.reject!{|d|c<d&&d%c==0}}
>
> .swapped to reject, and now I don't have to test for c and d being
> nil, or do the final compact. Seems ok even though I'm editing the
> array I'm looping through..

p (2..100).inject([]){|a,n|a.any?{|i|n%i==0}?a:a<<n}

William James

8/2/2006 9:24:00 AM

0

William James wrote:
> Daniel Baird wrote:
> > On 8/2/06, Daniel Baird <danielbaird@gmail.com> wrote:
> > > Hi all,
> > >
> > > I've been golfing around with the sieve of Eratosthenes. Here's what
> > > I've got so far:
> > >
> > > a=(2..100).to_a;p a.each{|c|a.map!{|d|c&&d&&c<d&&d%c==0?nil:d}}.compact
> > >
> > > It's already under 80 chars, but I'd still love to remove the
> > > definition of the array a, and do the whole thing with no semicolons.
> > > Any suggestions?
> > >
> >
> > Improvement:
> >
> > a=(2..100).to_a;p a.each{|c|a.reject!{|d|c<d&&d%c==0}}
> >
> > .swapped to reject, and now I don't have to test for c and d being
> > nil, or do the final compact. Seems ok even though I'm editing the
> > array I'm looping through..
>
> p (2..100).inject([]){|a,n|a.any?{|i|n%i==0}?a:a<<n}
p (2..100).inject([]){|a,n|a.any?{|i|n%i<1}?a:a<<n}

Wang Austin-W22255

8/2/2006 9:33:00 AM

0

Hi Ruby Gurus,

Could you please point me how to do some scientific calculation in Ruby:

1) How to calculate std.dev for an integer array?
2) Is there a log() function in Ruby?

Thank you!
Austin

Sander Land

8/2/2006 10:38:00 AM

0

On 8/2/06, Wang Austin-W22255 <xuwang@motorola.com> wrote:
> Hi Ruby Gurus,
>
> Could you please point me how to do some scientific calculation in Ruby:
>
> 1) How to calculate std.dev for an integer array?
> 2) Is there a log() function in Ruby?
>
> Thank you!
> Austin

1) I don't think there's a builtin function for that, so you'll have
to create your own.
You'll probably want to add it to the Array class.
class Array
def stddev
# your function here
end
end

You could also check rubyforge.org, maybe there's a statistics library
available there.

2) Math.log

Paul Battley

8/2/2006 10:57:00 AM

0

On 02/08/06, Robert Dober <robert.dober@gmail.com> wrote:
> you'd love this one
> [*2..100]

That's lovely. Why haven't I seen it before?!

Paul.

jogloran

8/2/2006 11:03:00 AM

0

class Array
def sd(ar)
n = length
sum_sq = inject(0.0) {|accu, v| accu + v*v}
sum = inject(0.0) {|accu, v| accu + v}

Math.sqrt((sum_sq - sum*sum/n) / (n-1))
end
end

On 8/2/06, Sander Land <sander.land@gmail.com> wrote:
> On 8/2/06, Wang Austin-W22255 <xuwang@motorola.com> wrote:
> > Hi Ruby Gurus,
> >
> > Could you please point me how to do some scientific calculation in Ruby:
> >
> > 1) How to calculate std.dev for an integer array?
> > 2) Is there a log() function in Ruby?
> >
> > Thank you!
> > Austin
>
> 1) I don't think there's a builtin function for that, so you'll have
> to create your own.
> You'll probably want to add it to the Array class.
> class Array
> def stddev
> # your function here
> end
> end
>
> You could also check rubyforge.org, maybe there's a statistics library
> available there.
>
> 2) Math.log
>
>

Jano Svitok

8/2/2006 11:07:00 AM

0

On 8/2/06, Wang Austin-W22255 <xuwang@motorola.com> wrote:
> 1) How to calculate std.dev for an integer array?

try GSL: http://rb-gsl.rubyforge.org/...

J.