📜  查找一个数字的和及其最大素数

📅  最后修改于: 2021-06-26 18:27:54             🧑  作者: Mango

给定整数N ,任务是找到N的和及其最大素数。
例子:

方法:找到数字的最大质数并将其存储在maxPrimeFact中,然后打印N + maxPrimeFact的值。
下面是上述方法的实现:

C++
// C++ program to find sum of n and
// it's largest prime factor
#include 
#include 
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
 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


输出:
38

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。