Please, help (GCD) greatest common divisor

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

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

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

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 :wink: ???
it’s a search engine and gives ~19.000.000 hits to your request.

hth
ralf

Best I could do on short notice:

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

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

Wayne B. 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” …

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

Ryan V. 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

On 27/02/2013, at 7:48 AM, Bartosz Dziewoński [email protected]
wrote:

Best I could do on short notice:

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

LOL

Henry

On Tue, Feb 26, 2013 at 9:39 PM, Henry M. [email protected]
wrote:

On 27/02/2013, at 7:48 AM, Bartosz Dziewoński [email protected] wrote:

Best I could do on short notice:

gcd=->(){.sort_by(&:[email protected]).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

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

On Tue, Feb 26, 2013 at 12:48 PM, Bartosz Dziewoński
[email protected] wrote:

Best I could do on short notice:

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


Matma R.

OMG this is gorgeous.

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

Wayne

On Wed, 27 Feb 2013 15:50:15 +0100, Wayne B. [email protected]
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.

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 :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”.

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 E.'s blog)
I saved this for later because it’s way over my head and he is writing
one twisted code:)
Thanks again.
Attila

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs