programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Rob Spoor
• Devaka Cooray
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Tim Moores
Bartenders:
• Mikalai Zaikin

# question on iteration

Ranch Hand
Posts: 83
• Number of slices to send:
Optional 'thank-you' note:
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

Ranch Hand
Posts: 117
• Number of slices to send:
Optional 'thank-you' note:
Hello Ricky

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

By this you will get the old value of y.

Regards
Shashikant

Ricky James
Ranch Hand
Posts: 83
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:

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
• Number of slices to send:
Optional 'thank-you' note:
Hi Ricky

Sorry please ignore my previous post.
Just try the following code

Regards
Shashikant

Ricky James
Ranch Hand
Posts: 83
• Number of slices to send:
Optional 'thank-you' note:

Originally posted by Shashikant Dahatonde:
Hi Ricky

Sorry please ignore my previous post.
Just try the following code

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

 Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters?