We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
importjava.io.*;importjava.util.*;importjava.text.*;importjava.math.*;importjava.util.regex.*;classsolution{intgcd(intm,intn){if(n==0)//gcd(m,0) ==>gcd ends ==> m (is the gcd){returnm;}else{returngcd(n,m%n);}}intlcm(int[]a,intn){intres=1,i;for(i=0;i<n;i++){res=res*a[i]/gcd(res,a[i]);}returnres;}publicstaticvoidmain(String[]args){Scannerscan=newScanner(System.in);//finding lcm of a no using now reduction of gcd to lcmsolutionob=newsolution();intn=scan.nextInt();intm=scan.nextInt();int[]a=newint[n];//find lcmint[]b=newint[m];//find only gcdfor(inti=0;i<n;i++){a[i]=scan.nextInt();}//finding lcm of the first arrayintresult=ob.lcm(a,n);intresult2=1;for(inti=0;i<m;i++){intr=1;b[i]=scan.nextInt();}// Finding only the gcd of the second arrayintno=b[0];for(inti=1;i<m;i++){no=ob.gcd(no,b[i]);}//count the no of multiples of lcm that evenly divides gcd//System.out.println("Results:"+result);//System.out.println("Result2:"+no);intcount=0;//count the no of multiplesintf=result;//lcmintl=no;//gcdfor(inti=f;i<=l;i+=f){if(l%i==0){count++;}}System.out.println(count);}}
Between Two Sets
You are viewing a single comment's thread. Return to all comments →
Code for the above solution:
//Ecluids algorithm to find gcd /* gcd(m,n)
pseudo code while n (!=0) { r=m%n; m=n; n=r; } */