Please, help (GCD) greatest common divisor

Write a program to read two integers and show their greatest common
divisor.

···

--
Posted via http://www.ruby-forum.com/.

What have ya got so far?

Sam

···

On 02/27/2013 07:28 AM, Caddy Tonks Lupin wrote:

Write a program to read two integers and show their greatest common
divisor.

Caddy:
Why do I smell homework here? I'll give you a hint:

Look here: http://www.ruby-doc.org/core-2.0/Integer.html

No need for do any real stuff. You will just have to write the code to
accept/verify integers and process them.

-Wayne

btw, have you heard of google :wink: ???
it's a search engine and gives ~19.000.000 hits to your request.

hth
ralf

···

On 02/26/2013 07:28 PM, Caddy Tonks Lupin wrote:

Write a program to read two integers and show their greatest common
divisor.

Best I could do on short notice:

gcd=->(_){_.sort_by(&:-@).inject{|p,q|q.nonzero?&&gcd[[q,p-q]]||--p}}
$/=' ';gets;puts gcd[[$_,gets].map &(method :Integer)]

···

--
Matma Rex

Wayne Brisette wrote in post #1099174:

Caddy:
Why do I smell homework here? I'll give you a hint:

Look here: Class: Integer (Ruby 2.0.0)

No need for do any real stuff. You will just have to write the code to
accept/verify integers and process them.

-Wayne

I need an algorithm, using "for i in ... do" ...

···

--
Posted via http://www.ruby-forum.com/\.

LOL

Henry

···

On 27/02/2013, at 7:48 AM, Bartosz Dziewoński <matma.rex@gmail.com> wrote:

Best I could do on short notice:

gcd=->(_){_.sort_by(&:-@).inject{|p,q|q.nonzero?&&gcd[[q,p-q]]||--p}}
$/=' ';gets;puts gcd[[$_,gets].map &(method :Integer)]

OMG this is gorgeous.

···

On Tue, Feb 26, 2013 at 12:48 PM, Bartosz Dziewoński <matma.rex@gmail.com> wrote:

Best I could do on short notice:

gcd=->(_){_.sort_by(&:-@).inject{|p,q|q.nonzero?&&gcd[[q,p-q]]||--p}}
$/=' ';gets;puts gcd[[$_,gets].map &(method :Integer)]

--
Matma Rex

Don't you think you'd learn better if you actually tried to figure it
out instead of asking a mailing list to do your work for you? At least
post some code that you've tried already, talk about what you've
researched, etc. I know there's at least one programming teacher/prof
that's a regular here as well, what makes you think that your teacher
isn't here and might catch you cheating?

-Ryan

···

On 2/26/13 12:48 PM, Caddy Tonks Lupin wrote:

Wayne Brisette wrote in post #1099174:

Caddy:
Why do I smell homework here? I'll give you a hint:

Look here: http://www.ruby-doc.org/core-2.0/Integer.html

No need for do any real stuff. You will just have to write the code to
accept/verify integers and process them.

-Wayne

I need an algorithm, using "for i in ... do" ...

Yes, it's beatiful! Thank you Bartosz! Even the most obvious
spoonfeeding thread can turn into beauty and art. Isn't that amazing?

Cheers

robert

···

On Tue, Feb 26, 2013 at 9:39 PM, Henry Maddocks <hmaddocks@me.com> wrote:

On 27/02/2013, at 7:48 AM, Bartosz Dziewoński <matma.rex@gmail.com> wrote:

Best I could do on short notice:

gcd=->(_){_.sort_by(&:-@).inject{|p,q|q.nonzero?&&gcd[[q,p-q]]||--p}}
$/=' ';gets;puts gcd[[$_,gets].map &(method :Integer)]

LOL

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Hello Everyone,

This thread is the best example why it is worth reading every single
mail in this list and I would like to thank Bartosz for his solution.
I didn't understand it at first so I came up with a little explanation
for the constructs used in it:

Would you mind looking into this gist whether I am on the right path?
Thanks for your time.

regards
Attila

···

On Wed, Feb 27, 2013 at 5:47 AM, tamouse mailing lists <tamouse.lists@gmail.com> wrote:

On Tue, Feb 26, 2013 at 12:48 PM, Bartosz Dziewoński > <matma.rex@gmail.com> wrote:

Best I could do on short notice:

gcd=->(_){_.sort_by(&:-@).inject{|p,q|q.nonzero?&&gcd[[q,p-q]]||--p}}
$/=' ';gets;puts gcd[[$_,gets].map &(method :Integer)]

Ryan Victory wrote in post #1099182:

Don't you think you'd learn better if you actually tried to figure it
out instead of asking a mailing list to do your work for you? At least
post some code that you've tried already, talk about what you've
researched, etc. I know there's at least one programming teacher/prof
that's a regular here as well, what makes you think that your teacher
isn't here and might catch you cheating?

-Ryan

I am japanese :), my teacher don't speak english :)... and this is not
a work, it is an attempt to project

···

--
Posted via http://www.ruby-forum.com/\.

I'm still puzzled why you rewrite things when GCD is built-in. What am I
missing?

Wayne

Replying to the gist.

### II. "inject"-part
# Breaking it down with [6,8]
gcd[[6,8]] #=> 2
gcd=->(_){_.sort_by(&:-@).inject{|p,q|q.nonzero?&&gcd[[q,p-q]]||--p}}
# ( I am sure that I am missing something here )
# 1st iteration: [8,6] -> 6 && gcd[[6,2]] || --8
# 2nd iteration: [6,2] -> 6 && 2 && gcd[[2,4]] || --6
# 3rd iteration: [4,2] -> 6 && 2 && 2 && gcd[[2,2]] || --4
# 4th iteration: [2,2] -> 6 && 2 && 2 && 2 && gcd[[2,0]] || --2
# 5th iteration: [2,0] -> 6 && 2 && 2 && 2 && nil && gcd[[0,2]] || --2
# On the last iteration everything evaluates to nil before the ||.# (One remaining question: why --p?)

#inject on a two-element array is essentially the same as splatting it in two separate variables. The following two snippets do the same thing:

     result = [1, 2].inject{|a, b| do_stuff_with(a, b) }

     a, b = *[1, 2]
     result = do_stuff_with(a, b)

"a && b || c" is the same as "if a; b; else c; end" (or "a ? b : c"). This is a little abuse of the behavior of boolean operators :slight_smile:

"--p" is just for laughs. While this looks like decrementation in C / C++, it parses as "-(-p)" as is the same as simply "p".

···

--
Matma Rex

Since this isn't homework, why do you want to build your own routine instead of
using the built-in tools?

It's super easy to use.

wayneb ~ $ irb
1.9.3-p374 :001 > 54.gcd(24)
=> 6

What is it you are trying to accomplish by using your own routines for this?
Maybe if we understood that it would help.

Wayne

My code was just an exercise in obfuscation.

···

On Wed, 27 Feb 2013 15:50:15 +0100, Wayne Brisette <wbrisett@att.net> wrote:

I'm still puzzled why you rewrite things when GCD is built-in. What am I
missing?

--
Matma Rex

Thanks for taking the time to looking into it!
I could figure out the boolean chain given the precedence but I
thought that I am missing something obvious with "--p" because I
couldn't twist more out of it in irb. ( ... and yes, my first thought
was that p is being decremented but that made no sense:)

I always found "inject" hard to grasp (my mind is like Pratchett's
A'tuin when it comes to abstraction) and your example gives a new
angle to chew on as I've never seen this approach before. Thanks!

If you're interested in obfuscated code: http://mamememo.blogspot.hu/
(Yusuke Endoh's blog)
I saved this for later because it's way over my head and he is writing
one twisted code:)
Thanks again.
Attila

···

On Wed, Feb 27, 2013 at 6:19 PM, Bartosz Dziewoński <matma.rex@gmail.com> wrote:

Replying to the gist.

### II. "inject"-part
# Breaking it down with [6,8]
gcd[[6,8]] #=> 2

gcd=->(_){_.sort_by(&:-@).inject{|p,q|q.nonzero?&&gcd[[q,p-q]]||--p}}
# ( I am sure that I am missing something here )
# 1st iteration: [8,6] -> 6 && gcd[[6,2]] || --8
# 2nd iteration: [6,2] -> 6 && 2 && gcd[[2,4]] || --6
# 3rd iteration: [4,2] -> 6 && 2 && 2 && gcd[[2,2]] || --4
# 4th iteration: [2,2] -> 6 && 2 && 2 && 2 && gcd[[2,0]] || --2
# 5th iteration: [2,0] -> 6 && 2 && 2 && 2 && nil && gcd[[0,2]] || --2
# On the last iteration everything evaluates to nil before the ||.# (One
remaining question: why --p?)

#inject on a two-element array is essentially the same as splatting it in
two separate variables. The following two snippets do the same thing:

    result = [1, 2].inject{|a, b| do_stuff_with(a, b) }

    a, b = *[1, 2]
    result = do_stuff_with(a, b)

"a && b || c" is the same as "if a; b; else c; end" (or "a ? b : c"). This
is a little abuse of the behavior of boolean operators :slight_smile:

"--p" is just for laughs. While this looks like decrementation in C / C++,
it parses as "-(-p)" as is the same as simply "p".

--
Matma Rex