Advertisement
Guest User

Untitled

a guest
Jan 20th, 2020
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.72 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. public static void Function1(TextField inText1, Label label)
  15. {
  16. String result = "";
  17. int n = Integer.parseInt(inText1.getText());
  18.  
  19. try
  20. {
  21. Integer.parseInt(inText1.getText());
  22. }
  23. catch (NumberFormatException e)
  24. {
  25. label.setText("All the integers must be grater than 1");
  26. }
  27.  
  28. if (n <= 1)
  29. label.setText("All the integers must be grater than 1");
  30. else
  31. {
  32. int countP = 0;
  33. boolean A[] = new boolean [n + 1];
  34. Arrays.fill(A,true);
  35.  
  36. for(int i = 2; i <= (int)Math.sqrt(n); i++)
  37. {
  38. if (A[i] == true)
  39. {
  40. for (int j = i * i; j <= n; j = j + i)
  41. {
  42. A[j]= false;
  43. }
  44. }
  45. }
  46.  
  47. for (int i = 2; i < A.length; i++)
  48. {
  49. if (A[i] == true)
  50. {
  51. result += i + "\n";
  52. countP++;
  53. }
  54.  
  55. count = countP;
  56. label.setText(result);
  57.  
  58. chronology += "Function type: Eratosthenes \n"
  59. + "Input: " + n + "\nResult: " + result;
  60. }
  61. }
  62. }
  63.  
  64.  
  65.  
  66. public static void Function2(TextField inText1, TextField inText2, Label label)
  67. {
  68. String result = "";
  69. int n = Integer.parseInt(inText1.getText());
  70. int m = Integer.parseInt(inText2.getText());
  71.  
  72. try
  73. {
  74. Integer.parseInt(inText1.getText());
  75. Integer.parseInt(inText2.getText());
  76. }
  77. catch (NumberFormatException e)
  78. {
  79. label.setText("All the integers must be grater than 0");
  80. }
  81.  
  82. if (n <= 0 || m <= 0)
  83. {
  84. label.setText("All the integers must be grater than 0");
  85. }
  86. else
  87. {
  88. while (n != m)
  89. {
  90. if (n > m)
  91. n = n - m;
  92. else
  93. m = m - n;
  94. }
  95.  
  96. gcd = n;
  97. result = Integer.toString(n);
  98. label.setText(result);
  99.  
  100. chronology += "Function type: Euclid algorithm \n"
  101. + "Input: " + n + "," + m + "\nResult: " + result;
  102. }
  103. }
  104.  
  105.  
  106.  
  107. public static void Function3(TextField inText1, Label label)
  108. {
  109. String result = "";
  110. int n = Integer.parseInt(inText1.getText());
  111.  
  112. try
  113. {
  114. Integer.parseInt(inText1.getText());
  115. }
  116. catch (NumberFormatException e)
  117. {
  118. label.setText("All the integers must be grater than 0");
  119. }
  120.  
  121. int countP = 0;
  122. for (int i = 2; i <= n; i++)
  123. {
  124. if(n % i == 0)
  125. {
  126. result += i + "\n";
  127. countP++;
  128. }
  129. }
  130.  
  131. count = countP;
  132. result = Integer.toString(count);
  133. label.setText(result);
  134.  
  135. chronology += "Function type: Number of prime numbers\n"
  136. + "Input: " + n + "\nResult: " + result;
  137. }
  138.  
  139.  
  140.  
  141. public static void Function4(TextField inText1, Label label)
  142. {
  143. int n = Integer.parseInt(inText1.getText());
  144. try
  145. {
  146. Integer.parseInt(inText1.getText());
  147. }
  148. catch (NumberFormatException e)
  149. {
  150. label.setText("All the integers must be grater than 0");
  151. }
  152. if (n <= 0)
  153. label.setText("All the integers must be grater than 0");
  154. else
  155. {
  156. int countCoprime = 0;
  157. TextField inText2 = new TextField();
  158. for(int i = n; 0< 1; i--)
  159. {
  160. inText2.setText(Integer.toString(i));
  161. Functions.Function2(inText1, inText2, label);
  162. if (gcd == 1)
  163. {
  164. countCoprime++;
  165. }
  166. }
  167.  
  168. label.setText(Integer.toString(countCoprime));
  169.  
  170. chronology += "Function type: Eulerโ€™s totient function\n"
  171. + "Input: " + inText1.getText() + "\nResult: " + countCoprime;
  172. }
  173. }
  174.  
  175.  
  176.  
  177. public static void Function5(TextField inText1, Label label)
  178. {
  179. String result = "";
  180. int n = Integer.parseInt(inText1.getText());
  181.  
  182. try
  183. {
  184. Integer.parseInt(inText1.getText());
  185. }
  186. catch (NumberFormatException e)
  187. {
  188. label.setText("All the integers must be grater than 1");
  189. }
  190.  
  191. for(int i = 2; i< n; i++) {
  192. while(n % i == 0) {
  193. System.out.println(i+" ");
  194. n = n/i;
  195. }
  196. }
  197.  
  198. result = Integer.toString(n);
  199. label.setText(result);
  200.  
  201. chronology += "Function type: Prime factorization\n"
  202. + "Input: " + n + "\nResult: " + result;
  203. }
  204.  
  205.  
  206.  
  207. public static void Function6(TextField inText1, TextField inText2, Label label)
  208. {
  209. int n = Integer.parseInt(inText2.getText());
  210. int result = 0;
  211. int xpow;
  212.  
  213. try
  214. {
  215. Integer.parseInt(inText1.getText());
  216. Integer.parseInt(inText2.getText());
  217. }
  218. catch (NumberFormatException e)
  219. {
  220. label.setText("All the integers must be positive numbers"
  221. + " with n > 1 and x >= 0");
  222. }
  223.  
  224. for (int i = 1; i <= n; i++)
  225. {
  226. if (n % i == 0)
  227. {
  228. xpow = (int) Math.pow(i, Integer.parseInt(inText1.getText()));
  229. result += xpow;
  230. }
  231. }
  232.  
  233. label.setText(Integer.toString(result));
  234.  
  235. chronology += "Function type: Sigma(x, n) function\n"
  236. + "Input: " + inText1.getText() + ","
  237. + inText2.getText() + "\nResult: " + result;
  238. }
  239.  
  240.  
  241.  
  242. public static void Function7(TextField inText1, TextField inText2,
  243. TextField inText3,TextField inText4, Label label)
  244. {
  245. String randomnum = "";
  246. int a = Integer.parseInt(inText1.getText());
  247. int b = Integer.parseInt(inText2.getText());
  248. int m = Integer.parseInt(inText3.getText());
  249. int n = Integer.parseInt(inText1.getText());
  250.  
  251. try
  252. {
  253. Integer.parseInt(inText1.getText());
  254. Integer.parseInt(inText2.getText());
  255. Integer.parseInt(inText3.getText());
  256. Integer.parseInt(inText4.getText());
  257. }
  258. catch (NumberFormatException e)
  259. {
  260. label.setText("All the integers are acceptable");
  261. }
  262.  
  263. int randomArray [] = new int [n];
  264.  
  265. for(int i = 0; i < n; i++)
  266. {
  267. if(i == 0)
  268. {
  269. randomArray[i] = b % m;
  270. }
  271. else
  272. randomArray [i] = (a * randomArray[i - 1] + b) % m;
  273. }
  274.  
  275. for(int count = 0; count < randomArray.length; count++)
  276. {
  277. randomnum += randomArray[count] + "";
  278. }
  279.  
  280. label.setText(randomnum);
  281.  
  282. chronology += "Function type: Linear congruential generator\n"
  283. + "Input: " + a + "," + b + "," + m + ","
  284. + n + "\nResult: " + randomnum;
  285. }
  286.  
  287.  
  288.  
  289. public static void Function8(TextField inText1, Label label)
  290. {
  291. int n = Integer.parseInt(inText1.getText());
  292. int result;
  293.  
  294. try
  295. {
  296. Integer.parseInt(inText1.getText());
  297. }
  298. catch (NumberFormatException e)
  299. {
  300. label.setText("All the integers must be grater than 0");
  301. }
  302.  
  303. if(n <= 0)
  304. label.setText("All the integers must be grater than 0");
  305. else
  306. {
  307. result = (int)((1 / (4 * n * Math.sqrt(3))) * (Math.pow(Math.E, (Math.PI * Math.sqrt((2 * n)/3)))));
  308. label.setText(Integer.toString(result));
  309.  
  310. chronology += "Function type: Partition function p(n) of n\n"
  311. + "Input: " + n + "\nResult: " + result;
  312. }
  313. }
  314.  
  315. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement