📅  最后修改于: 2023-12-03 15:10:56.250000             🧑  作者: Mango
本文旨在介绍如何检查给定范围内的数组元素的乘积是否为第M个根。
在阅读本文之前,我们需要了解以下知识点:
给定一个长度为N的数组,以及一个整数M,我们需要判断给定数组的元素乘积是否为M的根。
具体而言,我们需要计算给定数组的元素乘积,然后求得该乘积的M次方根,判断该数是否等于给定的数。
下面是Python代码实现:
from math import pow
def check_root(arr, m, num):
product = 1
for i in range(len(arr)):
product *= arr[i]
root = pow(product, 1/m)
return root == num
下面是JavaScript代码实现:
function checkRoot(arr, m, num) {
let product = 1;
for (let i = 0; i < arr.length; i++) {
product *= arr[i];
}
const root = Math.pow(product, 1 / m);
return root === num;
}
下面是Python和JavaScript的使用示例:
arr = [2, 3, 5, 7, 11, 13]
m = 2
num = 55
if check_root(arr, m, num):
print(f"{num} is the {m}-th root of the product of array elements")
else:
print(f"{num} is not the {m}-th root of the product of array elements")
const arr = [2, 3, 5, 7, 11, 13];
const m = 2;
const num = 55;
if (checkRoot(arr, m, num)) {
console.log(`${num} is the ${m}-th root of the product of array elements`);
} else {
console.log(`${num} is not the ${m}-th root of the product of array elements`);
}
以上代码输出的结果都应该为:
55 is the 2-th root of the product of array elements