Sum of element whose prime factors are present in array
Last Updated :
17 Aug, 2022
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, also present in the array Factors of 10 are 2 and 5, out of which only 2 is present in the array. So, sum = 2 + 3 = 5 Input: arr[] = {5, 11, 55, 25, 100} Output: 96
Approach: The idea is to first calculate the least prime factor till maximum element of the array with Prime Factorization using Sieve.
- Now, Iterate the array and for an element arr[i]
- If arr[i] != 1:
- If leastPrimeFactor(arr[i]) is present in the array then update arr[i] / leastPrimeFactor(arr[i]) and go to step 2.
- Else go to step 1.
- Else update sum = sum + originalVal(arr[i]).
- Print the sum in the end.
Below is the implementation of the above approach:
C++
// C++ program to find the sum of the elements of an array
// whose prime factors are present in the same array
#include <bits/stdc++.h>
using namespace std;
#define MAXN 1000001
// Stores smallest prime factor for every number
int spf[MAXN];
// Function to calculate SPF (Smallest Prime Factor)
// for every number till MAXN
void sieve()
{
spf[1] = 1;
for (int i = 2; i < MAXN; i++)
// Marking smallest prime factor for every
// number to be itself.
spf[i] = i;
// Separately marking spf for every even
// number as 2
for (int i = 4; i < MAXN; i += 2)
spf[i] = 2;
for (int i = 3; i * i < MAXN; i++) {
// If i is prime
if (spf[i] == i) {
// Marking SPF for all numbers divisible by i
for (int j = i * i; j < MAXN; j += i)
// Marking spf[j] if it is not
// previously marked
if (spf[j] == j)
spf[j] = i;
}
}
}
// Function to return the sum of the elements of an array
// whose prime factors are present in the same array
int sumFactors(int arr[], int n)
{
// Function call to calculate smallest prime factors of
// all the numbers upto MAXN
sieve();
// Create map for each element
std::map<int, int> map;
for (int i = 0; i < n; ++i)
map[arr[i]] = 1;
int sum = 0;
for (int i = 0; i < n; ++i) {
int num = arr[i];
// If smallest prime factor of num is present in array
while (num != 1 && map[spf[num]] == 1) {
num /= spf[num];
}
// Each factor of arr[i] is present in the array
if (num == 1)
sum += arr[i];
}
return sum;
}
// Driver program
int main()
{
int arr[] = { 5, 11, 55, 25, 100 };
int n = sizeof(arr) / sizeof(arr[0]);
// Function call to print required answer
cout << sumFactors(arr, n);
return 0;
}
Java
// Java program to find the sum of the elements of an array
// whose prime factors are present in the same array
import java.util.*;
public class GFG{
final static int MAXN = 1000001 ;
// Stores smallest prime factor for every number
static int spf[] = new int [MAXN];
// Function to calculate SPF (Smallest Prime Factor)
// for every number till MAXN
static void sieve()
{
spf[1] = 1;
for (int i = 2; i < MAXN; i++)
// Marking smallest prime factor for every
// number to be itself.
spf[i] = i;
// Separately marking spf for every even
// number as 2
for (int i = 4; i < MAXN; i += 2)
spf[i] = 2;
for (int i = 3; i * i < MAXN; i++) {
// If i is prime
if (spf[i] == i) {
// Marking SPF for all numbers divisible by i
for (int j = i * i; j < MAXN; j += i)
// Marking spf[j] if it is not
// previously marked
if (spf[j] == j)
spf[j] = i;
}
}
}
// Function to return the sum of the elements of an array
// whose prime factors are present in the same array
static int sumFactors(int arr[], int n)
{
// Function call to calculate smallest prime factors of
// all the numbers upto MAXN
sieve();
// Create map for each element
Map map=new HashMap();
for(int i = 0 ; i < MAXN ; ++i)
map.put(i,0) ;
for (int i = 0; i < n; ++i)
map.put(arr[i],1);
int sum = 0;
for (int i = 0; i < n; ++i) {
int num = arr[i];
// If smallest prime factor of num is present in array
while (num != 1 && (int)(map.get(spf[num])) == 1) {
num /= spf[num];
}
// Each factor of arr[i] is present in the array
if (num == 1)
sum += arr[i];
}
return sum;
}
// Driver program
public static void main(String []args)
{
int arr[] = { 5, 11, 55, 25, 100 };
int n = arr.length ;
// Function call to print required answer
System.out.println(sumFactors(arr, n)) ;
}
// This code is contributed by Ryuga
}
Python3
# Python program to find the sum of the
# elements of an array whose prime factors
# are present in the same array
from collections import defaultdict
MAXN = 1000001
MAXN_sqrt = int(MAXN ** (0.5))
# Stores smallest prime factor
# for every number
spf = [None] * (MAXN)
# Function to calculate SPF (Smallest
# Prime Factor) for every number till MAXN
def sieve():
spf[1] = 1
for i in range(2, MAXN):
# Marking smallest prime factor
# for every number to be itself.
spf[i] = i
# Separately marking spf for every
# even number as 2
for i in range(4, MAXN, 2):
spf[i] = 2
for i in range(3, MAXN_sqrt):
# If i is prime
if spf[i] == i:
# Marking SPF for all numbers
# divisible by i
for j in range(i * i, MAXN, i):
# Marking spf[j] if it is
# not previously marked
if spf[j] == j:
spf[j] = i
# Function to return the sum of the elements
# of an array whose prime factors are present
# in the same array
def sumFactors(arr, n):
# Function call to calculate smallest
# prime factors of all the numbers upto MAXN
sieve()
# Create map for each element
Map = defaultdict(lambda:0)
for i in range(0, n):
Map[arr[i]] = 1
Sum = 0
for i in range(0, n):
num = arr[i]
# If smallest prime factor of num
# is present in array
while num != 1 and Map[spf[num]] == 1:
num = num // spf[num]
# Each factor of arr[i] is present
# in the array
if num == 1:
Sum += arr[i]
return Sum
# Driver Code
if __name__ == "__main__":
arr = [5, 11, 55, 25, 100]
n = len(arr)
# Function call to print
# required answer
print(sumFactors(arr, n))
# This code is contributed by Rituraj Jain
C#
// C# program to find the sum of the elements
// of an array whose prime factors are present
// in the same array
using System;
using System.Collections.Generic;
class GFG
{
static int MAXN = 1000001;
// Stores smallest prime factor for every number
static int []spf = new int [MAXN];
// Function to calculate SPF (Smallest Prime Factor)
// for every number till MAXN
static void sieve()
{
spf[1] = 1;
for (int i = 2; i < MAXN; i++)
// Marking smallest prime factor for
// every number to be itself.
spf[i] = i;
// Separately marking spf for every even
// number as 2
for (int i = 4; i < MAXN; i += 2)
spf[i] = 2;
for (int i = 3; i * i < MAXN; i++)
{
// If i is prime
if (spf[i] == i)
{
// Marking SPF for all numbers divisible by i
for (int j = i * i; j < MAXN; j += i)
// Marking spf[j] if it is not
// previously marked
if (spf[j] == j)
spf[j] = i;
}
}
}
// Function to return the sum of the elements
// of an array whose prime factors are present
// in the same array
static int sumFactors(int []arr, int n)
{
// Function call to calculate smallest
// prime factors of all the numbers upto MAXN
sieve();
// Create map for each element
Dictionary<int, int> map = new Dictionary<int, int>();
for(int i = 0 ; i < MAXN ; ++i)
map.Add(i, 0);
for (int i = 0; i < n; ++i)
{
if(map.ContainsKey(arr[i]))
{
map[arr[i]] = 1;
}
else
{
map.Add(arr[i], 1);
}
}
int sum = 0;
for (int i = 0; i < n; ++i)
{
int num = arr[i];
// If smallest prime factor of num
// is present in array
while (num != 1 &&
(int)(map[spf[num]]) == 1)
{
num /= spf[num];
}
// Each factor of arr[i] is present
// in the array
if (num == 1)
sum += arr[i];
}
return sum;
}
// Driver Code
public static void Main(String []args)
{
int []arr = { 5, 11, 55, 25, 100 };
int n = arr.Length;
// Function call to print required answer
Console.WriteLine(sumFactors(arr, n));
}
}
// This code is contributed by PrinciRaj1992
JavaScript
// JavaScript program to find the sum of the
// elements of an array whose prime factors
// are present in the same array
let MAXN = 1000001
let MAXN_sqrt = Math.floor(MAXN ** (0.5))
// Stores smallest prime factor
// for every number
let spf = new Array(MAXN)
// Function to calculate SPF (Smallest
// Prime Factor) for every number till MAXN
function sieve()
{
spf[1] = 1
for (var i = 2; i < MAXN; i++)
{
// Marking smallest prime factor
// for every number to be itself.
spf[i] = i
}
// Separately marking spf for every
// even number as 2
for (var i = 4; i < MAXN; i += 2)
spf[i] = 2
for (var i = 3; i < MAXN_sqrt; i++)
{
// If i is prime
if (spf[i] == i)
{
// Marking SPF for all numbers
// divisible by i
for (var j = i * i; j < MAXN; j += i)
{
// Marking spf[j] if it is
// not previously marked
if (spf[j] == j)
spf[j] = i
}
}
}
}
// Function to return the sum of the elements
// of an array whose prime factors are present
// in the same array
function sumFactors(arr, n)
{
// Function call to calculate smallest
// prime factors of all the numbers upto MAXN
sieve()
// Create map for each element
let Map = {}
for (var i = 0; i < n; i++)
Map[arr[i]] = 1
let Sum = 0
for (var i = 0; i < n; i++)
{
let num = arr[i]
// If smallest prime factor of num
// is present in array
while (num != 1 && Map.hasOwnProperty(spf[num]))
num = Math.floor(num / spf[num])
// Each factor of arr[i] is present
// in the array
if (num == 1)
Sum += arr[i]
}
return Sum
}
// Driver Code
let arr = [5, 11, 55, 25, 100]
let n = arr.length
// Function call to print
// required answer
console.log(sumFactors(arr, n))
// This code is contributed by phasing17
Time Complexity: O(MAXN*log(log(MAXN)))
Auxiliary Space: O(MAXN)
Similar Reads
Sum of array elements which are prime factors of a given number 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 = 35Output: 12Explanation: From the given array, 5 and 7 are prime factors of 35. Therefore, required sum = 5 +
8 min read
Count of pairs in an Array whose sum is Prime Given an array arr of size N elements, the task is to count the number of pairs of elements in the array whose sum is prime. Examples: Input: arr = {1, 2, 3, 4, 5} Output: 5 Explanation: Pairs with sum as a prime number are: {1, 2}, {1, 4}, {2, 3}, {2, 5} and {3, 4} Input: arr = {10, 20, 30, 40} Out
10 min read
Find prime factors of Array elements whose sum of exponents is divisible by K Given an array arr[] of N positive integers and an integer K., The task is to create a set of prime numbers such that the sum of all the powers of prime numbers in the prime factorization of all the array elements is divisible by K. Examples: Input: arr[] = {1, 2, 3}, K = 1 Output: {2, 3} Explanatio
12 min read
Sum of elements in an array having prime frequency Given an array arr, the task is to find the sum of the elements which have prime frequencies in the array. Note: 1 is neither prime nor composite.Examples: Input: arr[] = {5, 4, 6, 5, 4, 6} Output: 15 All the elements appear 2 times which is a prime So, 5 + 4 + 6 = 15Input: arr[] = {1, 2, 3, 3, 2, 3
7 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
Sum of factors of the product of a given array Given an array arr[] consisting of N positive integers, the task is to find the sum of factors of product of all array elements. Since the output can be very large, print it modulo 109 + 7. Examples: Input: arr[] = { 1, 2, 3, 4, 5 } Output: 360 Explanation: The product of all array elements = 1 * 2
15+ 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
Check whether the sum of prime elements of the array is prime or not Given an array having N elements. The task is to check if the sum of prime elements of the array is prime or not. Examples: Input: arr[] = {1, 2, 3} Output: Yes As there are two primes in the array i.e. 2 and 3. So, the sum of prime is 2 + 3 = 5 and 5 is also prime. Input: arr[] = {2, 3, 2, 2} Outpu
10 min read
Maximize sum of count of distinct prime factors of K array elements Given an array arr[] of size N, the task is to find the maximum sum possible of the count of distinct prime factors of K array elements. Examples: Input: arr[] = {6, 9, 12}, K = 2Output: 4Explanation: Distinct prime factors of 6, 9, 12 are 2, 1, 2. K elements whose distinct prime factors are maximum
10 min read