Advertisement
Gamerkin

lab8

Dec 1st, 2023
786
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.21 KB | None | 0 0
  1. package lab8;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Scanner;
  6.  
  7. public class PrimeFactorization {
  8.     public static List<Integer> getPrimeFactors(int n) {
  9.         List<Integer> primeFactors = new ArrayList<>();
  10.         while (n % 2 == 0) {
  11.             primeFactors.add(2);
  12.             n /= 2;
  13.         }
  14.         for (int i = 3; i <= Math.sqrt(n); i = i + 2) {
  15.             while (n % i == 0) {
  16.                 primeFactors.add(i);
  17.                 n = n / i;
  18.             }
  19.         }
  20.         if (n > 2) {
  21.             primeFactors.add(n);
  22.         }
  23.         return primeFactors;
  24.     }
  25.  
  26.     public static void main(String[] args) {
  27.         Scanner scanner = new Scanner(System.in);
  28.         int number = scanner.nextInt();
  29.         List<Integer> primeFactors = getPrimeFactors(number);
  30.         System.out.println("Простые множители числа " + number + ": " + primeFactors);
  31.     }
  32. }
  33.  
  34.  
  35. package lab8;
  36.  
  37. import java.util.Scanner;
  38.  
  39. public class PalindromeCheck {
  40.     public static boolean isPalindrome(String s) {
  41.         if (s.length() <= 1) {
  42.             return true;
  43.         } else {
  44.             return s.charAt(0) == s.charAt(s.length() - 1) && isPalindrome(s.substring(1, s.length() - 1));
  45.         }
  46.     }
  47.  
  48.     public static void main(String[] args) {
  49.         Scanner scanner = new Scanner(System.in);
  50.         String word = scanner.nextLine();
  51.         if (isPalindrome(word)) {
  52.             System.out.println("YES");
  53.         } else {
  54.             System.out.println("NO");
  55.         }
  56.     }
  57. }
  58.  
  59.  
  60.  
  61. package lab8;
  62.  
  63. import java.util.Scanner;
  64.  
  65. public class BinarySequences {
  66.     public static int HowMany(int a, int b) {
  67.         if (a == 0){
  68.             return 1;
  69.         }
  70.         if (a == 1) {
  71.             return b + 1;
  72.         }
  73.         if (a > 1 & b == 0) {
  74.             return 0;
  75.         }
  76.         return HowMany(a - 1, b - 1) + HowMany(a, b - 1);
  77.     }
  78.  
  79.     public static void main(String[] args) {
  80.         Scanner scanner = new Scanner(System.in);
  81.         int a = scanner.nextInt();
  82.         int b = scanner.nextInt();
  83.         System.out.println("Количество последовательностей: " + HowMany(a, b));
  84.     }
  85. }
  86.  
  87.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement