Guest User

everythingbig.java

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