LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Tags used in this thread
Popular LQ Tags , ,

Reply
 
Thread Tools
Old 10-14-2008, 10:06 AM   #16
indienick
Senior Member
 
Registered: Dec 2005
Location: London, ON, Canada
Distribution: Debian, Ubuntu, OpenBSD, OpenSolaris
Posts: 1,725
Thanked: 26

[Log in to get rid of this advertisement]
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.
indienick is offline     Reply With Quote
Old 11-05-2008, 04:27 AM   #17
bcahelper
LQ Newbie
 
Registered: Nov 2008
Posts: 3
Thanked: 0
Thumbs up 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.
bcahelper is offline     Reply With Quote
Old 11-05-2008, 04:27 AM   #18
bcahelper
LQ Newbie
 
Registered: Nov 2008
Posts: 3
Thanked: 0
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.
bcahelper is offline  
Tag This Post , ,
Reply With Quote
Old 08-15-2009, 05:41 AM   #19
kvmreddy
LQ Newbie
 
Registered: Aug 2009
Posts: 5
Thanked: 0
gcd of three numbers

check bellow link to find gcd of more than two numbers
http://bashscript.blogspot.com/2009/...o-numbers.html
kvmreddy is offline     Reply With Quote
Old 08-15-2009, 06:50 AM   #20
Dan04
Member
 
Registered: Jun 2006
Location: Texas
Distribution: Ubuntu
Posts: 160
Thanked: 4
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:]))' $*
Dan04 is offline     Reply With Quote
Old 08-15-2009, 07:00 AM   #21
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 1,036
Thanked: 50
Quote:
Originally Posted by Dan04 View Post
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".
Sergei Steshenko is offline     Reply With Quote
Old 08-15-2009, 07:20 AM   #22
Dan04
Member
 
Registered: Jun 2006
Location: Texas
Distribution: Ubuntu
Posts: 160
Thanked: 4
Smile

Quote:
Originally Posted by Sergei Steshenko View Post
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.
Dan04 is offline     Reply With Quote

Reply

Bookmarks


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What is the greatest software ever ? marozsas Linux - News 1 08-20-2006 09:37 AM
BOGUS.common.04y -> /home/common/Mailbox jayakrishnan Linux - Networking 0 11-19-2005 05:48 AM
the greatest site thanko LQ Suggestions & Feedback 3 12-28-2004 10:16 AM
GCD Algo. Help dontcare Programming 2 10-16-2004 10:50 PM
Isn't Linux the greatest! perry General 8 06-12-2004 04:26 AM


All times are GMT -5. The time now is 09:17 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration