Sum of array elements which are prime factors of a given number
Last Updated :
19 Apr, 2021
Given an array arr[] of size N and a positive integer K, the task is to find the sum of all array elements which are prime factors of K.
Examples:
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.
Approach: The idea is to traverse the array and for each array element, check if it is a prime factor of K or not. Add those elements to the sum, for which the condition satisfies. Follow the steps below to solve the problem:
- Initialize a variable, say sum, to store the required sum.
- Traverse the given array, and for each array element, perform the following operations:
- After complete traversal of the array, print the value of the sum as the result.
Below is the implementation of the above approach:
C++
// C++ program for the above approach
#include <bits/stdc++.h>
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
<script>
// Javascript program for the above approach
// Function to check if a
// number is prime or not
function 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 || n % 3 == 0)
return false;
var i;
// Above condition allows to check only
// for every 6th number, starting from 5
for (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
function primeFactorSum(arr, n, k)
{
// Stores the required sum
var sum = 0;
var i;
// Traverse the given array
for (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
document.write(sum);
}
// Driver Code
// Given arr[]
var arr = [1, 2, 3, 5, 6, 7, 15]
// Store the size of the array
var N = arr.length;
var K = 35;
primeFactorSum(arr, N, K);
</script>
Time Complexity: O(N*?X), where X is the largest element in the array
Auxiliary Space: O(1)
Similar Reads
Sum of array elements which are multiples of a given number Given an array arr[] consisting of positive integers and an integer N, the task is to find the sum of all array elements which are multiples of N Examples: Input: arr[] = {1, 2, 3, 5, 6}, N = 3Output: 9Explanation: From the given array, 3 and 6 are multiples of 3. Therefore, sum = 3 + 6 = 9. Input:
5 min read
Sum of element whose prime factors are present in array Given an array arr[] of non-negative integers where 2 ? arr[i] ? 106. The task is to find the sum of all those elements from the array whose prime factors are present in the same array. Examples: Input: arr[] = {2, 3, 10} Output: 5 Factor of 2 is 2 which is present in the array Factor of 3 is 3, als
10 min read
Find elements in given Array that are a factor of sum of remaining elements Given an array A[] of size N, the task is to find the elements in the array which are factors of the sum of the remaining element. So just select an element from an array and take the sum of the remaining elements and check whether the sum is perfectly divisible by the selected element or not. If it
11 min read
Find the sum of non-prime elements in the given array Given an array arr[] and the task is to print the sum of the non-prime elements from the array. Examples: Input: arr[] = {1, 3, 7, 4, 9, 8} Output: 22 Non-prime elements are {1, 4, 9, 8} and 1 + 4 + 9 + 8 = 22 Input: arr[] = {11, 4, 10, 7} Output: 14 Approach: Initialize sum = 0 and start traversing
8 min read
Prime factors of LCM of array elements Given an array arr[] such that 1 <= arr[i] <= 10^12, the task is to find prime factors of LCM of array elements. Examples: Input : arr[] = {1, 2, 3, 4, 5, 6, 7, 8} Output : 2 3 5 7 // LCM of n elements is 840 and 840 = 2*2*2*3*5*7 // so prime factors would be 2, 3, 5, 7 Input : arr[] = {20, 10
15+ min read
Sum of Maximum and Minimum prime factor of every number in the Array Given an array arr[], the task is to find the sum of the maximum and the minimum prime factor of every number in the given array.Examples: Input: arr[] = {15} Output: 8 The maximum and the minimum prime factors of 15 are 5 and 3 respectively.Input: arr[] = {5, 10, 15, 20, 25, 30} Output: 10 7 8 7 10
9 min read
Sum of every Kâth prime number in an array Given an integer k and an array of integers arr (less than 10^6), the task is to find the sum of every kâth prime number in the array. Examples: Input: arr[] = {2, 3, 5, 7, 11}, k = 2 Output: 10 All the elements of the array are prime. So, the prime numbers after every K (i.e. 2) interval are 3, 7 a
9 min read
Sum of every K'th prime number in an array Given an array of integers (less than 10^6), the task is to find the sum of all the prime numbers which appear after every (k-1) prime number i.e. every K'th prime number in the array. Examples: Input : Array : 2, 3, 5, 7, 11 ; n=5; k=2 Output : Sum = 10 Explanation: All the elements of the array ar
7 min read
Find sum of exponents of prime factors of numbers 1 to N Given an integer N, the task is to find the sum of exponents of prime factors of numbers 1 to N. Examples: Input: N = 4Output: 4Explanation:Numbers up to 4 are 1, 2, 3, 4 whereThe exponent of 1 in the prime factorization of 1 is 0 (20), For 2 it is 1 (21), For 3 it is 1 (31), and For 4 it is 2 (22).
10 min read