给定一个大小为N的数组arr[]和一个正整数K ,任务是找到所有数组元素的总和,这些元素是K 的质因子。
例子:
Input: arr[] = {1, 2, 3, 5, 6, 7, 15}, K = 35
Output: 12
Explanation: From the given array, 5 and 7 are prime factors of 35. Therefore, required sum = 5 + 7 = 12.
Input: arr[] = {1, 3, 5, 7}, K = 42
Output: 10
Explanation: From the given array, 3 and 7 are prime factors of 42. Therefore, required sum = 3 + 7 = 10.
方法:思想是遍历数组,对于每个数组元素,检查它是否是K的质因数。将满足条件的这些元素添加到总和中。请按照以下步骤解决问题:
- 初始化一个变量,比如sum ,以存储所需的总和。
- 遍历给定的数组,对每个数组元素,执行如下操作:
- 检查数组元素是否是K的质因数。
- 如果发现为真,则将当前元素添加到sum 中。
- 遍历完数组后,打印总和的值作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to check if a
// number is prime or not
bool isPrime(int n)
{
// Corner cases
if (n <= 1)
return false;
if (n <= 3)
return true;
// Check if n is a
// multiple of 2 or 3
if (n % 2 == 0 || n % 3 == 0)
return false;
// Above condition allows to check only
// for every 6th number, starting from 5
for (int i = 5; i * i <= n; i = i + 6)
// If n is a multiple of i and i + 2
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
// Function to find the sum of array
// elements which are prime factors of K
void primeFactorSum(int arr[], int n, int k)
{
// Stores the required sum
int sum = 0;
// Traverse the given array
for (int i = 0; i < n; i++) {
// If current element is a prime
// factor of k, add it to the sum
if (k % arr[i] == 0 && isPrime(arr[i])) {
sum = sum + arr[i];
}
}
// Print the result
cout << sum;
}
// Driver Code
int main()
{
// Given arr[]
int arr[] = { 1, 2, 3, 5, 6, 7, 15 };
// Store the size of the array
int N = sizeof(arr) / sizeof(arr[0]);
int K = 35;
primeFactorSum(arr, N, K);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG
{
// Function to check if a
// number is prime or not
static boolean isPrime(int n)
{
// Corner cases
if (n <= 1)
return false;
if (n <= 3)
return true;
// Check if n is a
// multiple of 2 or 3
if (n % 2 == 0 || n % 3 == 0)
return false;
// Above condition allows to check only
// for every 6th number, starting from 5
for (int i = 5; i * i <= n; i = i + 6)
// If n is a multiple of i and i + 2
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
// Function to find the sum of array
// elements which are prime factors of K
static void primeFactorSum(int arr[], int n, int k)
{
// Stores the required sum
int sum = 0;
// Traverse the given array
for (int i = 0; i < n; i++) {
// If current element is a prime
// factor of k, add it to the sum
if (k % arr[i] == 0 && isPrime(arr[i]))
{
sum = sum + arr[i];
}
}
// Print the result
System.out.println(sum);
}
// Driver code
public static void main(String[] args)
{
// Given arr[]
int arr[] = { 1, 2, 3, 5, 6, 7, 15 };
// Store the size of the array
int N = arr.length;
int K = 35;
primeFactorSum(arr, N, K);
}
}
// This code is contributed by Kingash.
Python3
# Python3 program for the above approach
# Function to check if a
# number is prime or not
def isPrime(n):
# Corner cases
if (n <= 1):
return False
if (n <= 3):
return True
# Check if n is a
# multiple of 2 or 3
if (n % 2 == 0 or n % 3 == 0):
return False
# Above condition allows to check only
# for every 6th number, starting from 5
i = 5
while (i * i <= n ):
# If n is a multiple of i and i + 2
if (n % i == 0 or n % (i + 2) == 0):
return False
i = i + 6
return True
# Function to find the sum of array
# elements which are prime factors of K
def primeFactorSum(arr, n, k):
# Stores the required sum
sum = 0
# Traverse the given array
for i in range(n):
# If current element is a prime
# factor of k, add it to the sum
if (k % arr[i] == 0 and isPrime(arr[i])):
sum = sum + arr[i]
# Print the result
print(sum)
# Driver Code
# Given arr[]
arr = [ 1, 2, 3, 5, 6, 7, 15 ]
# Store the size of the array
N = len(arr)
K = 35
primeFactorSum(arr, N, K)
# This code is contributed by code_hunt
C#
// C# program for the above approach
using System;
class GFG
{
// Function to check if a
// number is prime or not
static bool isPrime(int n)
{
// Corner cases
if (n <= 1)
return false;
if (n <= 3)
return true;
// Check if n is a
// multiple of 2 or 3
if (n % 2 == 0 || n % 3 == 0)
return false;
// Above condition allows to check only
// for every 6th number, starting from 5
for (int i = 5; i * i <= n; i = i + 6)
// If n is a multiple of i and i + 2
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
// Function to find the sum of array
// elements which are prime factors of K
static void primeFactorSum(int []arr, int n, int k)
{
// Stores the required sum
int sum = 0;
// Traverse the given array
for (int i = 0; i < n; i++) {
// If current element is a prime
// factor of k, add it to the sum
if (k % arr[i] == 0 && isPrime(arr[i]))
{
sum = sum + arr[i];
}
}
// Print the result
Console.Write(sum);
}
// Driver code
public static void Main(string[] args)
{
// Given arr[]
int []arr = { 1, 2, 3, 5, 6, 7, 15 };
// Store the size of the array
int N = arr.Length;
int K = 35;
primeFactorSum(arr, N, K);
}
}
// This code is contributed by ukasp.
Javascript
输出:
12
时间复杂度: O(N*√X),其中 X 是数组中最大的元素
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live