Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Least Majority Multiple
- // Given five positive integers, their least majority multiple is the smallest positive integer that is divisible by at least three of them.
- // Your task is to write a program that for given distinct integers a, b, c, d and e, returns their least majority multiple.
- // For example if we have 1, 2, 3, 4 and 5 the majority multiple of the given five numbers is 4 because it is divisible by 1, 2, and 4.
- // Another example: if we have 30, 42, 70, 35 and 90 the answer will be 210, because it is divisible by 30, 42, 70, and 35 - four out of five numbers, which is a majority.
- // Input
- // Read from the standard input
- // The input data will consist of 5 lines.
- // The numbers a, b, c, d and e will each be on a single line.
- // The input data will always be valid and in the format described. There is no need to check it explicitly.
- // Output
- // Print on the standard output
- // On the only output line you must print the least majority multiple of the given numbers.
- // Constraints
- // a, b, c, d and e will each be integer numbers between 1 and 100, inclusive.
- // a, b, c, d and e will be distinct.
- // Sample Tests
- // Input Output
- // 1
- // 2
- // 3
- // 4
- // 5 4
- // 30
- // 42
- // 70
- // 35
- // 90 210
- // JavaScript
- // Credit to https://www.w3resource.com/javascript-exercises/javascript-math-exercise-10.php
- const input = ['1', '2', '3', '4', '5'];
- const print = this.print || console.log;
- const gets = this.gets || ((arr, index) => () => arr[index++])(input, 0);
- const numArray = [];
- for (let i = 0; i < 5; i++) {
- numArray.push(+gets());
- }
- numArray.sort((a, b) => a - b);
- function gcdTwoNumbers (x, y) {
- x = Math.abs(x);
- y = Math.abs(y);
- while (y) {
- const t = y;
- y = x % y;
- x = t;
- }
- return x;
- }
- function lcmTwoNumbers (x, y) {
- if ((typeof x !== 'number') || (typeof y !== 'number')) {
- return false;
- }
- return (!x || !y) ? 0 : Math.abs((x * y) / gcdTwoNumbers(x, y));
- }
- function lcm3 (x, y, z) {
- return lcmTwoNumbers(lcmTwoNumbers(x, y), z);
- }
- const a = numArray[0];
- const b = numArray[1];
- const c = numArray[2];
- const d = numArray[3];
- const e = numArray[4];
- print(Math.min(lcm3(a, b, c), lcm3(a, b, d), lcm3(a, b, e), lcm3(a, c, d), lcm3(a, c, e), lcm3(a, d, e), lcm3(b, c, d), lcm3(b, c, e), lcm3(b, d, e), lcm3(c, d, e)));
Advertisement
Add Comment
Please, Sign In to add comment