Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MathUtils {
- public static Answer greatestCommonDivisor(double one, double two) {
- double three = 0;
- int steps = 0;
- //with while we can actually assign and compare in one step which greatly simplifies things
- while ((three = one % two) != 0) {
- /* variable reassignment occurs here. While three does not equal 0 One is assigned the value of Two,
- * Two is assigned the value of Three then modulo occurs again to see if Three equals 0
- */
- one = two;
- two = three;
- steps++;
- }
- return new MathUtils.Answer(two, steps);
- }
- //create an inner class so we can return both Answer and amount of steps cleanly
- static class Answer {
- private double answer=-1;
- private int steps=-1;
- Answer(double answer, int steps) {
- this.answer = answer;
- this.steps = steps;
- }
- public double getAnswer() {
- return answer;
- }
- public void setAnswer(double answer) {
- this.answer = answer;
- }
- public int getSteps() {
- return steps;
- }
- public void setSteps(int steps) {
- this.steps = steps;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement