Advertisement
Guest User

Untitled

a guest
Jan 20th, 2020
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.02 KB | None | 0 0
  1. package pack;
  2. import javafx.scene.control.TextField;
  3. import java.lang.Math;
  4. import java.util.Arrays;
  5. import javafx.scene.control.Label;
  6.  
  7. public class Functions
  8. {
  9.  
  10. static String chronology = "";
  11. static int count = 0;
  12. static int gcd = 0;
  13.  
  14.  
  15. public static void Function1(TextField inText1, Label label)
  16. {
  17. String result = "";
  18. int n = Integer.parseInt(inText1.getText());
  19.  
  20. if (n <= 1)
  21. label.setText("All the integers must be greater than 1");
  22. else
  23. {
  24. int countP = 0;
  25. boolean A[] = new boolean [n + 1];
  26. Arrays.fill(A,true);
  27.  
  28. for(int i = 2; i <= (int)Math.sqrt(n); i++)
  29. {
  30. if (A[i] == true)
  31. {
  32. for (int j = i * i; j <= n; j = j + i)
  33. {
  34. A[j]= false;
  35. }
  36. }
  37. }
  38.  
  39. result += "[";
  40. for (int i = 2; i < A.length; i++)
  41. {
  42.  
  43. if (A[i] == true)
  44. {
  45. result += i + ",";
  46. countP++;
  47. }
  48.  
  49. count = countP;
  50.  
  51. }
  52. result += "]";
  53. label.setText(result);
  54. chronology += "Function type: Eratosthenes \n"
  55. + "Input: " + n + "\nResult: " + result;
  56. }
  57. }
  58.  
  59.  
  60.  
  61.  
  62. public static void Function2(TextField inText1, TextField inText2, Label label) {
  63. // TODO Auto-generated method stub
  64.  
  65. String result = "";
  66. int n = Integer.parseInt(inText1.getText());
  67. int m = Integer.parseInt(inText2.getText());
  68.  
  69. if (n <= 0 || m <= 0)
  70. {
  71. label.setText("All the integers must be greater than 0");
  72. }
  73. else
  74. {
  75. while (n != m)
  76. {
  77. if (n > m)
  78. n = n - m;
  79. else
  80. m = m - n;
  81. }
  82.  
  83. gcd = n;
  84. result = Integer.toString(n);
  85. label.setText(result);
  86.  
  87. chronology += "Function type: Euclid algorithm \n"
  88. + "Input: " + n + "," + m + "\nResult: " + result;
  89. }
  90. }
  91.  
  92.  
  93.  
  94. public static void Function3(TextField inText1, Label label)
  95. {
  96. String result = "";
  97. int n = Integer.parseInt(inText1.getText());
  98.  
  99. int countP = 0;
  100. for (int i = 2; i <= n; i++)
  101. {
  102. if(n % i == 0)
  103. {
  104. result += i + "\n";
  105. countP++;
  106. }
  107. }
  108.  
  109. count = countP;
  110. result = Integer.toString(count);
  111. label.setText(result);
  112.  
  113. chronology += "Function type: Number of prime numbers\n"
  114. + "Input: " + n + "\nResult: " + result;
  115. }
  116.  
  117.  
  118.  
  119. public static void Function4(TextField inText1, Label label)
  120. {
  121. int n = Integer.parseInt(inText1.getText());
  122.  
  123. if (n <= 0)
  124. label.setText("All the integers must be greater than 0");
  125. else
  126. {
  127. int countCoprime = 0;
  128. TextField inText2 = new TextField();
  129.  
  130.  
  131. for(int i = n; n > 1; i--)
  132. {
  133. inText2.setText(Integer.toString(i));
  134. Functions.Function2(inText1, inText2, label);
  135. if (gcd == 1)
  136. {
  137. countCoprime++;
  138. }
  139. }
  140.  
  141. label.setText(Integer.toString(countCoprime));
  142.  
  143. chronology += "Function type: Euler’s totient function\n"
  144. + "Input: " + inText1.getText() + "\nResult: " + countCoprime;
  145. }
  146. }
  147.  
  148.  
  149.  
  150. public static void Function5(TextField inText1, Label label)
  151. {
  152. String result = "";
  153. int n = Integer.parseInt(inText1.getText());
  154.  
  155. for(int i = 2; i< n; i++) {
  156. while(n % i == 0) {
  157. result += n + ",";
  158. n = n/i;
  159. }
  160. }
  161.  
  162. label.setText(result);
  163.  
  164. chronology += "Function type: Prime factorization\n"
  165. + "Input: " + n + "\nResult: " + result;
  166. }
  167.  
  168.  
  169.  
  170. public static void Function6(TextField inText1, TextField inText2, Label label)
  171. {
  172. int n = Integer.parseInt(inText2.getText());
  173. int result = 0;
  174. int xpow;
  175.  
  176. if (n < 1 || Integer.parseInt(inText1.getText()) <= 0)
  177. label.setText("All the integers must be positive numbers"
  178. + " with n > 1 and x >= 0");
  179. else
  180. {
  181. for (int i = 1; i <= n; i++)
  182. {
  183. if (n % i == 0)
  184. {
  185. xpow = (int) Math.pow(i, Integer.parseInt(inText1.getText()));
  186. result += xpow;
  187. }
  188. }
  189.  
  190. label.setText(Integer.toString(result));
  191.  
  192. chronology += "Function type: Sigma(x, n) function\n"
  193. + "Input: " + inText1.getText() + ","
  194. + inText2.getText() + "\nResult: " + result;
  195. }
  196. }
  197.  
  198.  
  199.  
  200. public static void Function7(TextField inText1, TextField inText2,
  201. TextField inText3,TextField inText4, Label label)
  202. {
  203. String randomnum = "";
  204. int a = Integer.parseInt(inText1.getText());
  205. int b = Integer.parseInt(inText2.getText());
  206. int m = Integer.parseInt(inText3.getText());
  207. int n = Integer.parseInt(inText1.getText());
  208.  
  209. int randomArray [] = new int [n];
  210.  
  211. for(int i = 0; i < n; i++)
  212. {
  213. if(i == 0)
  214. {
  215. randomArray[i] = b % m;
  216. }
  217. else
  218. randomArray [i] = (a * randomArray[i - 1] + b) % m;
  219. }
  220.  
  221. for(int count = 0; count < randomArray.length; count++)
  222. {
  223. randomnum += randomArray[count] + "";
  224. }
  225. result = i;
  226. label.setText(randomnum);
  227.  
  228. chronology += "Function type: Linear congruential generator\n"
  229. + "Input: " + a + "," + b + "," + m + ","
  230. + n + "\nResult: " + randomnum;
  231. }
  232.  
  233.  
  234.  
  235. public static void Function8(TextField inText1, Label label)
  236. {
  237. int n = Integer.parseInt(inText1.getText());
  238. int result;
  239.  
  240. if(n <= 0)
  241. label.setText("All the integers must be greater than 0");
  242. else
  243. {
  244. result = (int)((1 / (4 * n * Math.sqrt(3))) * (Math.pow(Math.E, (Math.PI * Math.sqrt((2 * n)/3)))));
  245. label.setText(Integer.toString(result));
  246.  
  247. chronology += "Function type: Partition function p(n) of n\n"
  248. + "Input: " + n + "\nResult: " + result;
  249. }
  250. }
  251.  
  252.  
  253. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement