Find sum of a number and its maximum prime factor

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 = 38



Input: 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


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#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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
?>

chevron_right


Output:

38


My Personal Notes arrow_drop_up

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.