Data Structures and Algorithms Discussion Board

Data Structures, Algorithms, Java and C++ => Java => Topic started by: zaini on March 17, 2009, 08:22:30 AM



Title: iterative, stack based method [Urgent]
Post by: zaini on March 17, 2009, 08:22:30 AM
Given two positive integers i and j, the greatest common divisor of i and j written gcd
(i, j) is the largest integer k such that (i % k = 0) and (j % k = 0).
Develop an iterative, stack based method that returns the greatest common divisor of
i and j.
Here is the method specification:
/**
* Finds the greatest common divisor of two given positive integers
*
* @param i – one of the given positive integers.
* @param j – the other given positive integer.
*
* @return the greatest common divisor of iand j.
*
* @throws IllegalArgumentException – if either i or j is not a positive integer.
*
*/
public static int gcd (int i, int j)
Test your method with a main method that reads in two positive integers and outputs their greatest common divisor.
Sample Input – Output:
Please enter two positive integers, separated by a space (or *** to quit): 20 40
The greatest common divisor of 20 and 40 is 20.
Please enter two positive integers, separated by a space (or *** to quit): ***



help me..... and 10Q


Title: Re: iterative, stack based method [Urgent]
Post by: Denis on March 17, 2009, 10:00:52 AM
What have you done yet? It's better to start with writing GCD algorithm using recursion (http://simpleprogrammingtutorials.com/tutorials/concepts/recursion.php). Then it can be transormed into a stack-based iteration.


Title: Re: iterative, stack based method [Urgent]
Post by: suguneswari on March 25, 2009, 12:33:47 PM
Hi Zaini..

Better you do it your self than begging for others help!!
Don't be so selfish, you will not learn anything
See me at my room at 19th floor this thursday. :(



Title: Re: iterative, stack based method [Urgent]
Post by: izzammil on March 25, 2009, 08:43:46 PM
hi zaini
would u help me to solve the stack program that u have post..
can u give me the program so that i can check it with my own program..
because i have the problem with it..
thanks..