Given a number x , find y such that x*y + 1 is not a prime
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; } // Driver code int main() { int x = 5; cout << findY(x); return 0; } |
chevron_right
filter_none
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 |
chevron_right
filter_none
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 |
chevron_right
filter_none
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 |
chevron_right
filter_none
PHP
<?php // PHP implementation of above approach function findY($x) { if ($x > 2) return $x - 2; return $x + 2; } // Driver code $x = 5; echo (findY($x)); // This code is contributed // by Shivi_Aggarwal ?> |
chevron_right
filter_none
Output :
3
Recommended Posts:
- Find coordinates of a prime number in a Prime Spiral
- Program to find the Nth Prime Number
- Program to find the next prime number
- Find largest prime factor of a number
- Find prime number K in an array such that (A[i] % K) is maximum
- Find sum of a number and its maximum prime factor
- Analysis of Different Methods to find Prime Number in Python
- Find Largest Special Prime which is less than or equal to a given number
- Find a sequence of N prime numbers whose sum is a composite number
- Find the number of primitive roots modulo prime
- Find third number such that sum of all three number becomes prime
- Quick ways to check for Prime and find next Prime in Java
- Count occurrences of a prime number in the prime factorization of every element from the given range
- Print the nearest prime number formed by adding prime numbers to N
- Check if a prime number can be expressed as sum of two Prime Numbers
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



