Forum     

Go Back   Digit Technology Discussion Forum > Software > Programming
Register FAQ Calendar Mark Forums Read

Programming The destination for developers - C, C++, Java, Python and the lot


Closed Thread
 
LinkBack Thread Tools Display Modes
Old 19-12-2007, 09:23 PM   #1 (permalink)
Right Off the Assembly Line
 
Join Date: Jun 2007
Posts: 23
Default GCD of 3 numbers


Quote:
//Find GCD of three numbers
#include<iostream.h>
#include<conio.h>

void main()
{
int a,b,c,i,j,m;
cout<<"Enter the values";
cin>>a>>b>>c;
if(a>b)
{
if(a>c)
{
m=a;
}
else
{
m=c;
}
}
else
{
if(b>c)
{
m=b;
}
else
{
m=c;
}
}
j=0;
for(i=m;i>=2;i--)
{
if(a%i==0 && b%i==0 && c%i==0)
{
j++;
break;
}
}
if(j==0)
cout<<"GCD is 1";
else
cout<<"GCD is"<<i;
getch();
}
whats wrong in this code?? My teacher says logic I have used is wrong
kg_87 is offline  
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 19-12-2007, 10:03 PM   #2 (permalink)
Commander in Chief
 
QwertyManiac's Avatar
 
Join Date: Jul 2005
Posts: 6,658
Default Re: GCD of 3 numbers

You should use the Euclidean algorithm to find GCD, its much easier and faster that way. Since you would use the remainders got by dividing one number by another and find the GCD (final remaining remainder).

Here's the implementation in Standard C++ code:
Code:
#include<iostream>

using namespace std;

int gcd(int a, int b)
{
    int t;
    
    while(b!=0)
    {
        t = b;
        b = a%b; // B's now the remainder of A/B.
        a = t;
    } // And we go to the next iteration.
    
    return a;
}

int main()
{
    int a,b,c,g;
    cout<<"Enter 3 numbers: ";
    cin>>a>>b>>c;
    g=gcd(gcd(a,b),c); // Comm./Asso. laws...
    cout<<"The GCD of the 3 numbers is: "<<g<<endl;
    return 0;
}
Your way takes some extra number crunching and thus its way slow for large numbers.

Apart from that, it sure is correct in its answer. Your teacher is probably acting oversmart.

Lets try an example of 12121212, 18181818, 24242424

Time taken
Code:
bigbang@pysnakums:~$ time ./yours
GCD is
6060606

real    0m0.271s
user    0m0.272s
sys     0m0.000s

bigbang@pysnakums:~$ time ./euclid
The GCD of the 3 numbers is: 6060606

real    0m0.003s
user    0m0.004s
sys     0m0.000s
__________________
Harsh J
www.harshj.com

Last edited by QwertyManiac; 19-12-2007 at 10:22 PM.
QwertyManiac is offline  
Old 20-12-2007, 01:08 PM   #3 (permalink)
Right Off the Assembly Line
 
Join Date: Jun 2007
Posts: 23
Default Re: GCD of 3 numbers

Thanks
kg_87 is offline  
Closed Thread

Bookmarks

Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Game of Numbers vish786 Chit-Chat 3 26-11-2007 01:23 AM
serial numbers of win xp sp2 pravesh_4766 Software Q&A 3 30-04-2006 08:32 PM
Usb pin numbers beyondthegracefgod Peripherals 5 07-11-2004 02:51 PM

 
Latest Threads
- by Charan
- by Sarath
- by clmlbx

Advertisement




All times are GMT +5.5. The time now is 12:46 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2