Go Job Hunting at the LQ Job Marketplace
 Home Forums HCL Reviews Tutorials Articles Register Search Today's Posts Mark Forums Read
 LinuxQuestions.org Greatest common Divisor (GCD)
 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

 10-14-2008, 10:06 AM #16 indienick Senior Member   Registered: Dec 2005 Location: London, ON, Canada Distribution: Arch, Ubuntu, Slackware, OpenBSD, FreeBSD Posts: 1,853 Rep: Not only did ErV provide you a solid algorithm, I actually wrote out a [rather inefficient] script for you! What's more, this is not a bad forum for a beginner, but this is a bad forum for a lazy beginner. You have shown absolutely no effort in your trials to get help, henceforth no one is providing much in the way of effort to help you.
 11-05-2008, 04:27 AM #17 bcahelper LQ Newbie   Registered: Nov 2008 Posts: 3 Rep: Simplest Solution There are several technique by which you can solve this. I found a simplest one. I know you are an IGNOU student, finding solution of your assignment.
 11-05-2008, 04:27 AM #18 bcahelper LQ Newbie   Registered: Nov 2008 Posts: 3 Rep: I found a easy understandable solution for you. You can check this. Code: ``` echo "Enter Number 1:-" read a echo "Enter Number 2:-" read b echo "Enter Number 3:-" read c gcd=1 if [ \$a -ge \$b ] then s=\$a else s=\$b fi if [ \$s -ge \$c ] then t=\$s else t=\$c fi i=1 while [ \$i -le \$t ] do d=`expr \$a % \$i` e=`expr \$b % \$i` f=`expr \$c % \$i` if [ \$d -eq 0 -a \$e -eq 0 -a \$f -eq 0 ] then gcd=\$i fi i=`expr \$i + 1` done echo "The GCD of \$a,\$b and \$c is \$gcd"``` This solution is taken from http://bcahelper.blogspot.com Hope this help you.
 08-15-2009, 05:41 AM #19 kvmreddy LQ Newbie   Registered: Aug 2009 Posts: 15 Rep: gcd of three numbers check bellow link to find gcd of more than two numbers http://bashscript.blogspot.com/2009/...o-numbers.html
 08-15-2009, 06:50 AM #20 Dan04 Member   Registered: Jun 2006 Location: Texas Distribution: Ubuntu Posts: 207 Rep: Here's one that works with any number of command-line arguments: Code: ```#!/bin/bash python -c 'print reduce((lambda a, b: max(i for i in xrange(1, min(a, b) + 1) if all(not any(("i" * n).split("i" * i)) for n in (a, b)))), map(int, __import__("sys").argv[1:]))' \$*```
08-15-2009, 07:00 AM   #21
Sergei Steshenko
Senior Member

Registered: May 2005
Posts: 4,481

Rep:
Quote:
 Originally Posted by Dan04 Here's one that works with any number of command-line arguments: Code: ```#!/bin/bash python -c 'print reduce((lambda a, b: max(i for i in xrange(1, min(a, b) + 1) if all(not any(("i" * n).split("i" * i)) for n in (a, b)))), map(int, __import__("sys").argv[1:]))' \$*```
And those people are telling us that Perl code is unreadable .

...

No, I'm not saying Perl one-liner would be more readable. But, still, in Perl it would just be '@ARGV', not "__import__("sys").argv".

08-15-2009, 07:20 AM   #22
Dan04
Member

Registered: Jun 2006
Location: Texas
Distribution: Ubuntu
Posts: 207

Rep:

Quote:
 Originally Posted by Sergei Steshenko And those people are telling us that Perl code is unreadable . ... No, I'm not saying Perl one-liner would be more readable. But, still, in Perl it would just be '@ARGV', not "__import__("sys").argv".
Yeah, I really think sys.argv should have been a builtin. If I didn't care about having a true one-liner, I'd do:

Code:
```from sys import *; print reduce((lambda a, b: max(i for i in xrange(1,
min(a, b) + 1) if all(not any(("i" * n).split("i" * i)) for n in (a,
b)))), map(int, argv[1:]))```
Obviously with "import *" instead of just "import argv" so I get a massive number of "Unused import" warnings (and a score of -270) in pylint.

But what do you think of my touch of using str.split to factor an integer? I tried to do it with a regular expression, but couldn't get it to work right.

 Tags script, shell