Given a number x , find y such that x*y + 1 is not a prime
Last Updated :
22 Aug, 2022
Given a number x, Find y (y > 0) such that x*y + 1 is not a prime.
Examples:
Input : 2
Output : 4
Input : 5
Output : 3
Observation:
x*(x-2) + 1 = (x-1)^2 which is not a prime.
Approach:
For x > 2, y will be x-2 otherwise y will be x+2
C++
#include <bits/stdc++.h>
using namespace std;
int findY( int x)
{
if (x > 2)
return x - 2;
return x + 2;
}
int main()
{
int x = 5;
cout << findY(x);
return 0;
}
|
C
#include <stdio.h>
int findY( int x)
{
if (x > 2)
return x - 2;
return x + 2;
}
int main()
{
int x = 5;
printf ( "%d" ,findY(x));
return 0;
}
|
Java
import java.util.*;
class GFG
{
public static int findY( int x)
{
if (x > 2 )
return x - 2 ;
return x + 2 ;
}
public static void main(String [] args)
{
int x = 5 ;
System.out.println(findY(x));
}
}
|
Python3
def findY(x):
if (x > 2 ):
return x - 2
return x + 2
if __name__ = = '__main__' :
x = 5
print (findY(x))
|
C#
using System;
class GFG
{
public static int findY( int x)
{
if (x > 2)
return x - 2;
return x + 2;
}
public static void Main()
{
int x = 5;
Console.WriteLine(findY(x));
}
}
|
PHP
<?php
function findY( $x )
{
if ( $x > 2)
return $x - 2;
return $x + 2;
}
$x = 5;
echo (findY( $x ));
?>
|
Javascript
<script>
function findY(x)
{
if (x > 2)
return x - 2;
return x + 2;
}
var x = 5;
document.write(findY(x));
</script>
|
Output:
3
Time Complexity: O(1)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...