๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๊ฐœ์ธ๊ณต๋ถ€/Algorithm

[๋ฐฑ์ค€][Node.js] 9613๋ฒˆ : GCD ํ•ฉ

by ๐Ÿ‡๋ฐ•๋ด‰๋ด‰๐Ÿ‡ 2021. 12. 28.

 

 

Algorithm

-  GCD ํ•ฉ -

 


 

๋ฌธ์ œ

์–‘์˜ ์ •์ˆ˜ n๊ฐœ๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ์Œ์˜ GCD์˜ ํ•ฉ์„ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

 

์ž…์ถœ๋ ฅ ์˜ˆ์‹œ

 

๋ฌธ์ œ ํ’€๊ธฐ

const readline = require("readline");
const rl = readline.createInterface({
	input: process.stdin,
	output: process.stdout,
});
let input = [];
rl.on("line", function (line) {
	input.push(
		line
			.toString()
			.split(" ")
			.map((el) => +el)
	);
}).on("close", function () {
	input.shift();
	let result = [];

	for (let i = 0; i < input.length; i++) {
		result.push(findValue(input[i][0], input[i].splice(1)));
	}
	console.log(result.join("\n"));
	process.exit();
});

let findValue = (a, b) => {
	let sum = 0;

	for (let i = 0; i < a - 1; i++) {
		for (let j = i + 1; j < a; j++) {
			sum += gcd(b[i], b[j]);
		}
	}
	return sum;
};

let gcd = (x, y) => {
	while (y) {
		[x, y] = [y, x % y];
	}
	return x;
};

 

 

 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€