Last active
February 18, 2022 14:41
-
-
Save aadipoddar/0ff394e46c9b6b4a476c094041083e07 to your computer and use it in GitHub Desktop.
Print Prime Triplet Integers between a given range
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| A prime triplet is a collection of three Prime Numbers in the form | |
| (p, p + 2, p + 6) or (p, p + 4, p + 6) | |
| Write a program to input M and N where M < N and , M and N less than 50. | |
| Find and Display all the possible Prime Triplets in the range M and N | |
| where the value of M and N entered by the user. | |
| Eg: | |
| INPUT : | |
| M = 1 | |
| N = 20 | |
| OUTPUT: | |
| 5 7 11 | |
| 7 11 13 | |
| 11 13 17 | |
| 13 17 19 | |
| */ | |
| import java.util.*; | |
| class PrimeTriplet | |
| { | |
| boolean Validate(int first, int second) | |
| { | |
| if(first > second) | |
| { | |
| System.out.println("First Number is Greater than the Second one"); | |
| return false; | |
| } | |
| if(second >= 50) | |
| { | |
| System.out.println("Second one exceeds 50"); | |
| return false; | |
| } | |
| if(second >= 50) | |
| { | |
| System.out.println("Second one exceeds 50"); | |
| return false; | |
| } | |
| if(first < 1 || second < 1) | |
| { | |
| System.out.println("Range is less than 1"); | |
| return false; | |
| } | |
| if(first == second) | |
| { | |
| System.out.println("First number is same as second"); | |
| return false; | |
| } | |
| return true; | |
| } | |
| boolean IsPrime(int number) | |
| { | |
| for(int i = 2; i < number; i++) | |
| if(number % i == 0) | |
| return false; | |
| return true; | |
| } | |
| boolean IsTripletPrime(int number) | |
| { | |
| PrimeTriplet obj = new PrimeTriplet(); | |
| if(obj.IsPrime(number + 2)) | |
| { | |
| if(obj.IsPrime(number + 6)) | |
| { | |
| System.out.print(number + "\t" + (number + 2) + "\t" + (number + 6)); | |
| return true; | |
| } | |
| } | |
| else if(obj.IsPrime(number + 4)) | |
| { | |
| if(obj.IsPrime(number + 6)) | |
| { | |
| System.out.print(number + "\t" + (number + 4) + "\t" + (number + 6)); | |
| return true; | |
| } | |
| } | |
| return false; | |
| } | |
| public static void main() | |
| { | |
| Scanner sc = new Scanner(System.in); | |
| System.out.println("Enter the first number of the range"); | |
| int first = sc.nextInt(); | |
| System.out.println("Enter the second number of the range"); | |
| int second = sc.nextInt(); | |
| PrimeTriplet obj = new PrimeTriplet(); | |
| if(!obj.Validate(first, second)) | |
| return; | |
| for(int i = first + 1; i < second - 6; i++) | |
| if(obj.IsPrime(i)) | |
| if(obj.IsTripletPrime(i)) | |
| System.out.println(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment