From ac41f7286588e21b63563151206d0e10ce655ac8 Mon Sep 17 00:00:00 2001 From: Igor Pashev Date: Mon, 7 Nov 2022 18:22:42 +0200 Subject: Update Java --- gcd.java | 53 +++++++++++++++++++++++++---------------------------- 1 file changed, 25 insertions(+), 28 deletions(-) diff --git a/gcd.java b/gcd.java index 82722a1..8770beb 100644 --- a/gcd.java +++ b/gcd.java @@ -1,41 +1,38 @@ /* * SYNOPSIS: * - * # gcj -o gcd --main=gcd gcd.java - * # ./gcd 11 22 33 44 121 - * * # javac gcd.java * # java gcd 11 22 33 * */ -public class gcd -{ - public static int gcd2(int a, int b) { - int c; - while (b != 0) { - c = b; - b = a % b; - a = c; - } - return a; +public class gcd { + public static long gcd2(long a, long b) { + long c; + while (b != 0) { + c = b; + b = a % b; + a = c; } + return a; + } - public static int gcdn(int [] a) { - int r = a[0]; - for (int i = 1; i < a.length; i++) - r = gcd2(r, a[i]); - return r; - } + public static long gcdn(long[] a) { + long r = a[0]; + for (int i = 1; i < a.length; i++) + r = gcd2(r, a[i]); + return r; + } + + public static void main(String[] argv) { + if (argv.length == 0) + return; - public static void main(String [] argv) { - if (argv.length == 0) - return; - int [] n = new int [argv.length]; - for (int i = 0; i < argv.length; i++) { - n[i] = Integer.parseInt(argv[i]); - } - System.out.println(gcdn(n)); + long[] n = new long[argv.length]; + for (int i = 0; i < argv.length; i++) { + n[i] = Long.parseLong(argv[i]); } -} + System.out.println(gcdn(n)); + } +} -- cgit v1.2.3