Sort by

recency

|

3084 Discussions

|

  • + 0 comments
    def getTotalX(a, b):
        l = math.lcm(*a)
        g = math.gcd(*b)
        count = 0
        for x in range(l, g+1, l):
            if (g % x) == 0:
                count += 1
        return count
    
  • + 0 comments
    import java.io.*;
    import java.util.stream.Stream;
    
    public class BetweenTwoSets {
        public static void main(String[] args) {
            try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                 BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out))) {
                int[] firstLine = getArray(br.readLine());
                int[] firstArray = getArray(br.readLine());
                int[] secondArray = getArray(br.readLine());
                bw.write(String.valueOf(countNumbersBetweenSets(firstLine, firstArray, secondArray)));
                bw.flush();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    
        private static int[] getArray(String line) throws IOException {
            return Stream.of(line.split(" ")).mapToInt(Integer::parseInt).toArray();
        }
    
        private static int countNumbersBetweenSets(int[] size, int[] firstArray, int[] secondArray) {
            int lcmOfFirst = calculateLcmOfArray(firstArray, size[0]);
            int gcdOfSecond = calculateGcdOfArray(secondArray, size[1]);
    
            int count = 0;
            int currentLcm = lcmOfFirst;
            while (currentLcm <= gcdOfSecond) {
                if (gcdOfSecond % currentLcm == 0) {
                    count++;
                }
                currentLcm += lcmOfFirst;
            }
            return count;
        }
    
        private static int calculateLcmOfArray(int[] array, int size) {
            int result = array[0];
            for (int i = 1; i < size; i++) {
                result = lcm(result, array[i]);
            }
            return result;
        }
    
        private static int calculateGcdOfArray(int[] array, int size) {
            int result = array[0];
            for (int i = 1; i < size; i++) {
                result = gcd(result, array[i]);
            }
            return result;
        }
    
        private static int lcm(int a, int b) {
            if (a == 0 || b == 0)
                return 0;
            return Math.abs(a * b) / gcd(a, b);
        }
    
        private static int gcd(int a, int b) {
            if (b == 0)
                return a;
            return gcd(b, a % b);
        }
    }
    
  • + 0 comments
    def getTotalX(a, b):
        # Write your code here
        ekuk=math.lcm(*a)
        ekub=math.gcd(*b)
        count=0
        for i in range(ekuk,ekub+1):
            result = list(map(lambda x: i%x==0, a))
            if False not in result:
                result2=list(map(lambda y: y%i==0,b))
                if False not in result2:
                    count+=1
        return count
    
  • + 0 comments

    Find one or more numbers x, y , z etc.. such that for x, a is a factor of x (GCD) b is a multiple of x

    We need to check the numbers in the range from max(a) to min(b).
    Loop through this range of integers and check numbers in 'a' divide the number without remainders. Also check x divides numbers in 'b' without remainders.

  • + 0 comments

    Python Code:

    def lcm(x, y): return x * y // math.gcd(x, y)

    def getTotalX(a, b): # Find LCM of all numbers in a l = a[0] for i in range(1, len(a)): l = lcm(l, a[i])

    # Find GCD of all numbers in b
    g = b[0]
    for i in range(1, len(b)):
        g = math.gcd(g, b[i])
    
    # Count numbers between sets
    count = 0
    for i in range(l, g + 1, l):
        if g % i == 0:
            count += 1
    return count