Solving multiple elements least common multiple problem in array by divide-and-conquer method

Source: Internet
Author: User

public class Main {
Test function public static void main (string[] args) {final int length=50;int[] a=new int[length];a[0]=1;for (int i=1;i<length ; i++) A[i]=a[i-1]+1;long Time_start=system.currenttimemillis (); for (int i=0;i<length;i++) System.out.println ("front" + (i+1) + "Common multiple:" + getmintimes (A, 0, i)); Long Time_stop=system.currenttimemillis (); System.out.println ("Time:" + (Time_stop-time_start) + "MS");} public static long Getmintimes (int[] a,int Low, int.) {if ((high-low) >1) {int mid= (high+low)/2;long left= Getmintimes (A, low, mid), Long Right=getmintimes (A, mid+1, high), return Left/getmaxdivisor (left, right) *right;} else if ((high-low) ==1) {return a[low]/getmaxdivisor (A[low], A[high]) *a[high];} else {return a[low];}} public static long Getmaxdivisor (long X,long y) {long a=x>y?x:y;long b=x>y?y:x;long rmd=a%b;while (rmd!=0) {a=b;b= Rmd;rmd=a%b;} return b;}}

Each time the array is divided into two, then the least common multiple of the left and right two arrays are then merged, and the following is the result of the operation:

 top 1 of common multiple: 1 The first 2 of common multiple: 2 ago 3 of common multiple: 6 before 4 of common multiple: 12 before 5 of common multiple: 60 before 6 of common multiple: 60 ago 7 of common multiple: 420 ago 8 of common multiple: 840 ago 9 of common multiple: 2520 before 1 0 of common multiple: 2520 Top 11 of common multiple: 27720 ago 12 of common multiple: 27720 ago 13 of common multiple: 360360 ago 14 of common multiple: 360360 before 15 of common multiple: 360360 before 16 of common multiple: 720720 ago 17 of common multiple: 12252240 ago 18 of common multiple: 12252 240 Top 19 Common multiple: 232792560 20 of common multiple: 232792560 before 21 of common multiple: 232792560 ago 22 of common multiple: 232792560 ago 23 of common multiple: 5354228880 before 24 of common multiple: 5354228880 ago 25 of common multiple: 267711 44400 Top 26 Common multiple: 26771144400 27 of common multiple: 80313433200 before 28 of common multiple: 80313433200 ago 29 of common multiple: 2329089562800 ago 30 of common multiple: 2329089562800 ago 31 of common multiple: 722017764 46800 First 32 Common multiple: 144403552893600 33 of common multiple: 144403552893600 before 34 of common multiple: 144403552893600 before 35 of common multiple: 144403552893600 ago 36 of common multiple: 144403552893600 ago 37 of common multiple: 5342931457063200 top 38 of common multiple: 5342931457063200 ago 39 of common multiple: 5342931457063200 before 40 of common multiple: 5342931457063200 ago 41 of common multiple: 219060189739591200 Top 42 Common multiple: 219060189739591200 top 43 of common multiple: 7550878139845887136 ago 44 of common multiple: 7550878139845887136 before 45 of common multiple: 9027155914907130016 before 46 of common multiple:-7649 500553069439584 Top 47 of common multiple: 3012796197927393248 48 of common multiple: 3012796197927393248 before 49 of common multiple: 8062099791253733216 first 50 of common multiple: 3382763084072585 376time:16ms 

After a few lines, all errors are displayed because the long range is exceeded.

Solving multiple elements least common multiple problem in array by divide-and-conquer method

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.