Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

question on iteration

 
Ricky James
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Shashi Kant
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Ricky

In your code


you are assigning the value of y to x after changing the orginal value of y
i.e.

So modify your code like
By this you will get the old value of y.

Regards
Shashikant
 
Ricky James
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Shashi Kant
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Ricky

Instead of returning the value of x why can't you return value of y directly.

Regards
Shashikant
 
Ricky James
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ricky

Sorry please ignore my previous post.
Just try the following code
Hope this will help you




Regards
Shashikant
 
Ricky James
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic