Advertisement
Guest User

class2

a guest
Jan 20th, 2020
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.04 KB | None | 0 0
  1. import java.util.ArrayList;
  2.  
  3. public class SupportMethods {
  4.     static int sumParts = 0;
  5.  
  6.     public static void main(String[] args) {
  7.  
  8.         System.out.println(SupportMethods.gcd(72, 9));
  9.         System.out.println(SupportMethods.lcg(1, 2, 3, 4));
  10.         System.out.println(SupportMethods.randomList(1, 2, 3, 4));
  11.         System.out.println(SupportMethods.factors(14));
  12. //      SupportMethods.createParts(8);
  13. //      System.out.println(sumParts);
  14.     }
  15.  
  16.     public static int gcd(int n1, int n2) {
  17.         if (n2 == 0)
  18.             return n1;
  19.         int r = n1 % n2;
  20.         return gcd(n1, r);
  21.     }
  22.  
  23.     public static int lcg(int a, int b, int m, int n) {
  24.         if (n == 0) {
  25.             return 11;
  26.         }
  27.         return (((a * SupportMethods.lcg(a, b, m, n - 1)) + b) % m);
  28.     }
  29.  
  30.     public static ArrayList<Integer> factors(int b) {
  31.         ArrayList<Integer> factors = new ArrayList<Integer>();
  32.         for (int i = 1; i <= b; i++) {
  33.             if (b % i == 0) {
  34.                 factors.add(i);
  35.  
  36.             }
  37.         }
  38.         return (factors);
  39.     }
  40.  
  41.     public static ArrayList<Integer> randomList(int a, int b, int m, int n) {
  42.         ArrayList<Integer> randNums = new ArrayList<Integer>();
  43.         // int x = 0;
  44.         if (n == 0) {
  45.             randNums.add(11);
  46.         }
  47.         for (int i = 1; i <= n; i++) {
  48.             int x = SupportMethods.lcg(a, b, m, i);
  49.             randNums.add(x);
  50.         }
  51.         return (randNums);
  52.     }
  53.  
  54.     static boolean isPrime(int n) {
  55.         if (n == 2) {
  56.             return true;
  57.         }
  58.         if (n % 2 == 0) {
  59.             return false;
  60.         }
  61.         for (int i = 3; i * i <= n; i += 2) {
  62.             if (n % i == 0)
  63.                 return false;
  64.         }
  65.  
  66.         return true;
  67.     }
  68.  
  69. //  public static void createParts(int n) {
  70. //      int index = 0;
  71. //      int[] parts = new int[n];
  72. //      parts[index] = n;
  73. //
  74. //      while (true) {
  75. //          sumParts = sumParts + 1;
  76. //
  77. //          int remain = 0;
  78. //          while (index >= 0 && parts[index] == 1) {
  79. //              remain += parts[index];
  80. //              index--;
  81. //          }
  82. //
  83. //          if (index < 0)
  84. //              return;
  85. //
  86. //          parts[index]--;
  87. //          remain++;
  88. //
  89. //          while (remain > parts[index]) {
  90. //              parts[index + 1] = parts[index];
  91. //              remain = remain - parts[index];
  92. //              index++;
  93. //          }
  94. //
  95. //          parts[index + 1] = remain;
  96. //          index++;
  97. //      }
  98. //  }
  99.  
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement