可被数组的所有元素整除的最小完全平方
给定一个数组arr[] ,任务是找到可以被给定数组的所有元素整除的最小完美正方形。
例子:
Input: arr[] = {2, 3, 4, 5, 7}
Output: 44100
Input: arr[] = {20, 25, 14, 21, 100, 18, 42, 16, 55}
Output: 21344400
朴素方法:从 1 开始逐一检查所有平方数,并选择可被数组中所有元素整除的那个。
高效方法:找到数组所有元素的最小公倍数并将其存储在变量lcm中。找到找到的 LCM 的所有素数。
现在检查是否有任何素因数除以 lcm 奇数次。如果有这些因素,将 LCM 乘以这些因素。最后打印更新的LCM 。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
#define ll long long int
// Function to return the gcd of two numbers
ll gcd(ll a, ll b)
{
if (b == 0)
return a;
else
return gcd(b, a % b);
}
// Function to return the lcm of
// all the elements of the array
ll lcmOfArray(int arr[], int n)
{
if (n < 1)
return 0;
ll lcm = arr[0];
// To calculate lcm of two numbers
// multiply them and divide the result
// by gcd of both the numbers
for (int i = 1; i < n; i++)
lcm = (lcm * arr[i]) / gcd(lcm, arr[i]);
// Return the LCM of the array elements
return lcm;
}
// Function to return the smallest perfect square
// divisible by all the elements of arr[]
int minPerfectSquare(int arr[], int n)
{
ll minPerfectSq;
// LCM of all the elements of arr[]
ll lcm = lcmOfArray(arr, n);
minPerfectSq = (long long)lcm;
// Check if 2 divides lcm odd number of times
int cnt = 0;
while (lcm > 1 && lcm % 2 == 0) {
cnt++;
lcm /= 2;
}
if (cnt % 2 != 0)
minPerfectSq *= 2;
int i = 3;
// Check all the numbers that divide lcm
// odd number of times
while (lcm > 1) {
cnt = 0;
while (lcm % i == 0) {
cnt++;
lcm /= i;
}
// If i divided lcm odd number of times
// then multiply the lcm with i
if (cnt % 2 != 0)
minPerfectSq *= i;
i += 2;
}
// Return the answer
return minPerfectSq;
}
// Driver code
int main()
{
int arr[] = { 2, 3, 4, 5, 7 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << minPerfectSquare(arr, n);
return 0;
}
Java
// Java implementation of the approach
import java.util.*;
class solution
{
// Function to return the gcd of two numbers
static int gcd(int a, int b)
{
if (b == 0)
return a;
else
return gcd(b, a % b);
}
// Function to return the lcm of
// all the elements of the array
static int lcmOfArray(int arr[], int n)
{
if (n < 1)
return 0;
int lcm = arr[0];
// To calculate lcm of two numbers
// multiply them and divide the result
// by gcd of both the numbers
for (int i = 1; i < n; i++)
lcm = (lcm * arr[i]) / gcd(lcm, arr[i]);
// Return the LCM of the array elements
return lcm;
}
// Function to return the smallest perfect square
// divisible by all the elements of arr[]
static int minPerfectSquare(int arr[], int n)
{
int minPerfectSq;
// LCM of all the elements of arr[]
int lcm = lcmOfArray(arr, n);
minPerfectSq = (int)lcm;
// Check if 2 divides lcm odd number of times
int cnt = 0;
while (lcm > 1 && lcm % 2 == 0) {
cnt++;
lcm /= 2;
}
if (cnt % 2 != 0)
minPerfectSq *= 2;
int i = 3;
// Check all the numbers that divide lcm
// odd number of times
while (lcm > 1) {
cnt = 0;
while (lcm % i == 0) {
cnt++;
lcm /= i;
}
// If i divided lcm odd number of times
// then multiply the lcm with i
if (cnt % 2 != 0)
minPerfectSq *= i;
i += 2;
}
// Return the answer
return minPerfectSq;
}
// Driver code
public static void main(String args[])
{
int arr[] = { 2, 3, 4, 5, 7 };
int n = arr.length;
System.out.println(minPerfectSquare(arr, n));
}
}
// This code is contributed by
// Shashank_Sharma
Python3
# Python 3 implementation of the approach
from math import gcd
# Function to return the lcm of
# all the elements of the array
def lcmOfArray(arr, n):
if (n < 1):
return 0
lcm = arr[0]
# To calculate lcm of two numbers
# multiply them and divide the result
# by gcd of both the numbers
for i in range(1, n, 1):
lcm = int((lcm * arr[i]) /
gcd(lcm, arr[i]))
# Return the LCM of the array elements
return lcm
# Function to return the smallest perfect
# square divisible by all the elements of arr[]
def minPerfectSquare(arr, n):
# LCM of all the elements of arr[]
lcm = lcmOfArray(arr, n)
minPerfectSq = int(lcm)
# Check if 2 divides lcm odd
# number of times
cnt = 0
while (lcm > 1 and lcm % 2 == 0):
cnt += 1
lcm /= 2
if (cnt % 2 != 0):
minPerfectSq *= 2
i = 3
# Check all the numbers that divide
# lcm odd number of times
while (lcm > 1):
cnt = 0;
while (lcm % i == 0):
cnt += 1
lcm /= i
# If i divided lcm odd number of
# times then multiply the lcm with i
if (cnt % 2 != 0):
minPerfectSq *= i
i += 2
# Return the answer
return minPerfectSq
# Driver code
if __name__ == '__main__':
arr = [2, 3, 4, 5, 7]
n = len(arr)
print(minPerfectSquare(arr, n))
# This code is contributed by
# Sanjit_Prasad
C#
// C# implementation of the approach
using System;
public class GFG{
// Function to return the gcd of two numbers
static int gcd(int a, int b)
{
if (b == 0)
return a;
else
return gcd(b, a % b);
}
// Function to return the lcm of
// all the elements of the array
static int lcmOfArray(int []arr, int n)
{
if (n < 1)
return 0;
int lcm = arr[0];
// To calculate lcm of two numbers
// multiply them and divide the result
// by gcd of both the numbers
for (int i = 1; i < n; i++)
lcm = (lcm * arr[i]) / gcd(lcm, arr[i]);
// Return the LCM of the array elements
return lcm;
}
// Function to return the smallest perfect square
// divisible by all the elements of arr[]
static int minPerfectSquare(int []arr, int n)
{
int minPerfectSq;
// LCM of all the elements of arr[]
int lcm = lcmOfArray(arr, n);
minPerfectSq = (int)lcm;
// Check if 2 divides lcm odd number of times
int cnt = 0;
while (lcm > 1 && lcm % 2 == 0) {
cnt++;
lcm /= 2;
}
if (cnt % 2 != 0)
minPerfectSq *= 2;
int i = 3;
// Check all the numbers that divide lcm
// odd number of times
while (lcm > 1) {
cnt = 0;
while (lcm % i == 0) {
cnt++;
lcm /= i;
}
// If i divided lcm odd number of times
// then multiply the lcm with i
if (cnt % 2 != 0)
minPerfectSq *= i;
i += 2;
}
// Return the answer
return minPerfectSq;
}
// Driver code
static public void Main (){
int []arr = { 2, 3, 4, 5, 7 };
int n = arr.Length;
Console.WriteLine(minPerfectSquare(arr, n));
}
}
//This code is contributed by ajit.
PHP
1 && $lcm % 2 == 0)
{
$cnt++;
$lcm = floor($lcm / 2);
}
if ($cnt % 2 != 0)
$minPerfectSq *= 2;
$i = 3;
// Check all the numbers that divide
// lcm odd number of times
while ($lcm > 1)
{
$cnt = 0;
while ($lcm % $i == 0)
{
$cnt++;
$lcm = $lcm / $i;
}
// If i divided lcm odd number of times
// then multiply the lcm with i
if ($cnt % 2 != 0)
$minPerfectSq *= $i;
$i += 2;
}
// Return the answer
return $minPerfectSq;
}
// Driver code
$arr = array( 2, 3, 4, 5, 7 );
$n = sizeof($arr);
echo minPerfectSquare($arr, $n);
// This code is contributed by Ryuga
?>
Javascript
输出:
44100