Guest User

everything.java

a guest
Dec 21st, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 44.18 KB | None | 0 0
  1. import java.util.*;
  2. import java.math.*;
  3.  
  4. public class everything{
  5. //Buffer for formatting the factor tree String
  6. private static int buffer = 0;
  7.  
  8. //If true, show the related information
  9. private static boolean factorc = false;
  10. private static boolean everything = false;
  11. private static boolean allforc = false;
  12. private static boolean allforab = false;
  13.  
  14. private static boolean showdef = false;
  15. private static boolean showrootofd = false;
  16. private static boolean showbign = false;
  17. private static boolean showatbign = false;
  18. private static boolean showatcbign = false;
  19. private static boolean showatcshad = false;
  20. private static boolean showxx2f1 = false;
  21. private static boolean showxx2d1 = false;
  22. private static boolean showxxbign = false;
  23. private static boolean showxxcbign = false;
  24. private static boolean showxxcshad = false;
  25.  
  26. private static boolean showen = false;
  27. private static boolean showa1b1 = false;
  28. private static boolean showxpnxpn = false;
  29. private static boolean show2xpnxpn = false;
  30. private static boolean show0naabb = false;
  31. private static boolean show0nabab = false;
  32. private static boolean show0nabba = false;
  33. private static boolean show0nbaab = false;
  34. private static boolean show0n1cc = false;
  35. private static boolean show11d = false;
  36. private static boolean show01a = false;
  37. private static boolean showe1an = false;
  38. private static boolean showf1anm1 = false;
  39. private static boolean showe1bn = false;
  40. private static boolean showf1bnm1 = false;
  41. private static boolean showxxan = false;
  42. private static boolean showxxbn = false;
  43.  
  44. //Whether to bit trim (all odd) or not (all differences of two squares)
  45. private static boolean bittrim = false;
  46.  
  47. public static void main(String[] args){
  48. Scanner scan = new Scanner(System.in);
  49. String ain = "";
  50. String bin = "";
  51. BigInteger a = BigInteger.valueOf(1);
  52. BigInteger b = BigInteger.valueOf(1);
  53. System.out.println("Do you want to input c, or a and b? y for c, n for a and b");
  54. factorc = yncheck();
  55. if(factorc){
  56. System.out.println("c=");
  57. ain = "1";
  58. bin = scan.nextLine();
  59. try{
  60. a = new BigInteger(ain);
  61. b = new BigInteger(bin);
  62. if(((b.mod(BigInteger.valueOf(2))).equals(BigInteger.valueOf(0))) && !((b.mod(BigInteger.valueOf(4))).equals(BigInteger.valueOf(0)))){ //if it's not the difference of two squares
  63. System.out.println("You need to input a c value which is the difference of two squares");
  64. return;
  65. }
  66. if((b.mod(BigInteger.valueOf(2))).equals(BigInteger.valueOf(0))){ //if it's an even c (and it passed the above check), a+b needs to be even
  67. a = a.add(BigInteger.valueOf(1));
  68. b = b.divide(BigInteger.valueOf(2));
  69. }
  70. } catch(Exception e){
  71. System.out.println("Something was wrong with your c input (it needs to be a number)");
  72. return;
  73. }
  74. } else {
  75. System.out.println("a=");
  76. ain = scan.nextLine();
  77. System.out.println("b=");
  78. bin = scan.nextLine();
  79. try{
  80. a = new BigInteger(ain);
  81. b = new BigInteger(bin);
  82. } catch(Exception e){
  83. System.out.println("Something was wrong with your a and b input (they need to be numbers)");
  84. return;
  85. }
  86. }
  87. mainbools();
  88. BigCell cell = new BigCell(a, b);
  89. System.out.println(cell.description());
  90. System.out.println();
  91. System.out.println("=========================================");
  92. System.out.println();
  93. System.out.print("c = " + cell.c() + doeverything(cell.c()));
  94.  
  95. buffer++;
  96.  
  97. recursed(cell.d());
  98. recursee(cell.e());
  99. }
  100.  
  101. /*This function allows the user to determine whether they want to analyze just c, just the ab pairs, or everything.*/
  102. public static void mainbools(){
  103. System.out.println("Before you get output, you'll need to go through a few options.");
  104. System.out.println("Do you want to bit trim down the factor tree, or include even numbers that are the difference of two squares? y for bit trim, n for evens");
  105. bittrim = yncheck();
  106. System.out.println("Do you want to see everything? y/n");
  107. everything = yncheck();
  108. if(everything){
  109. allforc = true;
  110. allforab = true;
  111. showdef = true;
  112. showrootofd = true;
  113. showbign = true;
  114. showatbign = true;
  115. showatcbign = true;
  116. showatcshad = true;
  117. showxx2f1 = true;
  118. showxx2d1 = true;
  119. showxxbign = true;
  120. showxxcbign = true;
  121. showxxcshad = true;
  122. showen = true;
  123. showa1b1 = true;
  124. showxpnxpn = true;
  125. show2xpnxpn = true;
  126. show0naabb = true;
  127. show0nabab = true;
  128. show0nabba = true;
  129. show0nbaab = true;
  130. show0n1cc = true;
  131. show11d = true;
  132. show01a = true;
  133. showe1an = true;
  134. showf1anm1 = true;
  135. showe1bn = true;
  136. showf1bnm1 = true;
  137. showxxan = true;
  138. showxxbn = true;
  139. return;
  140. }
  141. System.out.println("Do you want to see information from c? y/n");
  142. allforc = yncheck();
  143. System.out.println("Do you want to see information from each ab pair? y/n");
  144. allforab = yncheck();
  145. if(allforc){
  146. boolsforc();
  147. if(allforab){
  148. System.out.println();
  149. }
  150. }
  151. if(allforab){
  152. boolsforab();
  153. }
  154. }
  155.  
  156. /*This function allows the user to determine what information about c they want to see, if any.*/
  157. public static void boolsforc(){
  158. System.out.println("Here is the list of available information for each c. Input each respective number on one line separated by a space for each thing you want to analyze.");
  159. System.out.println("\t 1 --- d, e and f");
  160. System.out.println("\t 2 --- Root of d cell");
  161. System.out.println("\t 3 --- BigN and BigN-1");
  162. System.out.println("\t 4 --- (e,1) and (f,1) where a[t] = BigN and BigN-1");
  163. System.out.println("\t 5 --- (e,1) and (f,1) where a[t] = c*BigN and c*(BigN-1)");
  164. System.out.println("\t 6 --- (e,1) and (f,1) where a[t] = c(c-BigN+1) and c(c-BigN+2)");
  165. System.out.println("\t 7 --- (e,1) and (f,1) where x+x = 2f+1 (i.e. where x = f or f+1)");
  166. System.out.println("\t 8 --- (e,1) and (f,1) where x+x = 2d+1");
  167. System.out.println("\t 9 --- (e,1) and (f,1) where x+x = BigN or BigN-1");
  168. System.out.println("\t10 --- (e,1) and (f,1) where x+x = c*BigN or c*BigN-1");
  169. System.out.println("\t11 --- (e,1) and (f,1) where x+x = c(c-BigN+1) or c(c-BigN+2)");
  170. ArrayList<Integer> picks = picknumbers();
  171. if(picks.contains(1)){
  172. showdef = true;
  173. }
  174. if(picks.contains(2)){
  175. showrootofd = true;
  176. }
  177. if(picks.contains(3)){
  178. showbign = true;
  179. }
  180. if(picks.contains(4)){
  181. showatbign = true;
  182. }
  183. if(picks.contains(5)){
  184. showatcbign = true;
  185. }
  186. if(picks.contains(6)){
  187. showatcshad = true;
  188. }
  189. if(picks.contains(7)){
  190. showxx2f1 = true;
  191. }
  192. if(picks.contains(8)){
  193. showxx2d1 = true;
  194. }
  195. if(picks.contains(9)){
  196. showxxbign = true;
  197. }
  198. if(picks.contains(10)){
  199. showxxcbign = true;
  200. }
  201. if(picks.contains(11)){
  202. showxxcshad = true;
  203. }
  204. }
  205.  
  206. /*This function allows the user to determine what information about a an b they want to see, if any.*/
  207. public static void boolsforab(){
  208. System.out.println("Here is the list of available information for each ab pair. Input each respective number on one line separated by a space for each thing you want to analyze.");
  209. System.out.println("\t 1 --- the (e,n) cell");
  210. System.out.println("\t 2 --- (a-1)(b-1) and BigN-n");
  211. System.out.println("\t 3 --- the (x+n)(x+n) square and its triangles and nn+2d(n-1)+f-1 configuration");
  212. System.out.println("\t 4 --- the 2(x+n)(x+n) square and its triangles");
  213. System.out.println("\t 5 --- (0,n) a=aa b=bb");
  214. System.out.println("\t 6 --- (0,n) a=ab b=ab");
  215. System.out.println("\t 7 --- (0,n) a=a b=abb");
  216. System.out.println("\t 8 --- (0,n) a=b b=aab");
  217. System.out.println("\t 9 --- (0,n) a=1 b=cc");
  218. System.out.println("\t10 --- the cell in (1,1) where d is the n value for (0,n) a=aa b=bb");
  219. System.out.println("\t11 --- the cell in (0,1) where a is the n value for (0,n) a=aa b=bb");
  220. System.out.println("\t12 --- (e,1) a[t] = an");
  221. System.out.println("\t13 --- (f,1) a[t] = a(n-1)");
  222. System.out.println("\t14 --- (e,1) a[t] = bn");
  223. System.out.println("\t15 --- (f,1) a[t] = b(n-1)");
  224. System.out.println("\t16 --- (e,1) and (f,1) where x+x = an or a(n-1)");
  225. System.out.println("\t17 --- (e,1) and (f,1) where x+x = bn or b(n-1)");
  226. ArrayList<Integer> picks = picknumbers();
  227. if(picks.contains(1)){
  228. showen = true;
  229. }
  230. if(picks.contains(2)){
  231. showa1b1 = true;
  232. }
  233. if(picks.contains(3)){
  234. showxpnxpn = true;
  235. }
  236. if(picks.contains(4)){
  237. show2xpnxpn = true;
  238. }
  239. if(picks.contains(5)){
  240. show0naabb = true;
  241. }
  242. if(picks.contains(6)){
  243. show0nabab = true;
  244. }
  245. if(picks.contains(7)){
  246. show0nabba = true;
  247. }
  248. if(picks.contains(8)){
  249. show0nbaab = true;
  250. }
  251. if(picks.contains(9)){
  252. show0n1cc = true;
  253. }
  254. if(picks.contains(10)){
  255. show11d = true;
  256. }
  257. if(picks.contains(11)){
  258. show01a = true;
  259. }
  260. if(picks.contains(12)){
  261. showe1an = true;
  262. }
  263. if(picks.contains(13)){
  264. showf1anm1 = true;
  265. }
  266. if(picks.contains(14)){
  267. showe1bn = true;
  268. }
  269. if(picks.contains(15)){
  270. showf1bnm1 = true;
  271. }
  272. if(picks.contains(16)){
  273. showxxan = true;
  274. }
  275. if(picks.contains(17)){
  276. showxxbn = true;
  277. }
  278. }
  279.  
  280. /*When the user is prompted to input a number, this function will make sure input is valid.*/
  281. public static ArrayList<Integer> picknumbers(){
  282. ArrayList<Integer> picks = new ArrayList<Integer>();
  283. Scanner scan = new Scanner(System.in);
  284. String ints = scan.nextLine();
  285. String[] parts = ints.split(" ");
  286. for(int i=0; i<parts.length; i++){
  287. try {
  288. int test = Integer.parseInt(parts[i]);
  289. picks.add(test);
  290. } catch(Exception e) {
  291. }
  292. }
  293. return picks;
  294. }
  295.  
  296. /*When the user is prompted to input y/n, this function will make sure input is valid.*/
  297. public static boolean yncheck(){
  298. Scanner scan = new Scanner(System.in);
  299. for(;;){
  300. String line = scan.nextLine();
  301. if(line.equals("y") || line.equals("Y")){
  302. return true;
  303. } else if(line.equals("n") || line.equals("N")){
  304. return false;
  305. }
  306. System.out.println("Please input y or n");
  307. }
  308. }
  309.  
  310. /*This function creates the d branches down the factor tree.*/
  311. public static void recursed(BigInteger d){
  312. String dstring = "";
  313.  
  314. if(bittrim){
  315. if((d.compareTo(BigInteger.valueOf(0)) == 1) && ((d.mod(BigInteger.valueOf(2))).equals(BigInteger.valueOf(0)))){
  316. while((d.mod(BigInteger.valueOf(2))).equals(BigInteger.valueOf(0))){
  317. dstring += d + " / ";
  318. d = d.divide(BigInteger.valueOf(2));
  319. }
  320. }
  321. } else {
  322. if((d.compareTo(BigInteger.valueOf(0)) == 1) && ((d.mod(BigInteger.valueOf(2))).equals(BigInteger.valueOf(0)))){
  323. if(!((d.mod(BigInteger.valueOf(4))).equals(BigInteger.valueOf(0)))){
  324. dstring += d + " / ";
  325. d = d.divide(BigInteger.valueOf(2));
  326. }
  327. }
  328. }
  329.  
  330. dstring += d;
  331. for(int i=0; i<buffer; i++){
  332. System.out.print(" ");
  333. }
  334. System.out.print("d = " + dstring);
  335. if((d.compareTo(BigInteger.valueOf(3))) == 1){
  336. System.out.println(" ---" + doeverything(d));
  337. BigInteger d2 = sqrt(d);
  338. BigInteger e = d.subtract(d2.multiply(d2));
  339. buffer++;
  340. recursed(d2);
  341. recursee(e);
  342. buffer--;
  343. } else {
  344. System.out.println();
  345. }
  346. }
  347.  
  348. /*This function creates the e branches of the factor tree.*/
  349. public static void recursee(BigInteger e){
  350. String estring = "";
  351. if(bittrim){
  352. if((e.compareTo(BigInteger.valueOf(0)) == 1) && ((e.mod(BigInteger.valueOf(2))).equals(BigInteger.valueOf(0)))){
  353. while((e.mod(BigInteger.valueOf(2))).equals(BigInteger.valueOf(0))){
  354. estring += e + " / ";
  355. e = e.divide(BigInteger.valueOf(2));
  356. }
  357. }
  358. } else {
  359. if((e.compareTo(BigInteger.valueOf(0)) == 1) && ((e.mod(BigInteger.valueOf(2))).equals(BigInteger.valueOf(0)))){
  360. if(!((e.mod(BigInteger.valueOf(4))).equals(BigInteger.valueOf(0)))){
  361. estring += e + " / ";
  362. e = e.divide(BigInteger.valueOf(2));
  363. }
  364. }
  365. }
  366. estring += e;
  367. for(int i=0; i<buffer; i++){
  368. System.out.print(" ");
  369. }
  370. System.out.print("e = " + estring);
  371. if((e.compareTo(BigInteger.valueOf(3))) == 1){
  372. System.out.println(" ---" + doeverything(e));
  373. BigInteger d = sqrt(e);
  374. BigInteger e2 = e.subtract(d.multiply(d));
  375. buffer++;
  376. recursed(d);
  377. recursee(e2);
  378. buffer--;
  379. } else {
  380. System.out.println();
  381. System.out.println();
  382. }
  383. }
  384.  
  385. /*This function creates the output String by calling the other relevant functions.*/
  386. public static String doeverything(BigInteger c){
  387. ArrayList<ArrayList<BigInteger>> factors = factorize(c);
  388. String thing = "\n";
  389. if(allforc){
  390. thing += forc(c);
  391. }
  392. if(allforab){
  393. thing += forpairs(factors);
  394. }
  395.  
  396. return thing;
  397. }
  398.  
  399. /*This function generates a String containing all relevant directly calculable information for each number down the factor tree.*/
  400. public static String forc(BigInteger c){
  401. String thing = "";
  402. BigInteger d = sqrt(c);
  403. BigInteger e = c.subtract(d.multiply(d));
  404. BigInteger f = e.subtract((d.multiply(BigInteger.valueOf(2))).add(BigInteger.valueOf(1)));
  405. BigInteger bign = ((c.add(BigInteger.valueOf(1))).divide(BigInteger.valueOf(2))).subtract(d);
  406. BigInteger cbign = c.multiply(bign);
  407. BigInteger cbignm1 = c.multiply(bign.subtract(BigInteger.valueOf(1)));
  408. BigInteger cshadown = c.multiply(c.subtract(bign).add(BigInteger.valueOf(1)));
  409. BigInteger cshadownp1 = c.multiply(c.subtract(bign).add(BigInteger.valueOf(2)));
  410.  
  411. //directly calculables
  412. if(showdef){
  413. thing += bufferadd();
  414. thing += "d = " + d + ", e = " + e + ", f = " + f + "\n";
  415. }
  416.  
  417. //Root of d
  418. if(showrootofd){
  419. thing += bufferadd();
  420. BigInteger rootofd_a = d;
  421. BigInteger rootofd_b = d.multiply(BigInteger.valueOf(9));
  422. BigCell rootofd = new BigCell(rootofd_a, rootofd_b);
  423. thing += "Root of d: " + rootofd.description();
  424. thing += "\n";
  425. }
  426.  
  427. //BigN and BigN-1
  428. if(showbign){
  429. thing += bufferadd();
  430. thing += "BigN = " + bign + ", BigN-1 = " + (bign.subtract(BigInteger.valueOf(1)));
  431. thing += "\n";
  432. }
  433.  
  434. //The cells in (e,1) and (f,1) where a[t]=BigN and a[t]=BigN-1
  435. if(showatbign){
  436. thing += bufferadd();
  437. BigCell e1_bign = e1cell(e, bign);
  438. thing += "(e,1) a=BigN = ";
  439. thing += e1_bign.description() + " --- " + squaresum(e1_bign);
  440. thing += "\n";
  441. thing += bufferadd();
  442. BigCell f1_bign = f1cell(f, (bign.subtract(BigInteger.valueOf(1))));
  443. thing += "(f,1) a=BigN-1 = ";
  444. thing += f1_bign.description() + " --- " + squaresum(f1_bign);
  445. thing += "\n";
  446. }
  447.  
  448. //The cells in (e,1) and (f,1) where a[t]=c*BigN and c*(BigN-1)
  449. if(showatcbign){
  450. thing += bufferadd();
  451. BigCell e1_cbign = e1cell(e, cbign);
  452. thing += "(e,1) a=c*BigN = ";
  453. thing += e1_cbign.description() + " --- " + squaresum(e1_cbign);
  454. thing += "\n";
  455. thing += bufferadd();
  456. BigCell f1_cbignm1 = f1cell(f, cbignm1);
  457. thing += "(f,1) a=c*(BigN-1) = ";
  458. thing += f1_cbignm1.description() + " --- " + squaresum(f1_cbignm1);
  459. thing += "\n";
  460. }
  461.  
  462. //The cells in (e,1) and (f,1) where a[t]=c(c-BigN+1) and c(c-BigN+2)
  463. if(showatcshad){
  464. thing += bufferadd();
  465. BigCell e1_cshadown = e1cell(e, cshadown);
  466. thing += "(e,1) a=c*(c-BigN+1) = ";
  467. thing += e1_cshadown.description() + " --- " + squaresum(e1_cshadown);
  468. thing += "\n";
  469. thing += bufferadd();
  470. BigCell f1_cshadownp1 = f1cell(f, cshadownp1);
  471. thing += "(f,1) a=c*(c-BigN+2) = ";
  472. thing += f1_cshadownp1.description() + " --- " + squaresum(f1_cshadownp1);
  473. thing += "\n";
  474. }
  475.  
  476. //The cells in (e,1) and (f,1) where x+x=2f+1
  477. if(showxx2f1){
  478. BigInteger twofp1 = (f.multiply(BigInteger.valueOf(-2))).add(BigInteger.valueOf(1));
  479. thing += xplusxstring("2f+1 (x=f or f+1)", twofp1, "filler", BigInteger.valueOf(2), e, f);
  480. }
  481.  
  482. //The cells in (e,1) and (f,1) where x+x=2d+1
  483. if(showxx2d1){
  484. BigInteger twodp1 = (d.multiply(BigInteger.valueOf(2))).add(BigInteger.valueOf(1));
  485. thing += xplusxstring("2d+1", twodp1, "filler", BigInteger.valueOf(2), e, f);
  486. }
  487.  
  488. //The cells in (e,1) and (f,1) where x+x=BigN or BigN-1
  489. if(showxxbign){
  490. thing += xplusxstring("BigN", bign, "BigN-1", (bign.subtract(BigInteger.valueOf(1))), e, f);
  491. }
  492.  
  493. //The cells in (e,1) and (f,1) where x+x=c*BigN or c*BigN-1
  494. if(showxxcbign){
  495. thing += xplusxstring("c*BigN", cbign, "c*(BigN-1)", cbignm1, e, f);
  496. }
  497.  
  498. //The cells in (e,1) and (f,1) where x+x=c(c-BigN+1) or c(c-BigN+2)
  499. if(showxxcshad){
  500. thing += xplusxstring("c*(c-BigN+1)", cshadown, "c*(c-BigN+2)", cshadownp1, e, f);
  501. }
  502.  
  503. return thing;
  504. }
  505.  
  506. /*This function generates a String containing all relevant information for each factor pair.*/
  507. public static String forpairs(ArrayList<ArrayList<BigInteger>> factors){
  508. String thing = "";
  509. for(int i=0; i<factors.size(); i++){
  510. if(((factors.get(i).get(0).add(factors.get(i).get(1))).mod(BigInteger.valueOf(2))).equals(BigInteger.valueOf(0))){
  511. BigInteger a = factors.get(i).get(0);
  512. BigInteger b = factors.get(i).get(1);
  513. BigCell paircell = new BigCell(a, b);
  514. BigCell zeron_aabb_cell = zeron_aabb(paircell);
  515. BigInteger n_aabb = zeron_aabb_cell.n();
  516. BigCell e1_na = e1cell(paircell.e(), (paircell.n().multiply(paircell.a())));
  517. BigCell f1_na = f1cell(paircell.f(), (paircell.a().multiply(paircell.n().subtract(BigInteger.valueOf(1)))));
  518. BigCell e1_bn = e1cell(paircell.e(), (paircell.b().multiply(paircell.n())));
  519. BigCell f1_bn = f1cell(paircell.f(), (paircell.b().multiply(paircell.n().subtract(BigInteger.valueOf(1)))));
  520.  
  521. thing += bufferadd();
  522. thing += factors.get(i);
  523. thing += "\n";
  524.  
  525. buffer++;
  526.  
  527. //The (e,n) cell
  528. if(showen){
  529. thing += bufferadd();
  530. thing += "(e,n) cell = " + paircell.description();
  531. thing += "\n";
  532. }
  533.  
  534. //(a-1)(b-1) and /2
  535. if(showa1b1){
  536. BigInteger a1b1 = (a.subtract(BigInteger.valueOf(1))).multiply(b.subtract(BigInteger.valueOf(1)));
  537. BigInteger a1b1over2 = a1b1.divide(BigInteger.valueOf(2));
  538. thing += bufferadd();
  539. thing += "(a-1)(b-1) = " + a1b1 + ", /2 (BigN-n) = " + a1b1over2;
  540. thing += "\n";
  541. }
  542.  
  543. //the (x+n)(x+n) square, its triangles, and its nn+2d(n-1)+f-1 configuration
  544. if(showxpnxpn){
  545. BigInteger xplusnsquared = paircell.xplusnsquared();
  546. BigInteger tri1 = BigInteger.valueOf(0);
  547. BigInteger tri2 = BigInteger.valueOf(0);
  548. String triout = "error in trioutput";
  549. if((xplusnsquared.mod(BigInteger.valueOf(2))).equals(BigInteger.valueOf(0))){
  550. BigInteger overfour = xplusnsquared.divide(BigInteger.valueOf(4));
  551. BigInteger largebase = sqrt(overfour);
  552. BigInteger smallbase = largebase.subtract(BigInteger.valueOf(1));
  553. tri1 = (largebase.multiply(largebase.subtract(BigInteger.valueOf(1)))).divide(BigInteger.valueOf(2));
  554. tri2 = (smallbase.multiply(smallbase.subtract(BigInteger.valueOf(1)))).divide(BigInteger.valueOf(2));
  555. triout = "(x+n)(x+n) is " + xplusnsquared + ", which is (4*" + tri1 + ")+(4*" + tri2 + ") (u=" + largebase + "&" + smallbase + ")";
  556. } else {
  557. BigInteger minusone = xplusnsquared.subtract(BigInteger.valueOf(1));
  558. tri1 = minusone.divide(BigInteger.valueOf(8));
  559. triout = "(x+n)(x+n) is " + xplusnsquared + ", which is (8*" + tri1 + ")+1 (u=" + paircell.u() + ")";
  560. }
  561. triout+="\n";
  562. triout+=bufferadd();
  563. BigInteger nn = paircell.n().multiply(paircell.n());
  564. BigInteger twodnm1 = (paircell.d().multiply(BigInteger.valueOf(2))).multiply(paircell.n().subtract(BigInteger.valueOf(1)));
  565. BigInteger posf = paircell.f().multiply(BigInteger.valueOf(-1));
  566. triout+="nn+2d(n-1)+f-1 = " + nn + "+" + twodnm1 + "+" + posf + "-1";
  567. thing += bufferadd();
  568. thing += triout;
  569. thing += "\n";
  570. }
  571.  
  572. //the 2(x+n)2(x+n) square, its triangles, and its nn+2d(n-1)+f-1 configuration
  573. if(show2xpnxpn){
  574. BigInteger xplusnsquared2 = (paircell.xplusn().multiply(BigInteger.valueOf(2))).multiply(paircell.xplusn().multiply(BigInteger.valueOf(2)));
  575. String triout2 = "error in trioutput2";
  576. BigInteger tri12 = BigInteger.valueOf(0);
  577. BigInteger tri22 = BigInteger.valueOf(0);
  578. BigInteger overfour2 = xplusnsquared2.divide(BigInteger.valueOf(4)); //since it's *2 it'll always be evens
  579. BigInteger largebase2 = sqrt(overfour2);
  580. BigInteger smallbase2 = largebase2.subtract(BigInteger.valueOf(1));
  581. tri12 = (largebase2.multiply(largebase2.subtract(BigInteger.valueOf(1)))).divide(BigInteger.valueOf(2));
  582. tri22 = (smallbase2.multiply(smallbase2.subtract(BigInteger.valueOf(1)))).divide(BigInteger.valueOf(2));
  583. triout2 = "2(x+n)2(x+n) is " + xplusnsquared2 + ", which is (4*" + tri12 + ")+(4*" + tri22 + ") (u=" + largebase2 + "&" + smallbase2 + ")";
  584. thing += bufferadd();
  585. thing += triout2;
  586. thing += "\n";
  587. }
  588.  
  589. //(0,n) a=aa b=bb
  590. if(show0naabb){
  591. thing += bufferadd();
  592. thing += "(0,n) aa,bb = ";
  593. thing += zeron_aabb_cell.description();
  594. thing += "\n";
  595. }
  596.  
  597. //(0,n) a=ab b=ab
  598. if(show0nabab){
  599. thing += bufferadd();
  600. BigCell zeron_ab_ab_cell = zeron_ab_ab(paircell);
  601. thing += "(0,n) ab,ab = ";
  602. thing += zeron_ab_ab_cell.description();
  603. thing += "\n";
  604. }
  605.  
  606. //(0,n) a=a b=abb
  607. if(show0nabba){
  608. thing += bufferadd();
  609. BigCell zeron_a_abb_cell = zeron_a_abb(paircell);
  610. thing += "(0,n) a,abb = ";
  611. thing += zeron_a_abb_cell.description();
  612. thing += "\n";
  613. }
  614.  
  615. //(0,n) a=b b=aab
  616. if(show0nbaab){
  617. thing += bufferadd();
  618. BigCell zeron_b_aab_cell = zeron_b_aab(paircell);
  619. thing += "(0,n) b,aab = ";
  620. thing += zeron_b_aab_cell.description();
  621. thing += "\n";
  622. }
  623.  
  624. //(0,n) a=1 b=cc
  625. if(show0n1cc){
  626. thing += bufferadd();
  627. BigCell zeron_1cc_cell = zeron_1cc(paircell);
  628. thing += "(0,n) 1,cc = ";
  629. thing += zeron_1cc_cell.description();
  630. thing += "\n";
  631. }
  632.  
  633. //the cell in (1,1) where d is equal to the n value for the aa bb cell
  634. if(show11d){
  635. thing += bufferadd();
  636. BigCell oneone_d_aabbn_cell = oneone_d_aabbn(n_aabb);
  637. thing += "(1,1) d=aabbn = ";
  638. thing += oneone_d_aabbn_cell.description();
  639. thing += "\n";
  640. }
  641.  
  642. //the cell in (0,1) where a is equal to the n value for the aa bb cell
  643. if(show01a){
  644. thing += bufferadd();
  645. BigCell zeroone_a_aabbn_cell = zeroone_a_aabbn(n_aabb);
  646. thing += "(0,1) a=aabbn = ";
  647. thing += zeroone_a_aabbn_cell.description();
  648. thing += "\n";
  649. }
  650.  
  651. //(e,1) na
  652. if(showe1an){
  653. thing += bufferadd();
  654. thing += "(e,1) a=na = ";
  655. thing += e1_na.description() + " --- " + squaresum(e1_na);
  656. thing += "\n";
  657. }
  658.  
  659. //(f,1) a(n-1)
  660. if(showf1anm1){
  661. thing += bufferadd();
  662. thing += "(f,1) a=a(n-1) = ";
  663. thing += f1_na.description() + " --- " + squaresum(f1_na);
  664. thing += "\n";
  665. }
  666.  
  667. //(e,1) bn
  668. if(showe1bn){
  669. thing += bufferadd();
  670. thing += "(e,1) a=bn = ";
  671. thing += e1_bn.description() + " --- " + squaresum(e1_bn);
  672. thing += "\n";
  673. }
  674.  
  675. //(f,1) b(n-1)
  676. if(showf1bnm1){
  677. thing += bufferadd();
  678. thing += "(f,1) a=b(n-1) = ";
  679. thing += f1_bn.description() + " --- " + squaresum(f1_bn);
  680. thing += "\n";
  681. }
  682.  
  683. //the cells in (e,1) and (f,1) for which x+x=an or a(n-1) (whichever's odd, if any)
  684. if(showxxan){
  685. thing += xplusxstring("an", e1_na.a(), "a(n-1)", f1_na.a(), paircell.e(), paircell.f());
  686. }
  687.  
  688. //the cells in (e,1) and (f,1) for which x+x=bn or b(n-1) (whichever's odd, if any)
  689. if(showxxbn){
  690. thing += xplusxstring("bn", e1_bn.a(), "b(n-1)", f1_bn.a(), paircell.e(), paircell.f());
  691. }
  692.  
  693. buffer--;
  694. }
  695. }
  696. return thing;
  697. }
  698.  
  699. /*This function creates an ArrayList containing each pair of factors that multiply together to give your given c.*/
  700. public static ArrayList<ArrayList<BigInteger>> factorize(BigInteger c){
  701. ArrayList<ArrayList<BigInteger>> factorpairs = new ArrayList<ArrayList<BigInteger>>();
  702. ArrayList<BigInteger> primefactors = primefactors(c);
  703. Collections.sort(primefactors);
  704. ArrayList<Integer> factorcount = new ArrayList<Integer>();
  705. int count=0;
  706. for(int i=0; i<primefactors.size(); i++){
  707. if(i>0){
  708. if(primefactors.get(i-1).equals(primefactors.get(i))){
  709. count++;
  710. } else {
  711. factorcount.add(count+1);
  712. count = 0;
  713. }
  714. }
  715. }
  716. factorcount.add(count+1);
  717.  
  718. Set<BigInteger> set = new HashSet<>(primefactors);
  719. primefactors.clear();
  720. primefactors.addAll(set);
  721. Collections.sort(primefactors);
  722. ArrayList<BigInteger> allfactors = new ArrayList<BigInteger>();
  723.  
  724. boolean divisible = true;
  725. for(int l=0; l<primefactors.size(); l++){
  726. allfactors.add(primefactors.get(l));
  727. }
  728.  
  729. int lower = 0;
  730. int higher = primefactors.size(); //when it starts it'll try 0 to the end of primefactors in allfactors
  731. while(divisible){
  732. divisible = false;
  733. for(int j=0; j<primefactors.size(); j++){
  734. //if allfactors[k] is divisible by primefactors[0] factorcount[0] times
  735. BigInteger exp = primefactors.get(j).pow(factorcount.get(j));
  736. for(int k=lower; k<higher; k++){
  737. if((!(allfactors.get(k).mod(exp).equals(BigInteger.valueOf(0)))) && !(allfactors.contains(allfactors.get(k).multiply(primefactors.get(j))))){ //if allfactors%primefactor^exp == 0
  738. allfactors.add(allfactors.get(k).multiply(primefactors.get(j)));
  739. divisible = true;
  740. }
  741. }
  742. }
  743. lower = higher;
  744. higher = allfactors.size();
  745. }
  746. allfactors.add(BigInteger.valueOf(1));
  747. Collections.sort(allfactors);
  748.  
  749. lower = 0;
  750. higher = allfactors.size()-1;
  751. while(lower<=higher){
  752. if((allfactors.get(lower).add(allfactors.get(higher))).mod(BigInteger.valueOf(2)).equals(BigInteger.valueOf(0))){ //a+b has to be even, but won't always be for even c (e.g. 20=2*10=4*5, but only 2*10 makes a valid n)
  753. ArrayList<BigInteger> temppair = new ArrayList<BigInteger>();
  754. temppair.add(allfactors.get(lower));
  755. temppair.add(allfactors.get(higher));
  756. factorpairs.add(temppair);
  757. }
  758. lower++;
  759. higher--;
  760. }
  761. if(factorpairs.get(0).get(0).equals(BigInteger.valueOf(1)) && factorpairs.get(0).get(1).equals(BigInteger.valueOf(1))){ //primes
  762. factorpairs = new ArrayList<ArrayList<BigInteger>>();
  763. ArrayList<BigInteger> primepair = new ArrayList<BigInteger>();
  764. primepair.add(BigInteger.valueOf(1));
  765. primepair.add(c);
  766. factorpairs.add(primepair);
  767. }
  768. return factorpairs;
  769. }
  770.  
  771. /*This function finds all prime factors of a given number.*/
  772. public static ArrayList<BigInteger> primefactors(BigInteger cfixed){
  773. BigInteger c = cfixed;
  774. ArrayList<BigInteger> factors = new ArrayList<BigInteger>();
  775. if(c.equals(BigInteger.valueOf(1))){
  776. factors.add(BigInteger.valueOf(1));
  777. return factors;
  778. }
  779. BigInteger lower = BigInteger.valueOf(2);
  780. BigInteger higher = sqrt(c).add(BigInteger.valueOf(1));
  781. while(lower.compareTo(higher) == -1){
  782. while((c.mod(lower).equals(BigInteger.valueOf(0))) && !(c.equals(lower))){
  783. factors.add(lower);
  784. c = c.divide(lower);
  785. factors.add(c);
  786. higher = sqrt(c).add(BigInteger.valueOf(1));
  787. }
  788. if(lower.equals(BigInteger.valueOf(2))){
  789. lower = lower.add(BigInteger.valueOf(1));
  790. } else {
  791. lower = lower.add(BigInteger.valueOf(2));
  792. }
  793. }
  794. for(int i=0; i<factors.size(); i++){
  795. for(int j=0; j<factors.size(); j++){
  796. if( ((factors.get(i)).mod(factors.get(j)).equals(BigInteger.valueOf(0))) && !(factors.get(i).equals(factors.get(j)))){
  797. factors.remove(i);
  798. j=factors.size();
  799. }
  800. }
  801. }
  802. return factors;
  803. }
  804.  
  805. /*This function adds a buffer to the String (increasing in width down each branch of the factor tree) for presentation purposes.*/
  806. public static String bufferadd(){
  807. String returnbuffer = "";
  808. for(int i=0; i<buffer+1; i++){
  809. returnbuffer+=" ";
  810. }
  811. return returnbuffer;
  812. }
  813.  
  814. /*This function generates the cell in (0,n) where a=aa and b=bb.*/
  815. public static BigCell zeron_aabb(BigCell cell){
  816. BigInteger a = cell.a().multiply(cell.a());
  817. BigInteger b = cell.b().multiply(cell.b());
  818. BigCell returncell = new BigCell(a,b);
  819. return returncell;
  820. }
  821.  
  822. /*This function generates the cell in (0,n) where a=1 and b=cc.*/
  823. public static BigCell zeron_1cc(BigCell cell){
  824. BigInteger a = BigInteger.valueOf(1);
  825. BigInteger b = cell.c().multiply(cell.c());
  826. BigCell returncell = new BigCell(a,b);
  827. return returncell;
  828. }
  829.  
  830. /*This function generates the cell in (0,n) where a=a and b=abb.*/
  831. public static BigCell zeron_a_abb(BigCell cell){
  832. BigInteger a = cell.a();
  833. BigInteger b = cell.b().multiply(cell.b().multiply(cell.a()));
  834. BigCell returncell = new BigCell(a,b);
  835. return returncell;
  836. }
  837.  
  838. /*This function generates the cell in (0,n) where a=c and b=c.*/
  839. public static BigCell zeron_ab_ab(BigCell cell){
  840. BigInteger a = cell.a().multiply(cell.b());
  841. BigInteger b = cell.a().multiply(cell.b());
  842. BigCell returncell = new BigCell(a,b);
  843. return returncell;
  844. }
  845.  
  846. /*This function generates the cell in (0,n) where a=b and b=aab.*/
  847. public static BigCell zeron_b_aab(BigCell cell){
  848. BigInteger a = cell.b();
  849. BigInteger b = cell.b().multiply(cell.a().multiply(cell.a()));
  850. BigCell returncell = new BigCell(a,b);
  851. return returncell;
  852. }
  853.  
  854. /*This function generates the cell in (1,1) where d is the n value from the cell in (0,n) where a=aa and b=bb.*/
  855. public static BigCell oneone_d_aabbn(BigInteger d){
  856. BigInteger e = BigInteger.valueOf(1);
  857. BigInteger n = BigInteger.valueOf(1);
  858. BigInteger f = (e.subtract((d.multiply(BigInteger.valueOf(2))).add(BigInteger.valueOf(1)))).multiply(BigInteger.valueOf(-1));
  859. BigInteger rootf = sqrt(f);
  860. BigInteger x = rootf.subtract(BigInteger.valueOf(1));
  861. BigInteger a = d.subtract(x);
  862. BigInteger b = a.add((x.multiply(BigInteger.valueOf(2))).add(n.multiply(BigInteger.valueOf(2))));
  863. BigCell returncell = new BigCell(a,b);
  864. return returncell;
  865. }
  866.  
  867. /*This function generates the cell in (0,1) where a is the n value from the cell in (0,n) where a=aa and b=bb.*/
  868. public static BigCell zeroone_a_aabbn(BigInteger a){
  869. BigInteger e = BigInteger.valueOf(0);
  870. BigInteger n = BigInteger.valueOf(1);
  871. BigInteger twoname = (n.multiply(a.multiply(BigInteger.valueOf(2)))).subtract(e);
  872. BigInteger x = sqrt(twoname);
  873. BigInteger b = a.add((x.multiply(BigInteger.valueOf(2))).add(n.multiply(BigInteger.valueOf(2))));
  874. BigCell returncell = new BigCell(a,b);
  875. return returncell;
  876. }
  877.  
  878. /*This function calculates the two consecutive squares or the doubled square that make up a[t] in (e,1) and (f,1).*/
  879. public static String squaresum(BigCell cell){
  880. BigInteger e = cell.e();
  881. BigInteger a = cell.a();
  882. BigInteger t = cell.t();
  883. String returnstring = "";
  884. if((e.mod(BigInteger.valueOf(2))).equals(BigInteger.valueOf(0))){
  885. BigInteger eover2 = e.divide(BigInteger.valueOf(2));
  886. BigInteger asquares = a.subtract(eover2);
  887. BigInteger asquare = asquares.divide(BigInteger.valueOf(2));
  888. returnstring = "a[t] = sq+sq+e/2 = " + asquare + "+" + asquare + "+" + eover2;
  889. } else {
  890. BigInteger eminus1over2 = (e.subtract(BigInteger.valueOf(1))).divide(BigInteger.valueOf(2));
  891. BigInteger afirstsquare = (t.subtract(BigInteger.valueOf(1))).multiply(t.subtract(BigInteger.valueOf(1)));
  892. BigInteger asecondsquare = t.multiply(t);
  893. returnstring = "a[t] = sq+(sq)+(e-1)/2 = " + afirstsquare + "+" + asecondsquare + "+" + eminus1over2;
  894. }
  895. return returnstring;
  896. }
  897.  
  898. /*This function calculates the cell in (e,1) where a[t] equals a given value (which is assumed to be valid). */
  899. public static BigCell e1cell(BigInteger e, BigInteger value){
  900. BigInteger n = BigInteger.valueOf(1);
  901. BigInteger a = value;
  902. BigInteger twoname = (n.multiply(a.multiply(BigInteger.valueOf(2)))).subtract(e);
  903. BigInteger x = sqrt(twoname);
  904. BigInteger b = a.add(x.multiply(BigInteger.valueOf(2))).add(n.multiply(BigInteger.valueOf(2)));
  905. BigCell returncell = new BigCell(a,b);
  906. return returncell;
  907. }
  908.  
  909. /*This function calculates the cell in (f,1) where a[t] equals a given value (which is assumed to be valid). */
  910. public static BigCell f1cell(BigInteger f, BigInteger value){
  911. BigInteger n = BigInteger.valueOf(1);
  912. BigInteger a = value;
  913. BigInteger twonamf = (n.multiply(a.multiply(BigInteger.valueOf(2)))).subtract(f);
  914. BigInteger x = sqrt(twonamf);
  915. BigInteger b = a.add(x.multiply(BigInteger.valueOf(2))).add(n.multiply(BigInteger.valueOf(2)));
  916. BigCell returncell = new BigCell(a,b,f);
  917. return returncell;
  918. }
  919.  
  920. /*This function calculates the cells in (e,1) and (f,1) where the x values add to some other value (like an or bn for example).*/
  921. public static ArrayList<BigCell> xplusx(BigInteger value, BigInteger e, BigInteger f){
  922. if((value.mod(BigInteger.valueOf(2))).equals(BigInteger.valueOf(0))){
  923. value = value.add(BigInteger.valueOf(1)); //not the best fix but I'll only be using it for odds anyway
  924. }
  925. BigInteger halflow = value.divide(BigInteger.valueOf(2));
  926. BigInteger halfhigh = halflow.add(BigInteger.valueOf(1)); //the two values that add to make value
  927. BigInteger xneg = BigInteger.valueOf(0);
  928. BigInteger xpos = BigInteger.valueOf(0);
  929. if((e.mod(BigInteger.valueOf(2))).equals(BigInteger.valueOf(0))){ //e has even x, f has odd x
  930. if((halflow.mod(BigInteger.valueOf(2))).equals(BigInteger.valueOf(0))){
  931. xpos = halflow;
  932. xneg = halfhigh;
  933. } else {
  934. xpos = halfhigh;
  935. xneg = halflow;
  936. }
  937. } else { //e has odd x, f has even x
  938. if((halflow.mod(BigInteger.valueOf(2))).equals(BigInteger.valueOf(0))){
  939. xpos = halfhigh;
  940. xneg = halflow;
  941. } else {
  942. xpos = halflow;
  943. xneg = halfhigh;
  944. }
  945. }
  946.  
  947. BigInteger n = BigInteger.valueOf(1);
  948. BigInteger aneg = ((xneg.multiply(xneg)).add(f)).divide(BigInteger.valueOf(2));
  949. BigInteger apos = ((xpos.multiply(xpos)).add(e)).divide(BigInteger.valueOf(2));
  950. BigInteger bneg = aneg.add(BigInteger.valueOf(2)).add(xneg.multiply(BigInteger.valueOf(2)));
  951. BigInteger bpos = apos.add(BigInteger.valueOf(2)).add(xpos.multiply(BigInteger.valueOf(2)));
  952. BigCell fcell = new BigCell(aneg, bneg, f);
  953. BigCell ecell = new BigCell(apos, bpos);
  954.  
  955. ArrayList<BigCell> returncells = new ArrayList<BigCell>();
  956. returncells.add(fcell);
  957. returncells.add(ecell);
  958. return returncells;
  959. }
  960.  
  961. /*This function turns the staircase x cells from row 1 (in the function directly above) into a String.*/
  962. public static String xplusxstring(String type1, BigInteger value1, String type2, BigInteger value2, BigInteger e, BigInteger f){
  963. String thing = "";
  964. thing += bufferadd();
  965. if(value1.mod(BigInteger.valueOf(2)).equals(BigInteger.valueOf(1))){
  966. thing += "The cells in (e,1) and (f,1) where x+x=" + type1 + "=" + value1 + "\n";
  967. buffer++;
  968. thing += bufferadd();
  969. ArrayList<BigCell> pair = xplusx(value1, e, f);
  970. thing += pair.get(0).description() + "\n" + bufferadd();
  971. thing += pair.get(1).description() + "\n";
  972. buffer--;
  973. } else if(value2.mod(BigInteger.valueOf(2)).equals(BigInteger.valueOf(1))){
  974. thing += "The cells in (e,1) and (f,1) where x+x=" + type2 + "=" + value2 + "\n";
  975. buffer++;
  976. thing += bufferadd();
  977. ArrayList<BigCell> pair = xplusx(value2, e, f);
  978. thing += pair.get(0).description() + "\n" + bufferadd();
  979. thing += pair.get(1).description() + "\n";
  980. buffer--;
  981. } else {
  982. thing += "Neither of " + type1 + " and " + type2 + " are odd, so here's where x+x=" + type1 + "+1=" + (value1.add(BigInteger.valueOf(1))) + " (using x from (e,1) and (f,1)):\n";
  983. buffer++;
  984. thing += bufferadd();
  985. ArrayList<BigCell> pair = xplusx((value1.add(BigInteger.valueOf(1))), e, f);
  986. thing += pair.get(0).description() + "\n" + bufferadd();
  987. thing += pair.get(1).description() + "\n";
  988. buffer--;
  989. }
  990. return thing;
  991. }
  992.  
  993. /*BigInteger sqrt method. Not the one Chris gave us - that didn't work properly for some reason.*/
  994. public static BigInteger sqrt(BigInteger x) {
  995. if(x.equals(BigInteger.ZERO)){
  996. return BigInteger.ZERO;
  997. }
  998. BigInteger div = BigInteger.ZERO.setBit(x.bitLength()/2);
  999. BigInteger div2 = div;
  1000. BigInteger returnvalue = BigInteger.valueOf(0);
  1001. // Loop until we hit the same value twice in a row, or wind
  1002. // up alternating.
  1003. for(;;) {
  1004. BigInteger y = div.add(x.divide(div)).shiftRight(1);
  1005. if (y.equals(div) || y.equals(div2)){
  1006. returnvalue = y;
  1007. break;
  1008. }
  1009. div2 = div;
  1010. div = y;
  1011. }
  1012.  
  1013. BigInteger testsquare = returnvalue.multiply(returnvalue);
  1014. BigInteger testp1square = (returnvalue.add(BigInteger.valueOf(1))).multiply(returnvalue.add(BigInteger.valueOf(1)));
  1015. if((testsquare.equals(x)) || (((testsquare.compareTo(x)) == -1) && ((testp1square.compareTo(x)) == 1))){
  1016. return returnvalue;
  1017. } else {
  1018. return returnvalue.subtract(BigInteger.valueOf(1));
  1019. }
  1020. }
  1021. }
  1022.  
  1023. /**
  1024. * Things to potentially add:
  1025. * - all (0,n) cells for d*d
  1026. * - the (e,n) cell for which (e+2n,n) is the solution record for each ab pair
  1027. * - the (f,n) cell for which (f+2(n-1),n) is the solution record for each ab pair
  1028. */
Advertisement
Add Comment
Please, Sign In to add comment