473,804 Members | 2,164 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Greatest Common Divisor problem

7 New Member
this is my gcd prog..

#include<iostre am>
using namespace std;
int GCD(int ,int );
void main()
{
int x,y;
cout<<"Plz enter the two numbers : ";
cin>>x>>y;
cout<<"The GCD("<<x<<","<< y<<") = " << GCD(x,y)<<endl;
}
int GCD(int x,int y)
{
if(y>x)return GCD(y,x);
if(x==y)return x;
if(x%y==0)retur n y;
return GCD(x,x-y);

}


if i enter numbers like 13 17..the prog gets stuck..so now how to solve that error????
Mar 5 '09 #1
5 2559
donbock
2,426 Recognized Expert Top Contributor
@tejas2991
Let's walk through the sequence of recursive GCD calls:
Expand|Select|Wrap|Line Numbers
  1. GCD(13,17)    ; operator input
  2. GCD(17,13)    ; because (y>x)
  3. GCD(17,4)     ; fall-through
  4. GCD(17,13)    ; fall-through
  5. ...           ; uh-oh!
Can you express your GCD algorithm mathematically?
Mar 5 '09 #2
JosAH
11,448 Recognized Expert MVP
I moved the two posts above to a separate thread; the question has nothing to do with the thread it came from.

kind regards,

Jos (moderator)
Mar 6 '09 #3
tejas2991
7 New Member
hey i found the solution!!!!

instead of all those if case :

if(y==0)
return y;

return gcd(y,x%y);
Mar 6 '09 #4
donbock
2,426 Recognized Expert Top Contributor
@tejas2991
That can't be right -- that function returns "0" for all values of x and y.
Mar 6 '09 #5
whodgson
542 Contributor
Expand|Select|Wrap|Line Numbers
  1. int GCD(int x,int y) 
  2.     if(y>x) return GCD(y,x); 
  3.     if(x==y) return x; 
  4.     if(x%y==0) return y; 
  5.     return GCD(x,x-y); 
Expand|Select|Wrap|Line Numbers
  1. long gcd(long m,long n)
  2. {
  3.      if(m>n)swap(m,n);
  4.      assert(n>0);
  5.      while(n>0)
  6.      {
  7.       long r=m%n;
  8.       m=n;
  9.       n=r;
  10.      }
  11.      return m;
  12. }     
This returns 1 as it should
Line 3 in the quoted function looks wrong -- but I don`t know why.
Mar 7 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

8
7753
by: Mike Nolan | last post by:
As far as I can tell, Postgres has no equivalent to greatest and least functions in Oracle. Yes, you can do the same thing with a case statement, but at the expense of writing MUCH longer SQL statements. Is this something that is on or can be added to the 'to do' list? I could write a series of user-defined functions to do specific comparisons (such as comparing several dates and returning the greatest one) but there doesn't appear to...
7
4182
by: Mathew Butler | last post by:
Suppose I have a table t with columns id, col1, col2, col3, col4, col5, col6 all numeric. I want to query the table and for each value of col<x> in the resultset I want to identify the largest value in each of the columns. For example, with the following dataset: id, col1, col2, col3, col4, col5, col6 1,2,3,4,5,6,7 2,8,6,5,4,3,2 3,2,3,9,4,2,1 I want a query to generate the following output: 1,7
21
2085
by: Frederick Gotham | last post by:
I'm trying to devise a compile-time constant for X, where X is the greatest number which satisfies both the following criteria: (1) X <= DESIGNATED_MAX_VALUE (2) X % Y == 0 I'll try to explain with example code:
7
1767
by: cess | last post by:
Hi!!! i would like to know if what is lacking in the codes below to have a greatest common denominator of two given(by the user) numbers?? I'm confused and i need your help! import java.io.*; public class gcd { public static void main (String args)throws Exception { String number;
4
13700
by: sdlt85 | last post by:
Hi, Can someone help me with an idea on how to start writing a C++ code for generating greatest common divisor and the linear combination of two intergers represented as gcd(m, n)= mx + ny and adding them it will give us the greatest common divisor and I need to use the extended Euclidean algorithm. I already have the code for the greatest common divisor but I dont know how to do the linear combination. This is what I have:
15
3685
by: judge82 | last post by:
I'm new to Java and I was looking for some help on this particular problem. I need to create a method that can simplify a fraction, that is, represent it as a fraction where the numerator and the denominator have no common divisors. it should read in the numerator and the denominator and then print out the fraction in its simplified form. I tried to calculate it with: while n!=0 { r = m mod n; m = n; n = r;
2
4180
by: 7asco | last post by:
i want the code of greater common divisor algorithm in c++
3
5698
by: stressedstudent | last post by:
I dont know where I am going wrong so I dont know which part to post, this is what I have, can anyone help me figure out where I am going wrong? THanks for any and all help. // into to c++ // This program should use a loop that lets the user enter a series of integers. // The user should enter -99 to signal the end of the series. // After all the numbers have been entered, // the program should display the largest and smallest numbers...
35
3668
by: aarklon | last post by:
Hi all, The following question is asked frequently in interviews How to find the greatest of 2 numbers without using relational operators ? the solution i have seen is ( a+b + abs(a-b) ) /2 ;
4
3475
by: jmf777 | last post by:
Hi I'm trying to write a program to find the greatest common divisor of 2 numbers input by the user. This is what I got so far: #include <stdio.h> int gcd(int a, int b); int main(void) {
0
9714
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10350
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10096
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7638
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6866
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5534
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4311
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3002
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.