📜  给定一个数x,找到y使得x * y + 1不是素数

📅  最后修改于: 2021-04-27 19:47:40             🧑  作者: Mango

给定数字x,求y(y> 0),使得x * y + 1不是素数。

例子:

Input : 2
Output : 4

Input : 5
Output : 3

观察:

方法 :

For x > 2, y will be x-2 otherwise y will be x+2
C++
#include 
using namespace std;
  
int findY(int x)
{
    if (x > 2)
        return x - 2;
  
    return x + 2;
}
  
// Driver code
int main()
{
    int x = 5;
    cout << findY(x);
    return 0;
}


Java
// JAVA implementation of above approach
  
import java.util.*;
  
class GFG
{
    public static int findY(int x)
    {
        if (x > 2)
            return x - 2;
      
        return x + 2;
    }
  
    // Driver code
    public static void  main(String [] args)
    {
        int x = 5;
        System.out.println(findY(x));
  
    }
      
}
  
  
// This code is contributed
// by ihritik


Python3
# Python3 implementation of above 
# approach
  
def findY(x):
  
    if (x > 2):
        return x - 2
      
    return x + 2
  
# Driver code
if __name__=='__main__':
    x = 5
    print(findY(x))
  
# This code is contributed
# by ihritik


C#
// C# implementation of above approach
using System;
  
class GFG
{
public static int findY(int x)
{
    if (x > 2)
        return x - 2;
  
    return x + 2;
}
  
// Driver code
public static void Main()
{
    int x = 5;
    Console.WriteLine(findY(x));
}
}
  
// This code is contributed
// by Subhadeep


PHP
 2) 
        return $x - 2; 
  
    return $x + 2; 
} 
  
// Driver code 
$x = 5; 
echo (findY($x)); 
  
// This code is contributed
// by Shivi_Aggarwal
?>


输出 :

3