Forum: Ruby Please, help (GCD) greatest common divisor.

Posted by Caddy Tonks Lupin (caddytonks)
on 2013-02-26 19:28
Write a program to read two integers and show their greatest common
divisor.
Posted by Sam Duncan (Guest)
on 2013-02-26 19:36
(Received via mailing list)
On 02/27/2013 07:28 AM, Caddy Tonks Lupin wrote:
> Write a program to read two integers and show their greatest common
> divisor.
>
What have ya got so far?

Sam
Posted by Wayne Brisette (Guest)
on 2013-02-26 19:38
(Received via mailing list)
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
Posted by Ralf Mueller (Guest)
on 2013-02-26 19:42
(Received via mailing list)
On 02/26/2013 07:28 PM, Caddy Tonks Lupin wrote:
> Write a program to read two integers and show their greatest common
> divisor.
>
btw, have you heard of google ;-) ????
it's a search engine and gives ~19.000.000 hits to your request.

hth
ralf
Posted by Caddy Tonks Lupin (caddytonks)
on 2013-02-26 19:47
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" ...
Posted by Bartosz Dziewoński (matmarex)
on 2013-02-26 19:55
(Received via mailing list)
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)]
Posted by Ryan Victory (Guest)
on 2013-02-26 19:59
(Received via mailing list)
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
Posted by Caddy Tonks Lupin (caddytonks)
on 2013-02-26 20:03
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 by Wayne Brisette (Guest)
on 2013-02-26 20:23
(Received via mailing list)
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
Posted by Henry Maddocks (Guest)
on 2013-02-26 21:40
(Received via mailing list)
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

Henry
Posted by Robert Klemme (robert_k78)
on 2013-02-26 23:11
(Received via mailing list)
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

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
Posted by tamouse mailing lists (Guest)
on 2013-02-27 05:48
(Received via mailing list)
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
>

OMG this is gorgeous.
Posted by Attila Gulyas (toraritte)
on 2013-02-27 15:40
(Received via mailing list)
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:

https://gist.github.com/toraritte/5048189

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
Posted by Wayne Brisette (Guest)
on 2013-02-27 15:51
(Received via mailing list)
I'm still puzzled why you rewrite things when GCD is built-in. What am I
missing?

Wayne
Posted by Bartosz Dziewoński (matmarex)
on 2013-02-27 18:12
(Received via mailing list)
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?

My code was just an exercise in obfuscation.
Posted by Bartosz Dziewoński (matmarex)
on 2013-02-27 18:20
(Received via mailing list)
Replying to the gist.

> # 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 :)

"--p" is just for laughs. While this looks like decrementation in C / 
C++, it parses as "-(-p)" as is the same as simply "p".
Posted by Attila Gulyas (toraritte)
on 2013-02-27 19:04
(Received via mailing list)
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
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.