Problem 17

3 comments on “Problem 17”


  • culbgaming12

    #include
    using namespace std;

    int gcd(int a, int b)
    {
    if (a == 0)
    {
    return b;
    }

    if (b == 0)
    {
    return a;
    }

    if (a > b)
    {
    return gcd(b, a % b);
    }

    if (b > a)
    {
    return gcd(a, b % a);
    }
    }

    int lcm(int a, int b)
    {
    if (a == 1)
    {
    return b;
    }

    if (b == 1)
    {
    return a;
    }

    else
    return a * b / gcd(a, b);
    }
    int main()
    {
    int n, m;

    cin >> n >> m;
    int arr[n];
    int brr[m];
    for (int i = 0; i > arr[i];
    }

    for (int i = 0; i > brr[i];
    }

    int con = brr[0];
    int lcm1;
    int count = 0;

    for (int i = 0; i < m – 1; i++)
    {
    con = gcd(con, brr[i + 1]);
    }

    for (int i = 0; i < n – 1; i++)
    {
    lcm1 = lcm(arr[i], arr[i + 1]);
    }

    for (int i = lcm1; i <= con; i++)
    {
    if (con % i == 0 && i % lcm1 == 0)
    {
    count++;
    }
    }

    cout << count << endl;
    return 0;
    }


  • banawariswami123

    import java.io.*;
    import java.math.*;
    import java.security.*;
    import java.text.*;
    import java.util.*;
    import java.util.concurrent.*;
    import java.util.regex.*;
    import java.util.Collections;

    class Result {

    /*
    * Complete the ‘getTotalX’ function below.
    *
    * The function is expected to return an INTEGER.
    * The function accepts following parameters:
    * 1. INTEGER_ARRAY a
    * 2. INTEGER_ARRAY b
    */

    public static int getTotalX(List a, List b) {
    // Write your code here

    Collections.sort(a);
    Collections.sort(b);
    //System.out.println(a);
    //System.out.println(b);

    int numOfInters = 0;
    for(Integer element = a.get((a.size()-1));element <= b.get(0); element++ ){
    System.out.println(element);

    int i;
    for(i = 0; i < a.size(); ++i){
    if(element%a.get(i) == 0){
    continue;
    }else{
    break;
    }
    }
    if(i < a.size()){
    continue;
    }
    for(i = 0; i < b.size(); ++i){
    if(b.get(i)%element == 0){
    continue;
    }else{
    break;
    }
    }
    if(i < b.size()){
    continue;
    }else{
    numOfInters++;
    }
    }
    return numOfInters;
    }

    }

    public class Solution {
    public static void main(String[] args) throws IOException {
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
    BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

    String[] firstMultipleInput = bufferedReader.readLine().replaceAll("\\s+$", "").split(" ");

    int n = Integer.parseInt(firstMultipleInput[0]);

    int m = Integer.parseInt(firstMultipleInput[1]);

    String[] arrTemp = bufferedReader.readLine().replaceAll("\\s+$", "").split(" ");

    List arr = new ArrayList();

    for (int i = 0; i < n; i++) {
    int arrItem = Integer.parseInt(arrTemp[i]);
    arr.add(arrItem);
    }

    String[] brrTemp = bufferedReader.readLine().replaceAll("\\s+$", "").split(" ");

    List brr = new ArrayList();

    for (int i = 0; i < m; i++) {
    int brrItem = Integer.parseInt(brrTemp[i]);
    brr.add(brrItem);
    }

    int total = Result.getTotalX(arr, brr);

    bufferedWriter.write(String.valueOf(total));
    bufferedWriter.newLine();

    bufferedReader.close();
    bufferedWriter.close();
    }
    }


  • Richik

    Python 3 code:-
    def gcd(x,y):
    if y==0:
    return x
    return gcd(y,x%y);
    def lcm(x,y):
    return ((x*y)//gcd(x,y));

    a=list(map(int,input().split()))
    b=list(map(int,input().split()))
    alcm=a[0]
    for i in range(1,len(a)):
    alcm=lcm(a[i],alcm)
    bgcd=b[0]
    for i in range(1,len(b)):
    bgcd=gcd(b[i],bgcd)
    comm=[] #stores the gcd of first array by incrementing itself and keeping itself lesser than any other value of the other array
    while alcm<=b[0]:
    comm.append(alcm)
    alcm+=alcm
    for i in b:
    for j in comm:
    if i%j!=0:
    comm.remove(j) #removing the gcd which is not a lcm for the other set.
    print(comm)