Java greatest common divisor (decomposition factorization)

Source: Internet
Author: User
Tags gcd greatest common divisor

Here are four ways to find greatest common divisor that are implemented in the Java language:

Package Gcd;import Java.util.arraylist;import Java.util.list;public class GCD {public static void main (string[] args) {lo ng Starttime;long Endtime;long durationtime;int[] testArray1 = new int[]{784, 988, 460, 732, 548, 998, 672, 1024, 888, 512 };int[] TestArray2 = new int[]{1024, 0, ten,, +, +, 582, 788};for (int i = +, < i++) {StartTime = System.nanotime (); System.out.println ("Euclidean method:" + Euclid (Testarray1[i],testarray2[i])); endTime = System.nanotime ();d urationtime = Endtime-starttime; System.out.println ("Euclidean algorithm time-consuming:" + durationtime + "\ n"); startTime = System.nanotime (); SYSTEM.OUT.PRINTLN ("Continuous Integer Detection method:" + consecutiveintegerstest (Testarray1[i], testarray2[i])); endTime = System.nanotime (); Durationtime = Endtime-starttime; System.out.println ("Sequential integer Detection Algorithm time:" + durationtime + "\ n"); startTime = System.nanotime (); System.out.println ("Tossing and subtracting:" + consecutivesubstract (testarray1[i), testarray2[i]); endTime = System.nanotime (); Durationtime = Endtime-starttime; System.out.println ("Subtraction Algorithm time:" + durationtime + "\ n"); startTime = System.nanotime (); System.out.println ("Decomposition factorization method:" + primefactors (Testarray1[i], testarray2[i])); endTime = System.nanotime ();d urationtime = Endtime-starttime; System.out.println ("Decomposition Factorization algorithm time:" + Durationtime);}} /** * Euclidean algorithm for greatest common divisor * @param no1 * @param NO2 * @return */public static int Euclid (int no1, int no2) {int Remainder;remaind ER = no1%no2;while (remainder! = 0) {no1 = No2;no2 = Remainder;remainder = No1%no2;} return NO2;} /** * Continuous Integer detection method * @param m * @param n * @return */public static int consecutiveintegerstest (int m, int n) {int t;if (M > N) t = N;elset = M;while (True) {if (m%t = = 0 && n%t = = 0) Break;elset = t-1;} return t;} /** * Subtraction * @param num1 * @param num2 * @return */public static int consecutivesubstract (int num1, int num2) {while (true {if (Num1 > num2) num1-= Num2;else if (Num1 < num2) num2-= Num1;elsereturn num1;}} /** * Decomposition Factorization method * @param primeNum1 * @param primeNum2 * @return */public static int primefactors (int prIMENUM1, int primeNum2) {int prime_gcd = 1;int comparelistsize;int temp1, temp2;int pn1 = primeNum1, pn2 = primeNum2; list<integer> num1list = new arraylist<integer> (); list<integer> num2list = new arraylist<integer> (); list<integer> samenumlist = new arraylist<integer> ();//Find factorization for (int i = 2; i < PN1/2;) {//Note here is PN1, instead of the PRIMENUM1,PRIMENUM1 value in the following execution process will continue to decrease if (primenum1%i = = 0) {//for remainder, if divisible, return TRUETEMP1 = primenum1/i;// Quotient PRIMENUM1 = temp1;//assigns the quotient to PRIMENUM1, re-determines whether the remainder is 0num1list.add (i);//factorization into num1list} else if (primenum1%i! = 0) {i = i + 1;//as The remainder of the fruit is not equal to 0, divisor I plus 1, continue to seek remainder}}for (int i = 2; i < PN2/2;) {if (primenum2%i = = 0) {Temp2 = Primenum2/i;primenum2 = Temp2;num2list.add (i);} else if (primenum2%i! = 0) {i = i + 1;} }int num1listsize = Num1list.size (); int num2listsize = Num2list.size (); if (Num1listsize < num2listsize) {for (int i = 0 ; I < num1list.size ();) {if (Num2list.contains (Num1list.get (i))) {PRIME_GCD *= num1list.get (i); Num2list.remove (num2list. IndexOf (Num1list.get (i))); Num1list.remove (i); if (num1list.size () = = 0 | | num2list.size () = = 0) break; else {i = i + 1;}}} else {for (int i = 0; i < num2list.size ();) {if (Num1list.contains (Num2list.get (i))) {PRIME_GCD *= num2list.get (i); Num1list.remove (Num1list.indexof ( Num2list.get (i)); Num2list.remove (i); if (num1list.size () = = 0 | | num2list.size () = = 0) break; else {i = i + 1;}}} return PRIME_GCD;}}


Java greatest common divisor (decomposition factorization)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.