473,406 Members | 2,549 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Euclid's Method

lotus18
866 512MB
Hello experts

The Euclid's Method:

If u is greater then v then the greatest common divisor of u and v is the sum as the greatest common divisor of v and u-v.

Now my question is, how can I apply this method in getting the right answer for the following inputs:

Inputs:

Numerator: 4
Denominator: 6

The result must be:

4/6 = 2/3

Even a simple program will do. Please help. Thanks.
Nov 17 '07 #1
11 3390
oler1s
671 Expert 512MB
So, what have you tried? Where's your code? We aren''t going to do your homework for you.
Nov 17 '07 #2
lotus18
866 512MB
Here's the code:

Expand|Select|Wrap|Line Numbers
  1. gcd(u, v) = gcd(v, u mod v) 
  2. // calculate gcd
  3. template <int u, int v>
  4. struct gcd
  5. {
  6.     enum { value = gcd<v, u % v>::value };
  7. };
  8.  
  9. template <int u>
  10. struct gcd<u, 0>
  11. {
  12.     enum { value = u };
  13. };
  14.  
  15. template <>
  16. struct gcd<0, 0>
  17.  
  18. {
  19.     enum { value = -1 };
  20. };
  21.  
Nov 17 '07 #3
oler1s
671 Expert 512MB
Right, so I see the work you have done. Oh wait, something isn't right here. It looks like you got it from: here.

We can see your utter lack of interest and effort, and moreover, from our mentality and experience, we can spot homework questions and copied answers. We don't spend time on the forums doing other people's homework.

It's also against the forum rules. Here it's explicit. Other forums may be more implicit in this rule. Either way, you're on your own.
Nov 17 '07 #4
lotus18
866 512MB
Right, so I see the work you have done. Oh wait, something isn't right here. It looks like you got it from: here.

We can see your utter lack of interest and effort, and moreover, from our mentality and experience, we can spot homework questions and copied answers. We don't spend time on the forums doing other people's homework.

It's also against the forum rules. Here it's explicit. Other forums may be more implicit in this rule. Either way, you're on your own.
Yes you're right. I only have a little knowledge in c++ and I have no time to understand and learn it by myself. Could you explain to me how do these codes work?
Nov 18 '07 #5
oler1s
671 Expert 512MB
I could, but I won't. Neither will other professionals. We come here to help people who genuinely want to learn. Not assist you in getting by your assignment. Sorry, but you have no sympathy from us.
Nov 18 '07 #6
lotus18
866 512MB
I could, but I won't. Neither will other professionals. We come here to help people who genuinely want to learn. Not assist you in getting by your assignment. Sorry, but you have no sympathy from us.
Sorry sir

OK, I'll do my assignment with my own. Can you help me If I try to build a code with my own? Thanks for your remarkable reminder. Don't worry sir I'll try to learn this language and I assure you that :)

Thanks for reply sir

Regards

Rey Sean
Nov 18 '07 #7
lotus18
866 512MB
Hi oler1s

I came up with these codes with the help of my friend

Expand|Select|Wrap|Line Numbers
  1. #include<iostream.h>
  2. void main()
  3.  
  4.  
  5.     {
  6.         int numerator, denominator, temp, tempnum, tempden, final1, final2;
  7.         cout<<"This program demostrates how to get the Greatest Common Denominator (GCD)\n";
  8.         cout<<"\n\n";
  9.         cout<<"Enter first number (numerator): ";
  10.         cin>>numerator;
  11.         cout<<"Enter second number (denominator): ";
  12.         cin>>denominator;
  13.  
  14.  
  15.         if(numerator<denominator)
  16.         { 
  17.             temp=numerator;
  18.             tempnum=numerator; //temporary storage for numerator
  19.             numerator=denominator; 
  20.             tempden=denominator; //temporary storage for denominator
  21.             denominator=temp;
  22.         //}
  23.         //else
  24.         //{
  25.         //    final1=numerator/denominator;
  26.         //    cout<<final1;
  27.         //}
  28.         while(denominator!=0)
  29.         {
  30.             temp=numerator%denominator;
  31.             numerator=denominator;
  32.             denominator=temp;
  33.         }
  34.         cout<<endl;
  35.         cout<<"The GCD is:"<<numerator<<endl;
  36.         final1=tempnum/numerator;
  37.         final2=tempden/numerator;
  38.         cout<<"The numerator is: "<<final1<<endl;
  39.         cout<<"The denominator is: "<<final2;
  40.         cout<<endl;
  41.         cout<<endl;
  42.         cout<<tempnum<<"/"<<tempden<<" = "<<final1<<"/"<<final2<<"\n\n";
  43.         }
  44.         else
  45.         {
  46.             //final1=numerator/denominator;
  47.             //cout<<final1<<endl;
  48.             temp=denominator%numerator;
  49.             cout<<temp<<endl;
  50.         }
  51.     }
  52.  
  53.  
Now, my problem is... how can I get the answer if the numerator is greater than denominator? For instance, 6/4 = 1 1/2. Any algorithm please. Thank you in advance.
Nov 18 '07 #8
oler1s
671 Expert 512MB
I'm not entirely sure you understand Euclid's method. It isn't a method of division. It's a way of finding an integral divisor that is common to two numbers. The GCD to be precise. It does not involve division. Only taking the remainder over and over.

This isn't a numerator/denominator issue. There is no fraction here. There is no straight division. You aren't dividing the numbers. And if your answer is an integer greater or equal to 1, then it obviously is wrong.

So I suggest you take a look at the Euclid's algorithm again. And understand it.
Nov 18 '07 #9
oler1s
671 Expert 512MB
Oh, I see what you're trying to do. But it's still messy, and complicated, and I'm not sure what is going on. I think the idea is to reduce a fraction to it's simplified form (i.e. reduce the numbers). So if you have a numerator of 6 and denominator of 4, the gcd is 2 and 6/2 = 3 and 4/2 = 2.

Euclid's algorithm doesn't care about the relative magnitude of the two numbers. If for gcd(a,b), a>b or b>a, it doesn't matter.

The only if/else statement you need is to make sure both a and b are greater than 0. Once you do that, just run euclid's algorithm to get a gcd, and divide the numerator and denominator by the gcd to get the simplified fraction. The GCD is obviously going to be equal to or smaller than the smaller of both numbers. So you don't have to write messy code that deals with numerator > denominator, and denominator > numerator.
Nov 18 '07 #10
lotus18
866 512MB
OK I'll do it. Thanks for your reply : )
Nov 18 '07 #11
lotus18
866 512MB
I'd just scanned the euclid's algorithm without understanding it. All I thought that euclid's algorithm is only used for reducing fraction to its simplest form and it doesn't care about for gcd(a,b), a>b or b>a.

Here's my latest codes yes it's still messy. I want to create a function but I don't know how to do it right now.
Expand|Select|Wrap|Line Numbers
  1. #include<iostream.h>
  2. void main()
  3.  
  4.  
  5.     {
  6.         int numerator, denominator, temp, tempnum, tempden, final1, final2;
  7.         cout<<"This program demostrates how to get the Greatest Common Denominator (GCD)\n";
  8.         cout<<"\n\n";
  9.  
  10.         rey:
  11.             cout<<"Enter first number (numerator): ";
  12.             cin>>numerator;
  13.             cout<<"Enter second number (denominator): ";
  14.             cin>>denominator;
  15.  
  16.             if (numerator==0||denominator==0)
  17.             {
  18.                 cout<<"The numerator or the denominator cannot be zero value.\n\n";
  19.                 goto rey;
  20.             }
  21.             else
  22.             {
  23.                 if(numerator<denominator)
  24.                 { 
  25.                     temp=numerator;
  26.                     tempnum=numerator; //temporary storage for numerator
  27.                     numerator=denominator; 
  28.                     tempden=denominator; //temporary storage for denominator
  29.                     denominator=temp;
  30.                 while(denominator!=0)
  31.                 {
  32.                     temp=numerator%denominator;
  33.                     numerator=denominator;
  34.                     denominator=temp;
  35.  
  36.                 }
  37.                 cout<<endl;
  38.                 cout<<"The GCD is:"<<numerator<<endl;
  39.                 final1=tempnum/numerator;
  40.                 final2=tempden/numerator;
  41.                 cout<<"The numerator is: "<<final1<<endl;
  42.                 cout<<"The denominator is: "<<final2;
  43.                 cout<<endl;
  44.                 cout<<endl;
  45.                 cout<<tempnum<<"/"<<tempden<<" = "<<final1<<"/"<<final2<<"\n\n";
  46.                 }
  47.             }
  48.         }
  49.  
Nov 19 '07 #12

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

Similar topics

11
by: Dave Rahardja | last post by:
OK, so I've gotten into a philosophical disagreement with my colleague at work. He is a proponent of the Template Method pattern, i.e.: class foo { public: void bar() { do_bar(); } protected:...
5
by: Chris | last post by:
Hi I have a scenario where I've created another AppDomain to dynamically load a DLL(s) into. In this newly loaded DLL I want to call a static method on a class. The problem arise is that I have...
4
by: daniel.w.gelder | last post by:
I wrote a template class that takes a function prototype and lets you store and call a C-level function, like this: inline string SampleFunction(int, bool) {..} functor<string (int, bool)>...
7
by: greenflame | last post by:
I am trying to make a matrix object. I have given it some properites. I am trying to add a method. When I call the method by Test.showDims(...) I want to only enter one input, that is the method by...
12
by: Erik the Red | last post by:
In Fundamental Algorithms (The Art of Computer Programming), the first algorithm discussed is Euclid's Algorithm. The only idea I have of writing this in python is that it must involve usage of...
5
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS...
18
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the...
10
by: Mihai Osian | last post by:
Hi everyone, Given the code below, can anyone tell me: a) Is this normal behaviour ? b) If it is, what is the reason behind it ? I would expect the A::method(int) to be inherited by B. ...
9
by: Steve Richter | last post by:
in a generic class, can I code the class so that I can call a static method of the generic class T? In the ConvertFrom method of the generic TypeConvert class I want to write, I have a call to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.