Given a non-negative integer n. The problem is to find the sum of the largest prime factor of each number less than equal to n.
Examples:
Input : n = 10 Output : 32 Largest prime factor of each number Prime factor of 2 = 2 Prime factor of 3 = 3 Prime factor of 4 = 2 Prime factor of 5 = 5 Prime factor of 6 = 3 Prime factor of 7 = 7 Prime factor of 8 = 2 Prime factor of 9 = 3 Prime factor of 10 = 5 Sum = (2+3+2+5+3+7+2+3+5) = 32 Input : n = 12 Output : 46
Algorithm:
sumOfLargePrimeFactor(n)
Declare prime[n+1] and initialize all value to 0
Initialize sum = 0
max = n / 2
for p = 2 to max
if prime[p] == 0 then
i = p*2
while i <= n
prime[i] = p
i = i + p
for p = 2 to n
if prime[p] then
sum = sum + prime[p]
else
sum = sum + p
return sum
C++
// C++ implementation to find sum of largest prime factor // of each number less than equal to n #include <bits/stdc++.h> using namespace std; // function to find sum of largest prime factor // of each number less than equal to n int sumOfLargePrimeFactor(int n) { // Create an integer array "prime[0..n]" and initialize // all entries of it as 0. A value in prime[i] will // finally be 0 if 'i' is a prime, else it will // contain the largest prime factor of 'i'. int prime[n+1], sum = 0; memset(prime, 0, sizeof(prime)); int max = n / 2; for (int p=2; p<=max; p++) { // If prime[p] is '0', then it is a // prime number if (prime[p] == 0) { // Update all multiples of p for (int i=p*2; i<=n; i += p) prime[i] = p; } } // Sum up the largest prime factor of all // the numbers for (int p=2; p<=n; p++) { // if 'p' is a non- prime number then // prime[p] gives its largesr prime // factor if (prime[p]) sum += prime[p]; // 'p' is a prime number else sum += p; } // required sum return sum; } // Driver program to test above int main() { int n = 12; cout << "Sum = " << sumOfLargePrimeFactor(n); return 0; } |
chevron_right
filter_none
Java
// Java implementation to find sum // of largest prime factor of each // number less than equal to n import java.io.*; import java.util.*; class GFG { // function to find sum of largest // prime factorof each number // less than equal to n static int sumOfLargePrimeFactor(int n) { // Create an integer array "prime[0..n]" // and initialize all entries of it as 0. // A value in prime[i] will finally be 0 // if 'i' is a prime, else it will contain // the largest prime factor of 'i'. int prime[] = new int[n + 1], sum = 0; Arrays.fill(prime, 0); int max = n / 2; for (int p = 2; p <= max; p++) { // If prime[p] is '0', then it is a // prime number if (prime[p] == 0) { // Update all multiples of p for (int i = p * 2; i <= n; i += p) prime[i] = p; } } // Sum up the largest prime factor of all // the numbers for (int p = 2; p <= n; p++) { // if 'p' is a non- prime number then // prime[p] gives its largesr prime // factor if (prime[p] != 0) sum += prime[p]; // 'p' is a prime number else sum += p; } // required sum return sum; } // Driver program public static void main(String args[]) { int n = 12; System.out.println("Sum = " + sumOfLargePrimeFactor(n)); } } // This code is contributed // by Nikita Tiwari. |
chevron_right
filter_none
Python3
# Python3 code implementation to find # sum of largest prime factor of # each number less than equal to n # function to find sum of largest # prime factor of each number less # than equal to n def sumOfLargePrimeFactor( n ): # Create an integer array "prime[0..n]" # and initialize all entries of it # as 0. A value in prime[i] will # finally be 0 if 'i' is a prime, # else it will contain the largest # prime factor of 'i'. prime = [0] * (n + 1) sum = 0 max = int(n / 2) for p in range(2, max + 1): # If prime[p] is '0', then # it is a prime number if prime[p] == 0: # Update all multiples of p for i in range(p * 2, n + 1, p): prime[i] = p # Sum up the largest prime factor # of all the numbers for p in range(2, n + 1): # if 'p' is a non- prime # number then prime[p] gives # its largesr prime factor if prime[p]: sum += prime[p] # 'p' is a prime number else: sum += p # required sum return sum # Driver code to test above function n = 12print("Sum =", sumOfLargePrimeFactor(n)) # This code is contributed by "Sharad_Bhardwaj". |
chevron_right
filter_none
C#
// C# implementation to find sum // of largest prime factor of each // number less than equal to n using System; class GFG { // function to find sum of largest // prime factorof each number // less than equal to n static int sumOfLargePrimeFactor(int n) { // Create an integer array "prime[0..n]" // and initialize all entries of it as 0. // A value in prime[i] will finally be 0 // if 'i' is a prime, else it will contain // the largest prime factor of 'i'. int[] prime = new int[n + 1]; int sum = 0; for (int i = 1; i < n + 1; i++) prime[i] = 0; int max = n / 2; for (int p = 2; p <= max; p++) { // If prime[p] is '0', then it is a // prime number if (prime[p] == 0) { // Update all multiples of p for (int i = p * 2; i <= n; i += p) prime[i] = p; } } // Sum up the largest prime factor of all // the numbers for (int p = 2; p <= n; p++) { // if 'p' is a non- prime number then // prime[p] gives its largesr prime // factor if (prime[p] != 0) sum += prime[p]; // 'p' is a prime number else sum += p; } // required sum return sum; } // Driver program public static void Main() { int n = 12; Console.WriteLine("Sum = " + sumOfLargePrimeFactor(n)); } } // This code is contributed by Sam007 |
chevron_right
filter_none
PHP
<?php // PHP implementation to find sum of largest prime factor // of each number less than equal to n // function to find sum of largest prime factor // of each number less than equal to n function sumOfLargePrimeFactor($n) { // Create an integer array "prime[0..n]" and initialize // all entries of it as 0. A value in prime[i] will // finally be 0 if 'i' is a prime, else it will // contain the largest prime factor of 'i'. $prime=array_fill(0,$n+1,0); $sum = 0; $max = (int)($n / 2); for ($p=2; $p<=$max; $p++) { // If prime[p] is '0', then it is a // prime number if ($prime[$p] == 0) { // Update all multiples of p for ($i=$p*2; $i<=$n; $i += $p) $prime[$i] = $p; } } // Sum up the largest prime factor of all // the numbers for ($p=2; $p<=$n; $p++) { // if 'p' is a non- prime number then // prime[p] gives its largesr prime // factor if ($prime[$p]) $sum += $prime[$p]; // 'p' is a prime number else $sum += $p; } // required sum return $sum; } // Driver program to test above $n = 12; echo "Sum = ".sumOfLargePrimeFactor($n); // This code is contributed by mits. ?> |
chevron_right
filter_none
Output:
Sum = 46
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Largest number less than N whose each digit is prime number
- Find largest factor of N such that N/F is less than K
- Count all the numbers less than 10^6 whose minimum prime factor is N
- Find Largest Special Prime which is less than or equal to a given number
- Largest number less than N with digit sum greater than the digit sum of N
- Number of triplets such that each value is less than N and each pair sum is a multiple of K
- Largest subset having with sum less than equal to sum of respective indices
- Numbers with sum of digits equal to the sum of digits of its all prime factor
- Largest number with maximum trailing nines which is less than N and greater than N-D
- Sum of Semi-Prime Numbers less than or equal to N
- Largest number less than or equal to N/2 which is coprime to N
- Largest number less than or equal to Z that leaves a remainder X when divided by Y
- Find largest prime factor of a number
- Find unique pairs such that each element is less than or equal to N
- Print all Semi-Prime Numbers less than or equal to N
- Highest and Smallest power of K less than and greater than equal to N respectively
- Count pairs with sum as a prime number and less than n
- Sum of elements in 1st array such that number of elements less than or equal to them in 2nd array is maximum
- Find sum of a number and its maximum prime factor
- Sum of Maximum and Minimum prime factor of every number in the Array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Mithun Kumar

