posted 10 years ago
Hello,
I am practicing iteration from a book and it has the following ex:
The greatest common factor (GCF) of two numbers is the largest number that evenly divides both numbers. Using Euclid�s method, create two codes for computing the GCF of two numbers. Use iteration for the first code and recursion for the other one.
Notes on Euclid�s algorithm:
1. Get input integers x and y.
2. Repeat the following step while y != 0
a. y = x % y;
b. x = old value of y
3. Return x.
For example, x = 14 and y = 6.
y = x % y = 14 % 6 = 2
x = 6
y = x % y = 6 % 2 = 0
x = 2 (GCF)
I tried this code with iteration and I am pasting it here:
public class ExerciseOne {
static int GCF(int x, int y){
while (y != 0){
y = x % y;
x = y;
}
return x;
}
public static void main(String args[]){
System.out.println(GCF(14,6));
}
}

Now I know the answer to this exercise is 2 and I am doing something fundamentally wrong. Can anyone please point out what is the problem with my code?
I can see that x doesn't get the 'old value' of y. But I don't know how I can do it.
Shall be extremely grateful.
Thanks
Ricky
I am practicing iteration from a book and it has the following ex:
The greatest common factor (GCF) of two numbers is the largest number that evenly divides both numbers. Using Euclid�s method, create two codes for computing the GCF of two numbers. Use iteration for the first code and recursion for the other one.
Notes on Euclid�s algorithm:
1. Get input integers x and y.
2. Repeat the following step while y != 0
a. y = x % y;
b. x = old value of y
3. Return x.
For example, x = 14 and y = 6.
y = x % y = 14 % 6 = 2
x = 6
y = x % y = 6 % 2 = 0
x = 2 (GCF)
I tried this code with iteration and I am pasting it here:
public class ExerciseOne {
static int GCF(int x, int y){
while (y != 0){
y = x % y;
x = y;
}
return x;
}
public static void main(String args[]){
System.out.println(GCF(14,6));
}
}

Now I know the answer to this exercise is 2 and I am doing something fundamentally wrong. Can anyone please point out what is the problem with my code?
I can see that x doesn't get the 'old value' of y. But I don't know how I can do it.
Shall be extremely grateful.
Thanks
Ricky
Ricky James
Ranch Hand
Posts: 83
posted 10 years ago
Hello Shashikant,
Thanks for the reply. I tried what you advised but even that gives me an error.
When I assign the value of y into x before the statement y = x % y I get the answer 6 which is the value of y so that means that the new value never gets assigned, and x takes the original value of y.
Ricky
Thanks for the reply. I tried what you advised but even that gives me an error.
When I assign the value of y into x before the statement y = x % y I get the answer 6 which is the value of y so that means that the new value never gets assigned, and x takes the original value of y.
Ricky
Shashi Kant
Ranch Hand
Posts: 117
Ricky James
Ranch Hand
Posts: 83
posted 10 years ago
Hi Shashikant,
I am trying to follow the requirements of the exercise. And they state the pseudocode that is to be followed.
I know this can be done using recursion as well and that is the next step of the exercise. But first I want to do the way the exercise suggests.
Thanks
Ricky
Originally posted by Shashikant Dahatonde:
Hello Ricky
Instead of returning the value of x why can't you return value of y directly.
Regards
Shashikant
Hi Shashikant,
I am trying to follow the requirements of the exercise. And they state the pseudocode that is to be followed.
I know this can be done using recursion as well and that is the next step of the exercise. But first I want to do the way the exercise suggests.
Thanks
Ricky
Shashi Kant
Ranch Hand
Posts: 117
Ricky James
Ranch Hand
Posts: 83
posted 10 years ago
Perfect!
It worked. So basically we store the value in a 'temp' variable and then assign that value into x.
Brilliant.
Thanks
Ricky
Originally posted by Shashikant Dahatonde:
Hi Ricky
Sorry please ignore my previous post.
Just try the following code
Hope this will help you
Regards
Shashikant
Perfect!
It worked. So basically we store the value in a 'temp' variable and then assign that value into x.
Brilliant.
Thanks
Ricky
Are you okay? You look a little big. Maybe this tiny ad will help:
The WEB SERVICES and JAXRS Course
https://coderanch.com/t/690789/WEBSERVICESJAXRS
