Find sum of a number and its maximum prime factor Last Updated : 08 Jul, 2022 Comments Improve Suggest changes Like Article Like Report Given an integer N, the task is to find the sum of N and it's maximum prime factor.Examples: Input: 19 Output: 38 Maximum prime factor of 19 is 19. Hence, 19 + 19 = 38Input: 8 Output: 10 8 + 2 = 10 Approach: Find the largest prime factor of the number and store it in maxPrimeFact then print the value of N + maxPrimeFact.Below is the implementation of the above approach: C++ // C++ program to find sum of n and // it's largest prime factor #include <cmath> #include <iostream> using namespace std; // Function to return the sum of n and // it's largest prime factor int maxPrimeFactors(int n) { int num = n; // Initialise maxPrime to -1. int maxPrime = -1; while (n % 2 == 0) { maxPrime = 2; n /= 2; } // n must be odd at this point, thus skip // the even numbers and iterate only odd numbers for (int i = 3; i <= sqrt(n); i += 2) { while (n % i == 0) { maxPrime = i; n = n / i; } } // This condition is to handle the case // when n is a prime number greater than 2 if (n > 2) maxPrime = n; // finally return the sum. int sum = maxPrime + num; return sum; } // Driver Program to check the above function. int main() { int n = 19; cout << maxPrimeFactors(n); return 0; } Java // Java program to find sum of n and // it's largest prime factor import java.io.*; class GFG { // Function to return the sum of n // and it's largest prime factor static int maxPrimeFactors(int n) { int num = n; // Initialise maxPrime to -1. int maxPrime = -1; while (n % 2 == 0) { maxPrime = 2; n /= 2; } // n must be odd at this point, // thus skip the even numbers and // iterate only odd numbers for (int i = 3; i <= Math.sqrt(n); i += 2) { while (n % i == 0) { maxPrime = i; n = n / i; } } // This condition is to handle the case // when n is a prime number greater than 2 if (n > 2) { maxPrime = n; } // finally return the sum. int sum = maxPrime + num; return sum; } // Driver Code public static void main (String[] args) { int n = 19; System.out.println(maxPrimeFactors(n)); } } // This code is contributed by anuj_67 Python3 # Python 3 program to find sum of n and # it's largest prime factor from math import sqrt # Function to return the sum of n and # it's largest prime factor def maxPrimeFactors(n): num = n # Initialise maxPrime to -1. maxPrime = -1; while (n % 2 == 0): maxPrime = 2 n = n / 2 # n must be odd at this point, thus skip # the even numbers and iterate only odd numbers p = int(sqrt(n) + 1) for i in range(3, p, 2): while (n % i == 0): maxPrime = i n = n / i # This condition is to handle the case # when n is a prime number greater than 2 if (n > 2): maxPrime = n # finally return the sum. sum = maxPrime + num return sum # Driver Code if __name__ == '__main__': n = 19 print(maxPrimeFactors(n)) # This code is contributed by # Surendra_Gangwar C# // C# program to find sum of n and // it's largest prime factor using System; class GFG { // Function to return the sum of n // and it's largest prime factor static int maxPrimeFactors(int n) { int num = n; // Initialise maxPrime to -1. int maxPrime = -1; while (n % 2 == 0) { maxPrime = 2; n /= 2; } // n must be odd at this point, // thus skip the even numbers and // iterate only odd numbers for (int i = 3; i <= Math.Sqrt(n); i += 2) { while (n % i == 0) { maxPrime = i; n = n / i; } } // This condition is to handle the case // when n is a prime number greater than 2 if (n > 2) { maxPrime = n; } // finally return the sum. int sum = maxPrime + num; return sum; } // Driver Code static void Main () { int n = 19; Console.WriteLine(maxPrimeFactors(n)); } } // This code is contributed by Ryuga PHP <?php // PHP program to find sum of n and // it's largest prime factor // Function to return the sum of n // and it's largest prime factor function maxPrimeFactors($n) { $num = $n; // Initialise maxPrime to -1. $maxPrime = -1; while ($n % 2 == 0) { $maxPrime = 2; $n /= 2; } // n must be odd at this point, // thus skip the even numbers // and iterate only odd numbers for ($i = 3; $i <= sqrt($n); $i += 2) { while ($n % $i == 0) { $maxPrime = $i; $n = $n / $i; } } // This condition is to handle the case // when n is a prime number greater than 2 if ($n > 2) $maxPrime = $n; // finally return the sum. $sum = $maxPrime + $num; return $sum; } // Driver Code $n = 19; echo maxPrimeFactors($n); // This code is contributed // by inder_verma ?> JavaScript <script> // Javascript program to find sum of n and // it's largest prime factor // Function to return the sum of n // and it's largest prime factor function maxPrimeFactors(n) { var num = n; // Initialise maxPrime to -1. var maxPrime = -1; while (n % 2 == 0) { maxPrime = 2; n = parseInt(n/2); } // n must be odd at this point, // thus skip the even numbers and // iterate only odd numbers for (var i = 3; i <= parseInt(Math.sqrt(n)); i += 2) { while (n % i == 0) { maxPrime = i; n = parseInt(n / i); } } // This condition is to handle the case // when n is a prime number greater than 2 if (n > 2) { maxPrime = n; } // finally return the sum. var sum = maxPrime + num; return sum; } // Driver Code var n = 19; document.write(maxPrimeFactors(n)); // This code contributed by shikhasingrajput </script> Output: 38 Time Complexity: O(sqrtn*logn) Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Find sum of a number and its maximum prime factor Anonymous Improve Article Tags : Interview Experiences Mathematical Competitive Programming C++ DSA Experiences +2 More Practice Tags : CPPMathematical Similar Reads Maximum number of unique prime factors Given a number N, find the maximum number of unique prime factors any number can have in range [1, N].Examples: Input : N = 500 Output : 4 The maximum number of prime factors for any number in [1, 500] is 4. A number in range that has 4 prime factors is 210 (2 x 3 x 5 x 7) Input : N = 3 Output : 1 I 10 min read Find maximum power of a number that divides a factorial Given two numbers, fact and n, find the largest power of n that divides fact! (Factorial of fact). Examples:Â Input : fact = 5, n = 2 Output : 3 Explanation: Value of 5! is 120. The largest power of 2 that divides 120 is 8 (or 23 Input : fact = 146, n = 15 Output : 35 The idea is based on Legendreâs 12 min read Find largest prime factor of a number Given a positive integer n ( 1 <= n <= 1015). Find the largest prime factor of a number. Input: 6Output: 3Explanation Prime factor are 2 and 3. Largest of them is 3.Input: 15Output: 5Explanation: Prime factors are 3 and 5. The largest of them is 5.Input: 28Output: 7Explanation: Prime factors a 10 min read Number with maximum number of prime factors Given an integer N. The task is to find a number that is smaller than or equal to N and has maximum prime factors. In case there are two or more numbers with the same maximum number of prime factors, find the smallest of all.Examples: Input : N = 10Output : 6Number of prime factor of:1 : 02 : 13 : 1 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 N-th prime factor of a given number Given Q queries which consist of two integers, one is number(1 <= number <= 106) and the other is N., the task is to find the N-th prime factor of the given number. Examples: Input: Number of Queries, Q = 4 number = 6, N = 1 number = 210, N = 3 number = 210, N = 2 number = 60, N = 2Output: 2 5 15 min read k-th prime factor of a given number Given two numbers n and k, print k-th prime factor among all prime factors of n. For example, if the input number is 15 and k is 2, then output should be "5". And if the k is 3, then output should be "-1" (there are less than k prime factors). Examples: Input : n = 225, k = 2 Output : 3 Prime factor 15+ min read Print all prime factors of a given number Given a number n, the task is to find all prime factors of n.Examples:Input: n = 24Output: 2 2 2 3Explanation: The prime factorization of 24 is 23Ã3.Input: n = 13195Output: 5 7 13 29Explanation: The prime factorization of 13195 is 5Ã7Ã13Ã29.Approach:Every composite number has at least one prime fact 6 min read Prime factors of a big number Given a number N, print all the prime factors and their powers. Here N <= 10^18Examples : Input : 250 Output : 2 1 5 3Explanation: The prime factors of 250 are 2and 5. 2 appears once in the prime factorization of and 5 is thrice in it. Input : 1000000000000000000Output : 2 18 5 18Explanation: The 7 min read Maximum number of prime factors a number can have with exactly x factors Given an integer X, denoting the number of factors of a positive integer N can have. The task is to find the maximum number of distinct prime factors the number N can have. Examples: Input: X = 9 Output: 2 Explanation: Some of the possible numbers having 9 factors are: 256: 1, 2, 4, 8, 16, 32, 64, 1 6 min read Like