ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
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.
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"
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:]))' $*
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".
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.