borovaneca

PrimePairs

Dec 5th, 2022
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.09 KB | None | 0 0
  1. package PBMore.WhileLoop.NestedLoops;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class PrimePairs {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.  
  9.         int firstCouple = Integer.parseInt(scanner.nextLine());
  10.         int secondCouple = Integer.parseInt(scanner.nextLine());
  11.         int differenceFirst = Integer.parseInt(scanner.nextLine());
  12.         int differenceSecond = Integer.parseInt(scanner.nextLine());
  13.  
  14.         for (int i = firstCouple; i <= firstCouple + differenceFirst; i++) {
  15.             for (int j = secondCouple; j <= secondCouple + differenceSecond; j++) {
  16.                 if (isPrime(i) && isPrime(j)){
  17.                 System.out.printf("%d%d\n", i, j);
  18.             }
  19.         }
  20.     }
  21.  
  22. }
  23.  
  24.     static boolean isPrime(int n) {
  25.         if (n <= 1)
  26.             return false;
  27.         else if (n == 2)
  28.             return true;
  29.         else if (n % 2 == 0)
  30.             return false;
  31.         for (int i = 3; i <= Math.sqrt(n); i += 2) {
  32.             if (n % i == 0)
  33.                 return false;
  34.         }
  35.         return true;
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment