From 3bd769d2ae947299d09b77170c2f8f6fffbe9966 Mon Sep 17 00:00:00 2001 From: teerapat1739 Date: Sat, 28 Oct 2017 01:19:55 +0700 Subject: [PATCH 1/3] Increase recursive GCD --- Others/GCD.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Others/GCD.java b/Others/GCD.java index dba60c6899c1..94f443b09252 100644 --- a/Others/GCD.java +++ b/Others/GCD.java @@ -13,3 +13,12 @@ public static int gcd(int a, int b) { return b; } } + +//Increase the number of calculations. +//Use functoin from above as recursive. +public static int gcd(int[] number) { + int result = number[0]; + for(int i = 1; i < number.length; i++) + result = gcd(result, number[i]); + return result; +} From cc6af5f59e416eb969959734d04d8e545b829e12 Mon Sep 17 00:00:00 2001 From: teerapat1739 Date: Sat, 28 Oct 2017 02:43:04 +0700 Subject: [PATCH 2/3] create main function GCD output 4 main --- Others/GCD.java | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/Others/GCD.java b/Others/GCD.java index 94f443b09252..21efb81d87f6 100644 --- a/Others/GCD.java +++ b/Others/GCD.java @@ -3,7 +3,7 @@ public class GCD{ -public static int gcd(int a, int b) { + public static int gcd(int a, int b) { int r = a % b; while (r != 0) { @@ -12,13 +12,16 @@ public static int gcd(int a, int b) { } return b; } -} + public static int gcd(int[] number) { + int result = number[0]; + for(int i = 1; i < number.length; i++) + result = gcd(result, number[i]); + + return result; + } -//Increase the number of calculations. -//Use functoin from above as recursive. -public static int gcd(int[] number) { - int result = number[0]; - for(int i = 1; i < number.length; i++) - result = gcd(result, number[i]); - return result; + public static void main(String[] args) { + int[] myIntArray = {4,16,32}; + System.out.println(gcd(myIntArray)); + } } From 205698d90fd254d8f2ab3c33706dd41dc2288fd8 Mon Sep 17 00:00:00 2001 From: teerapat1739 Date: Sat, 28 Oct 2017 03:39:09 +0700 Subject: [PATCH 3/3] Update GCD.java --- Others/GCD.java | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/Others/GCD.java b/Others/GCD.java index 21efb81d87f6..08da3805e9d8 100644 --- a/Others/GCD.java +++ b/Others/GCD.java @@ -1,20 +1,22 @@ //Oskar Enmalm 3/10/17 //This is Euclid's algorithm which is used to find the greatest common denominator +//Overide function name gcd public class GCD{ - public static int gcd(int a, int b) { - - int r = a % b; - while (r != 0) { - b = r; - r = b % r; + public static int gcd(int num1, int num2) { + + int gcdValue = num1 % num2; + while (gcdValue != 0) { + num2 = gcdValue; + gcdValue = num2 % gcdValue; } - return b; + return num2; } public static int gcd(int[] number) { int result = number[0]; for(int i = 1; i < number.length; i++) + //call gcd function (input two value) result = gcd(result, number[i]); return result; @@ -22,6 +24,7 @@ public static int gcd(int[] number) { public static void main(String[] args) { int[] myIntArray = {4,16,32}; + //call gcd function (input array) System.out.println(gcd(myIntArray)); } } pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy