📜  K重复添加N的最小除数后形成的数字

📅  最后修改于: 2021-04-24 05:47:28             🧑  作者: Mango

给定两个整数NK ,任务是生成执行K次运算的最终结果,其中涉及在每一步中将N的当前值除1以外的最小除数
例子:

天真的方法:蛮力解决此问题的方法是执行K次操作,然后打印最终数字。
高效方法:这里的技巧是,如果给定的N偶数,则所有K个运算的最小除数始终为2。因此,所需的K个数将很简单

同样,如果N为奇数,则最小除数将为奇数。因此,将它们相加将得出一个偶数值(奇数+奇数=偶数)。因此,现在可以将上述技巧应用于(K-1)操作,即

下面是上述方法的实现:

C++
// C++ program to find the Kth number
// formed after repeated addition of
// smallest divisor of N
#include
#include 
using namespace std;
 
void FindValue(int N, int K)
{
     
    // If N is even
    if( N % 2 == 0 )
    {
        N = N + 2 * K;
    }
     
    // If N is odd
    else
    {
        int i;
         
        for(i = 2; i < sqrt(N) + 1; i++)
        {
           if(N % i == 0)
              break;
        }
             
        // Add smallest divisor to N
        N = N + i;
         
        // Updated N is even
        N = N + 2 * ( K - 1 );
    }
    cout << N << endl;
}
     
// Driver code
int main()
{
    int N = 9;
    int K = 4;
     
    FindValue( N, K );
}
     
// This code is contributed by Surendra_Gangwar


Java
// Java program to find the Kth number
// formed after repeated addition of
// smallest divisor of N
import java.util.*;
class GFG{
 
static void FindValue(int N, int K)
{
     
    // If N is even
    if( N % 2 == 0 )
    {
        N = N + 2 * K;
    }
     
    // If N is odd
    else
    {
        int i;
         
        for(i = 2; i < Math.sqrt(N) + 1; i++)
        {
            if(N % i == 0)
                break;
        }
             
        // Add smallest divisor to N
        N = N + i;
         
        // Updated N is even
        N = N + 2 * ( K - 1 );
    }
    System.out.print(N);
}
     
// Driver code
public static void main(String args[])
{
    int N = 9;
    int K = 4;
     
    FindValue( N, K );
}
}
 
// This code is contributed by Nidhi_biet


Python3
# Python3 program to find the
# Kth number formed after
# repeated addition of
# smallest divisor of N
 
import math
 
def FindValue(N, K):
     
    # If N is even
    if( N % 2 == 0 ):
        N = N + 2 * K
     
    # If N is odd
    else:
         
        # Find the smallest divisor
        for i in range( 2, (int)(math.sqrt(N))+1 ):
            if( N % i == 0):
                break
             
        # Add smallest divisor to N
        N = N + i
         
        # Updated N is even
        N = N + 2 * ( K - 1 )
 
    print(N)
 
# Driver code
if __name__ == "__main__":
    N = 9
    K = 4
    FindValue( N, K )


C#
// C# program to find the Kth number
// formed after repeated addition of
// smallest divisor of N
using System;
class GFG{
 
static void FindValue(int N, int K)
{
     
    // If N is even
    if( N % 2 == 0 )
    {
        N = N + 2 * K;
    }
     
    // If N is odd
    else
    {
        int i;
         
        for(i = 2; i < Math.Sqrt(N) + 1; i++)
        {
            if(N % i == 0)
                break;
        }
             
        // Add smallest divisor to N
        N = N + i;
         
        // Updated N is even
        N = N + 2 * ( K - 1 );
    }
    Console.WriteLine(N);
}
     
// Driver code
public static void Main()
{
    int N = 9;
    int K = 4;
     
    FindValue( N, K );
}
}
 
// This code is contributed by Code_Mech


Javascript


输出:
18

时间复杂度: O(√N)