给定两个正整数N和K ,任务是在每个操作中将N的值增加其最小除数超过N (超过1 )(正好是K次)后,找到N的值。
例子:
Input: N = 5, K = 2
Output: 12
Explanation:
Smallest divisor of N (= 5) is 5. Therefore, N = 5 + 5 = 10.
Smallest divisor of N (= 10) is 2. Therefore, N = 5 + 2 = 12.
Therefore, the required output is 12.
Input: N = 6, K = 4
Output: 14
简单方法:最简单的方法到在范围解决这个问题,以迭代[1,K]使用变量i并且在每个操作中,找出最小的除数大于N个1和由所述最小除数大于1递增N的值的N。最后,打印N的值。
下面是上述方法的实现:
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to find the smallest
// divisor of N greater than 1
int smallestDivisorGr1(int N)
{
for (int i = 2; i <= sqrt(N);
i++) {
// If i is a divisor
// of N
if (N % i == 0) {
return i;
}
}
// If N is a prime number
return N;
}
// Function to find the value of N by
// performing the operations K times
int findValOfNWithOperat(int N, int K)
{
// Iterate over the range [1, K]
for (int i = 1; i <= K; i++) {
// Update N
N += smallestDivisorGr1(N);
}
return N;
}
// Driver Code
int main()
{
int N = 6, K = 4;
cout << findValOfNWithOperat(N, K);
return 0;
}
Java
// Java program to implement
// the above approach
class GFG{
// Function to find the smallest
// divisor of N greater than 1
static int smallestDivisorGr1(int N)
{
for (int i = 2; i <= Math.sqrt(N);
i++) {
// If i is a divisor
// of N
if (N % i == 0) {
return i;
}
}
// If N is a prime number
return N;
}
// Function to find the value of N by
// performing the operations K times
static int findValOfNWithOperat(int N, int K)
{
// Iterate over the range [1, K]
for (int i = 1; i <= K; i++)
{
// Update N
N += smallestDivisorGr1(N);
}
return N;
}
// Driver Code
public static void main(String[] args)
{
int N = 6, K = 4;
System.out.print(findValOfNWithOperat(N, K));
}
}
// This code is contributed by shikhasingrajput
C#
// C# program to implement
// the above approach
using System;
public class GFG
{
// Function to find the smallest
// divisor of N greater than 1
static int smallestDivisorGr1(int N)
{
for (int i = 2; i <= Math.Sqrt(N);
i++) {
// If i is a divisor
// of N
if (N % i == 0) {
return i;
}
}
// If N is a prime number
return N;
}
// Function to find the value of N by
// performing the operations K times
static int findValOfNWithOperat(int N, int K)
{
// Iterate over the range [1, K]
for (int i = 1; i <= K; i++)
{
// Update N
N += smallestDivisorGr1(N);
}
return N;
}
// Driver Code
public static void Main(String[] args)
{
int N = 6, K = 4;
Console.Write(findValOfNWithOperat(N, K));
}
}
// This code is contributed by 29AjayKumar
Javascript
C++14
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to find the smallest
// divisor of N greater than 1
int smallestDivisorGr1(int N)
{
for (int i = 2; i <= sqrt(N);
i++) {
// If i is a divisor
// of N
if (N % i == 0) {
return i;
}
}
// If N is a prime number
return N;
}
// Function to find the value of N by
// performing the operations K times
int findValOfNWithOperat(int N, int K)
{
// If N is an even number
if (N % 2 == 0) {
// Update N
N += K * 2;
}
// If N is an odd number
else {
// Update N
N += smallestDivisorGr1(N)
+ (K - 1) * 2;
}
return N;
}
// Driver Code
int main()
{
int N = 6, K = 4;
cout << findValOfNWithOperat(N, K);
return 0;
}
输出:
14
时间复杂度: O(K *√N)
辅助空间: O(1)
高效方法:请按照以下步骤解决问题:
- 如果N是偶数,则将N的值更新为(N + K * 2) 。
- 否则,找到大于N的1的最小正除数,例如smDiv并将值N更新为(N + smDiv +(K – 1)* 2)
- 最后,打印N的值。
下面是上述方法的实现:
C++ 14
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to find the smallest
// divisor of N greater than 1
int smallestDivisorGr1(int N)
{
for (int i = 2; i <= sqrt(N);
i++) {
// If i is a divisor
// of N
if (N % i == 0) {
return i;
}
}
// If N is a prime number
return N;
}
// Function to find the value of N by
// performing the operations K times
int findValOfNWithOperat(int N, int K)
{
// If N is an even number
if (N % 2 == 0) {
// Update N
N += K * 2;
}
// If N is an odd number
else {
// Update N
N += smallestDivisorGr1(N)
+ (K - 1) * 2;
}
return N;
}
// Driver Code
int main()
{
int N = 6, K = 4;
cout << findValOfNWithOperat(N, K);
return 0;
}
输出:
14
时间复杂度: O(√N)
辅助空间: O(1)