Guest User

everything.java

a guest
Dec 7th, 2018
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 21.29 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class everything{
  4. private static int buffer = 0;
  5. private static boolean all = true;
  6.  
  7. public static void main(String[] args){
  8. Scanner scan = new Scanner(System.in);
  9. System.out.println("a=");
  10. int ain = 0;
  11. while(ain==0){
  12. ain = scan.nextInt();
  13. }
  14. System.out.println("b=");
  15. int bin = 0;
  16. while(bin==0){
  17. bin = scan.nextInt();
  18. }
  19. GridCell cell = new GridCell(ain, bin);
  20. System.out.println(cell.description());
  21. System.out.println();
  22. System.out.println("=========================================");
  23. System.out.println();
  24. System.out.println("c = " + cell.c() + doeverything(cell.c()));
  25.  
  26. buffer++;
  27.  
  28. recursed(cell.d());
  29. recursee(cell.e());
  30. }
  31.  
  32. public static void recursed(int d){
  33. String dstring = "";
  34. if(d>0 && d%2==0){
  35. while(d%4!=0 && d%2==0){
  36. dstring += d + " / ";
  37. d=d/2;
  38. }
  39. }
  40. dstring += d;
  41. for(int i=0; i<buffer; i++){
  42. System.out.print(" ");
  43. }
  44. if(all){
  45. System.out.print("d = " + dstring);
  46. }
  47. if(d>2){
  48. System.out.println(" ---" + doeverything(d));
  49. int d2 = (int)Math.sqrt(d);
  50. int e = d-(d2*d2);
  51. buffer++;
  52. recursed(d2);
  53. recursee(e);
  54. buffer--;
  55. } else {
  56. System.out.println();
  57. }
  58. }
  59.  
  60. public static void recursee(int e){
  61. String estring = "";
  62. if(e>0 && e%2==0){
  63. while(e%4!=0 && e%2==0){
  64. estring += e + " / ";
  65. e=e/2;
  66. }
  67. }
  68. estring += e;
  69. for(int i=0; i<buffer; i++){
  70. System.out.print(" ");
  71. }
  72. if(all){
  73. System.out.print("e = " + estring);
  74. }
  75. if(e>2){
  76. System.out.println(" ---" + doeverything(e));
  77. int d = (int)Math.sqrt(e);
  78. int e2 = e-(d*d);
  79. buffer++;
  80. recursed(d);
  81. recursee(e2);
  82. buffer--;
  83. } else {
  84. System.out.println();
  85. System.out.println();
  86. }
  87. }
  88.  
  89. public static String doeverything(int c){
  90. ArrayList<ArrayList<Integer>> factors = factorize(c);
  91. String thing = "\n";
  92. thing += forc(c);
  93. thing += forpairs(factors);
  94.  
  95. return thing;
  96. }
  97.  
  98. public static String forc(int c){
  99. String thing = "";
  100. int d = (int)Math.sqrt(c);
  101. int e = c-(d*d);
  102. int f = e-((2*d)+1);
  103.  
  104. //Root of d
  105. thing += bufferadd();
  106. int rootofd_a = d;
  107. int rootofd_b = 9*d;
  108. GridCell rootofd = new GridCell(rootofd_a, rootofd_b);
  109. thing += "Root of d: " + rootofd.description();
  110. thing += "\n";
  111.  
  112. //BigN and BigN-1
  113. thing += bufferadd();
  114. int bign = ((c+1)/2)-d;
  115. thing += "BigN = " + bign + ", BigN-1 = " + (bign-1);
  116. thing += "\n";
  117.  
  118. //The cells in (e,1) and (f,1) where a[t]=BigN and a[t]=BigN-1
  119. thing += bufferadd();
  120. GridCell e1_bign = e1cell(e, bign);
  121. thing += "(e,1) a=BigN = ";
  122. if(e1_bign.c()<0){
  123. thing += "invalid - int overflow";
  124. } else {
  125. thing += e1_bign.description() + " --- " + squaresum(e1_bign);
  126. }
  127. thing += "\n";
  128. thing += bufferadd();
  129. GridCell f1_bign = f1cell(f, bign-1);
  130. thing += "(f,1) a=BigN-1 = ";
  131. if(f1_bign.c()<0){
  132. thing += "invalid - int overflow";
  133. } else {
  134. thing += f1_bign.description() + " --- " + squaresum(f1_bign);
  135. }
  136. thing += "\n";
  137.  
  138. //The cells in (e,1) and (f,1) where a[t]=c*BigN and c*(BigN-1)
  139. thing += bufferadd();
  140. int cbign = c*bign;
  141. int cbignm1 = c*(bign-1);
  142. GridCell e1_cbign = e1cell(e, cbign);
  143. thing += "(e,1) a=c*BigN = ";
  144. if(e1_cbign.c()<0){
  145. thing += "invalid - int overflow";
  146. } else {
  147. thing += e1_cbign.description() + " --- " + squaresum(e1_cbign);
  148. }
  149. thing += "\n";
  150. thing += bufferadd();
  151. GridCell f1_cbignm1 = f1cell(f, cbignm1);
  152. thing += "(f,1) a=c*(BigN-1) = ";
  153. if(f1_cbignm1.c()<0){
  154. thing += "invalid - int overflow";
  155. } else {
  156. thing += f1_cbignm1.description() + " --- " + squaresum(f1_cbignm1);
  157. }
  158. thing += "\n";
  159.  
  160. //The cells in (e,1) and (f,1) where a[t]=c(c-BigN+1) and c(c-BigN+2)
  161. thing += bufferadd();
  162. int cshadown = c*(c-bign+1);
  163. int cshadownp1 = c*(c-bign+2);
  164. GridCell e1_cshadown = e1cell(e, cshadown);
  165. thing += "(e,1) a=c*(c-BigN+1) = ";
  166. if(e1_cshadown.c()<0){
  167. thing += "invalid - int overflow";
  168. } else {
  169. thing += e1_cshadown.description() + " --- " + squaresum(e1_cshadown);
  170. }
  171. thing += "\n";
  172. thing += bufferadd();
  173. GridCell f1_cshadownp1 = e1cell(f, cshadownp1);
  174. thing += "(f,1) a=c*(c-BigN+2) = ";
  175. if(e1_bign.c()<0){
  176. thing += "invalid - int overflow";
  177. } else {
  178. thing += f1_cshadownp1.description() + " --- " + squaresum(f1_cshadownp1);
  179. }
  180. thing += "\n";
  181.  
  182. //The cells in (e,1) and (f,1) where x+x=2f+1
  183. int twofp1 = (f*-2)+1;
  184. thing += xplusxstring("2f+1 (x=f or f+1)", twofp1, "filler", 2, e, f);
  185.  
  186. //The cells in (e,1) and (f,1) where x+x=2d+1
  187. int twodp1 = (d*2)+1;
  188. thing += xplusxstring("2d+1", twodp1, "filler", 2, e, f);
  189.  
  190. //The cells in (e,1) and (f,1) where x+x=BigN or BigN-1
  191. thing += xplusxstring("BigN", bign, "BigN-1", (bign-1), e, f);
  192.  
  193. //The cells in (e,1) and (f,1) where x+x=c*BigN or c*BigN-1
  194. thing += xplusxstring("c*BigN", cbign, "c*(BigN-1)", cbignm1, e, f);
  195.  
  196. //The cells in (e,1) and (f,1) where x+x=c(c-BigN+1) or c(c-BigN+2)
  197. thing += xplusxstring("c*(c-BigN+1)", cshadown, "c*(c-BigN+2)", cshadownp1, e, f);
  198.  
  199. return thing;
  200. }
  201.  
  202. public static String forpairs(ArrayList<ArrayList<Integer>> factors){
  203. String thing = "";
  204. for(int i=0; i<factors.size(); i++){
  205. if((factors.get(i).get(0)+factors.get(i).get(1))%2==0){
  206. int a = factors.get(i).get(0);
  207. int b = factors.get(i).get(1);
  208.  
  209. thing += bufferadd();
  210. thing += factors.get(i);
  211. thing += "\n";
  212.  
  213. buffer++;
  214.  
  215. //The (e,n) cell
  216. GridCell paircell = new GridCell(a, b);
  217. thing += bufferadd();
  218. thing += "(e,n) cell = " + paircell.description();
  219. thing += "\n";
  220.  
  221. //(a-1)(b-1) and /2
  222. int a1b1 = (a-1)*(b-1);
  223. int a1b1over2 = a1b1/2;
  224. thing += bufferadd();
  225. thing += "(a-1)(b-1) = " + a1b1 + ", /2 (BigN-n) = " + a1b1over2;
  226. thing += "\n";
  227.  
  228. //the (x+n)(x+n) square, its triangles, and its nn+2d(n-1)+f-1 configuration
  229. int xplusnsquared = paircell.xplusnsquared();
  230. int tri1 = 0;
  231. int tri2 = 0;
  232. String triout = "error in trioutput";
  233. if(xplusnsquared%2==0){
  234. int overfour = xplusnsquared/4;
  235. for(int k=0; k<overfour; k++){
  236. int testtri1 = (k*(k-1))/2;
  237. int testtri2 = ((k-1)*(k-2))/2;
  238. if(overfour == testtri1+testtri2){
  239. tri1 = testtri1;
  240. tri2 = testtri2;
  241. triout = "(x+n)(x+n) is " + xplusnsquared + ", which is (4*" + tri1 + ")+(4*" + tri2 + ") (u=" + (k-1) + "&" + k + ")";
  242. }
  243. }
  244. } else {
  245. int minusone = xplusnsquared-1;
  246. tri1 = minusone/8;
  247. triout = "(x+n)(x+n) is " + xplusnsquared + ", which is (8*" + tri1 + ")+1 (u=" + paircell.u() + ")";
  248. }
  249. triout+="\n";
  250. triout+=bufferadd();
  251. triout+="nn+2d(n-1)+f-1 = " + (paircell.n()*paircell.n()) + "+" + (2*paircell.d()*(paircell.n()-1)) + "+" + (paircell.f()*-1) + "-1";
  252. thing += bufferadd();
  253. thing += triout;
  254. thing += "\n";
  255.  
  256. //the 2(x+n)2(x+n) square, its triangles, and its nn+2d(n-1)+f-1 configuration
  257. int xplusnsquared2 = (2*paircell.xplusn())*(2*paircell.xplusn());
  258. String triout2 = "error in trioutput2";
  259. int tri12 = 0;
  260. int tri22 = 0;
  261. int overfour2 = xplusnsquared2/4; //since it's *2 it'll always be evens
  262. for(int k2 = 0; k2<overfour2; k2++){
  263. int testtri12 = (k2*(k2-1))/2;
  264. int testtri22 = ((k2-1)*(k2-2))/2;
  265. if(overfour2 == testtri12+testtri22){
  266. tri12 = testtri12;
  267. tri22 = testtri22;
  268. triout2 = "(2(x+n))(2(x+n)) is " + xplusnsquared2 + ", which is (4*" + tri12 + ")+(4*" + tri22 + ") (u=" + (k2-1) + "&" + k2 + ")";
  269. }
  270. }
  271.  
  272. thing += bufferadd();
  273. thing += triout2;
  274. thing += "\n";
  275.  
  276. //(0,n) a=aa b=bb
  277. thing += bufferadd();
  278. GridCell zeron_aabb_cell = zeron_aabb(paircell);
  279. int n_aabb = zeron_aabb_cell.n();
  280. thing += "(0,n) aa,bb = ";
  281. if(zeron_aabb_cell.c() < 0){
  282. thing += "invalid - int overflow";
  283. } else {
  284. thing += zeron_aabb_cell.description();
  285. }
  286. thing += "\n";
  287.  
  288. //(0,n) a=ab b=ab
  289. thing += bufferadd();
  290. GridCell zeron_ab_ab_cell = zeron_ab_ab(paircell);
  291. thing += "(0,n) ab,ab = ";
  292. if(zeron_ab_ab_cell.c() < 0){
  293. thing += "invalid - int overflow";
  294. } else {
  295. thing += zeron_ab_ab_cell.description();
  296. }
  297. thing += "\n";
  298.  
  299. //(0,n) a=a b=abb
  300. thing += bufferadd();
  301. GridCell zeron_a_abb_cell = zeron_a_abb(paircell);
  302. thing += "(0,n) a,abb = ";
  303. if(zeron_a_abb_cell.c() < 0){
  304. thing += "invalid - int overflow";
  305. } else {
  306. thing += zeron_a_abb_cell.description();
  307. }
  308. thing += "\n";
  309.  
  310. //(0,n) a=b b=aab
  311. thing += bufferadd();
  312. GridCell zeron_b_aab_cell = zeron_b_aab(paircell);
  313. thing += "(0,n) b,aab = ";
  314. if(zeron_b_aab_cell.c() < 0){
  315. thing += "invalid - int overflow";
  316. } else {
  317. thing += zeron_b_aab_cell.description();
  318. }
  319. thing += "\n";
  320.  
  321. //(0,n) a=1 b=cc
  322. thing += bufferadd();
  323. GridCell zeron_1cc_cell = zeron_1cc(paircell);
  324. thing += "(0,n) 1,cc = ";
  325. if(zeron_1cc_cell.c() < 0){
  326. thing += "invalid - int overflow";
  327. } else {
  328. thing += zeron_1cc_cell.description();
  329. }
  330. thing += "\n";
  331.  
  332. //the cell in (1,1) where d is equal to the n value for the aa bb cell
  333. thing += bufferadd();
  334. GridCell oneone_d_aabbn_cell = oneone_d_aabbn(n_aabb);
  335. thing += "(1,1) d=aabbn = ";
  336. if(oneone_d_aabbn_cell.c() < 0){
  337. thing += "invalid - int overflow";
  338. } else {
  339. thing += oneone_d_aabbn_cell.description();
  340. }
  341. thing += "\n";
  342.  
  343. //the cell in (0,1) where a is equal to the n value for the aa bb cell
  344. thing += bufferadd();
  345. GridCell zeroone_a_aabbn_cell = zeroone_a_aabbn(n_aabb);
  346. thing += "(0,1) a=aabbn = ";
  347. if(zeroone_a_aabbn_cell.c() < 0){
  348. thing += "invalid - int overflow";
  349. } else {
  350. thing += zeroone_a_aabbn_cell.description();
  351. }
  352. thing += "\n";
  353.  
  354. //(e,1) na
  355. thing += bufferadd();
  356. GridCell e1_na = e1cell(paircell.e(), paircell.n()*paircell.a());
  357. thing += "(e,1) a=na = ";
  358. if(e1_na.c() < 0){
  359. thing += "invalid - int overflow";
  360. } else {
  361. thing += e1_na.description() + " --- " + squaresum(e1_na);
  362. }
  363. thing += "\n";
  364.  
  365. //(f,1) a(n-1)
  366. thing += bufferadd();
  367. GridCell f1_na = f1cell(paircell.f(), (paircell.n()-1)*paircell.a());
  368. thing += "(f,1) a=a(n-1) = ";
  369. if(f1_na.c() < 0){
  370. thing += "invalid - int overflow";
  371. } else {
  372. thing += f1_na.description() + " --- " + squaresum(f1_na);
  373. }
  374. thing += "\n";
  375.  
  376. //(e,1) bn
  377. thing += bufferadd();
  378. GridCell e1_bn = e1cell(paircell.e(), paircell.b()*paircell.n());
  379. thing += "(e,1) a=bn = ";
  380. if(e1_bn.c() < 0){
  381. thing += "invalid - int overflow";
  382. } else {
  383. thing += e1_bn.description() + " --- " + squaresum(e1_bn);
  384. }
  385. thing += "\n";
  386.  
  387. //(f,1) b(n-1)
  388. thing += bufferadd();
  389. GridCell f1_bn = f1cell(paircell.f(), (paircell.n()-1)*paircell.b());
  390. thing += "(f,1) a=b(n-1) = ";
  391. if(f1_bn.c() < 0){
  392. thing += "invalid - int overflow";
  393. } else {
  394. thing += f1_bn.description() + " --- " + squaresum(f1_bn);
  395. }
  396. thing += "\n";
  397.  
  398. //the cells in (e,1) and (f,1) for which x+x=an or a(n-1) and the same for bn or b(n-1) (whichever's odd, if any)
  399. thing += xplusxstring("an", e1_na.a(), "a(n-1)", f1_na.a(), paircell.e(), paircell.f());
  400. thing += xplusxstring("bn", e1_bn.a(), "b(n-1)", f1_bn.a(), paircell.e(), paircell.f());
  401.  
  402. buffer--;
  403. }
  404. }
  405. return thing;
  406. }
  407.  
  408. public static ArrayList<ArrayList<Integer>> factorize(int c){
  409. ArrayList<ArrayList<Integer>> factors = new ArrayList<ArrayList<Integer>>();
  410. int highest = c;
  411. for(int i=1; i<highest; i++){
  412. if(c%i==0){
  413. ArrayList<Integer> pair = new ArrayList<Integer>();
  414. pair.add(i);
  415. pair.add(c/i);
  416. factors.add(pair);
  417. highest = c/i;
  418. }
  419. }
  420. return factors;
  421. }
  422.  
  423. public static String bufferadd(){
  424. String returnbuffer = "";
  425. for(int i=0; i<buffer+1; i++){
  426. returnbuffer+=" ";
  427. }
  428. return returnbuffer;
  429. }
  430.  
  431. public static GridCell zeron_aabb(GridCell cell){
  432. int a = cell.a()*cell.a();
  433. int b = cell.b()*cell.b();
  434. GridCell returncell = new GridCell(a,b);
  435. return returncell;
  436. }
  437.  
  438. public static GridCell zeron_1cc(GridCell cell){
  439. int a = 1;
  440. int b = cell.c()*cell.c();
  441. GridCell returncell = new GridCell(a,b);
  442. return returncell;
  443. }
  444.  
  445. public static GridCell zeron_a_abb(GridCell cell){
  446. int a = cell.a();
  447. int b = cell.b()*cell.b()*cell.a();
  448. GridCell returncell = new GridCell(a,b);
  449. return returncell;
  450. }
  451.  
  452. public static GridCell zeron_ab_ab(GridCell cell){
  453. int a = cell.a()*cell.b();
  454. int b = cell.a()*cell.b();
  455. GridCell returncell = new GridCell(a,b);
  456. return returncell;
  457. }
  458.  
  459. public static GridCell zeron_b_aab(GridCell cell){
  460. int a = cell.b();
  461. int b = cell.b()*cell.a()*cell.a();
  462. GridCell returncell = new GridCell(a,b);
  463. return returncell;
  464. }
  465.  
  466. public static GridCell oneone_d_aabbn(int d){
  467. int e = 1;
  468. int n = 1;
  469. int f = -1*(e-((2*d)+1));
  470. int rootf = (int)Math.sqrt(f);
  471. int x = rootf-1;
  472. int a = d-x;
  473. int b = a+(2*x)+(2*n);
  474. GridCell returncell = new GridCell(a,b);
  475. return returncell;
  476. }
  477.  
  478. public static GridCell zeroone_a_aabbn(int a){
  479. int e = 0;
  480. int n = 1;
  481. int x = (int)Math.sqrt((2*n*a)-e);
  482. int b = a+(2*x)+(2*n);
  483. GridCell returncell = new GridCell(a,b);
  484. return returncell;
  485. }
  486.  
  487. public static String squaresum(GridCell cell){
  488. int e = cell.e();
  489. int a = cell.a();
  490. int t = cell.t();
  491. String returnstring = "";
  492. if(e%2==0){
  493. int eover2 = e/2;
  494. int asquares = a-eover2;
  495. int asquare = asquares/2;
  496. returnstring = "a[t] = sq+sq+e/2 = " + asquare + "+" + asquare + "+" + eover2;
  497. } else {
  498. int eminus1over2 = (e-1)/2;
  499. int afirstsquare = (t-1)*(t-1);
  500. int asecondsquare = t*t;
  501. returnstring = "a[t] = sq+(sq)+(e-1)/2 = " + afirstsquare + "+" + asecondsquare + "+" + eminus1over2;
  502. }
  503. return returnstring;
  504. }
  505.  
  506. public static GridCell e1cell(int e, int value){
  507. int n = 1;
  508. int a = value;
  509. int x = (int)Math.sqrt((2*a*n)-e);
  510. int b = a+(2*x)+(2*n);
  511. GridCell returncell = new GridCell(a,b);
  512. return returncell;
  513. }
  514.  
  515. public static GridCell f1cell(int f, int value){
  516. int n = 1;
  517. int a = value;
  518. int x = (int)Math.sqrt((2*a*n)-f);
  519. int b = a+(2*x)+(2*n);
  520. GridCell returncell = new GridCell(a,b,f);
  521. return returncell;
  522. }
  523.  
  524. public static ArrayList<GridCell> xplusx(int value, int e, int f){
  525. if(value%2==0){
  526. value+=1; //not the best fix but I'll only be using it for odds anyway
  527. }
  528. int halflow = value/2;
  529. int halfhigh = halflow+1; //the two values that add to make value
  530. int xneg = 0;
  531. int xpos = 0;
  532. if(e%2==0){ //e has even x, f has odd x
  533. if(halflow%2==0){
  534. xpos = halflow;
  535. xneg = halfhigh;
  536. } else {
  537. xpos = halfhigh;
  538. xneg = halflow;
  539. }
  540. } else { //e has odd x, f has even x
  541. if(halflow%2==0){
  542. xpos = halfhigh;
  543. xneg = halflow;
  544. } else {
  545. xpos = halflow;
  546. xneg = halfhigh;
  547. }
  548. }
  549.  
  550. int n = 1;
  551. int aneg = ((xneg*xneg)+f)/2;
  552. int apos = ((xpos*xpos)+e)/2;
  553. int bneg = aneg+2+(2*xneg);
  554. int bpos = apos+2+(2*xpos);
  555. GridCell fcell = new GridCell(aneg, bneg, f);
  556. GridCell ecell = new GridCell(apos, bpos);
  557.  
  558. ArrayList<GridCell> returncells = new ArrayList<GridCell>();
  559. returncells.add(fcell);
  560. returncells.add(ecell);
  561. return returncells;
  562. }
  563.  
  564. public static String xplusxstring(String type1, int value1, String type2, int value2, int e, int f){
  565. String thing = "";
  566. thing += bufferadd();
  567. if(value1%2!=0){
  568. thing += "The cells in (e,1) and (f,1) where x+x=" + type1 + "=" + value1 + "\n";
  569. buffer++;
  570. thing += bufferadd();
  571. ArrayList<GridCell> pair = xplusx(value1, e, f);
  572. thing += pair.get(0).description() + "\n" + bufferadd();
  573. thing += pair.get(1).description() + "\n";
  574. buffer--;
  575. } else if(value2%2!=0){
  576. thing += "The cells in (e,1) and (f,1) where x+x=" + type2 + "=" + value2 + "\n";
  577. buffer++;
  578. thing += bufferadd();
  579. ArrayList<GridCell> pair = xplusx(value2, e, f);
  580. thing += pair.get(0).description() + "\n" + bufferadd();
  581. thing += pair.get(1).description() + "\n";
  582. buffer--;
  583. } else {
  584. thing += "Neither of " + type1 + " and " + type2 + " are odd, so here's where x+x=" + type1 + "+1=" + (value1+1) + " (using x from (e,1) and (f,1)):\n";
  585. buffer++;
  586. thing += bufferadd();
  587. ArrayList<GridCell> pair = xplusx(value1+1, e, f);
  588. thing += pair.get(0).description() + "\n" + bufferadd();
  589. thing += pair.get(1).description() + "\n";
  590. buffer--;
  591. }
  592. return thing;
  593. }
  594. }
  595.  
  596. /**
  597. * Things to potentially add:
  598. * - all (0,n) cells for d*d
  599. * - the (e,n) cell for which (e+2n,n) is the solution record for each ab pair
  600. * - the (f,n) cell for which (f+2(n-1),n) is the solution record for each ab pair
  601. */
Advertisement
Add Comment
Please, Sign In to add comment