Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class everything{
- private static int buffer = 0;
- private static boolean all = true;
- public static void main(String[] args){
- Scanner scan = new Scanner(System.in);
- System.out.println("a=");
- int ain = 0;
- while(ain==0){
- ain = scan.nextInt();
- }
- System.out.println("b=");
- int bin = 0;
- while(bin==0){
- bin = scan.nextInt();
- }
- GridCell cell = new GridCell(ain, bin);
- System.out.println(cell.description());
- System.out.println();
- System.out.println("=========================================");
- System.out.println();
- System.out.println("c = " + cell.c() + doeverything(cell.c()));
- buffer++;
- recursed(cell.d());
- recursee(cell.e());
- }
- public static void recursed(int d){
- String dstring = "";
- if(d>0 && d%2==0){
- while(d%4!=0 && d%2==0){
- dstring += d + " / ";
- d=d/2;
- }
- }
- dstring += d;
- for(int i=0; i<buffer; i++){
- System.out.print(" ");
- }
- if(all){
- System.out.print("d = " + dstring);
- }
- if(d>2){
- System.out.println(" ---" + doeverything(d));
- int d2 = (int)Math.sqrt(d);
- int e = d-(d2*d2);
- buffer++;
- recursed(d2);
- recursee(e);
- buffer--;
- } else {
- System.out.println();
- }
- }
- public static void recursee(int e){
- String estring = "";
- if(e>0 && e%2==0){
- while(e%4!=0 && e%2==0){
- estring += e + " / ";
- e=e/2;
- }
- }
- estring += e;
- for(int i=0; i<buffer; i++){
- System.out.print(" ");
- }
- if(all){
- System.out.print("e = " + estring);
- }
- if(e>2){
- System.out.println(" ---" + doeverything(e));
- int d = (int)Math.sqrt(e);
- int e2 = e-(d*d);
- buffer++;
- recursed(d);
- recursee(e2);
- buffer--;
- } else {
- System.out.println();
- System.out.println();
- }
- }
- public static String doeverything(int c){
- ArrayList<ArrayList<Integer>> factors = factorize(c);
- String thing = "\n";
- thing += forc(c);
- thing += forpairs(factors);
- return thing;
- }
- public static String forc(int c){
- String thing = "";
- int d = (int)Math.sqrt(c);
- int e = c-(d*d);
- int f = e-((2*d)+1);
- //Root of d
- thing += bufferadd();
- int rootofd_a = d;
- int rootofd_b = 9*d;
- GridCell rootofd = new GridCell(rootofd_a, rootofd_b);
- thing += "Root of d: " + rootofd.description();
- thing += "\n";
- //BigN and BigN-1
- thing += bufferadd();
- int bign = ((c+1)/2)-d;
- thing += "BigN = " + bign + ", BigN-1 = " + (bign-1);
- thing += "\n";
- //The cells in (e,1) and (f,1) where a[t]=BigN and a[t]=BigN-1
- thing += bufferadd();
- GridCell e1_bign = e1cell(e, bign);
- thing += "(e,1) a=BigN = ";
- if(e1_bign.c()<0){
- thing += "invalid - int overflow";
- } else {
- thing += e1_bign.description() + " --- " + squaresum(e1_bign);
- }
- thing += "\n";
- thing += bufferadd();
- GridCell f1_bign = f1cell(f, bign-1);
- thing += "(f,1) a=BigN-1 = ";
- if(f1_bign.c()<0){
- thing += "invalid - int overflow";
- } else {
- thing += f1_bign.description() + " --- " + squaresum(f1_bign);
- }
- thing += "\n";
- //The cells in (e,1) and (f,1) where a[t]=c*BigN and c*(BigN-1)
- thing += bufferadd();
- int cbign = c*bign;
- int cbignm1 = c*(bign-1);
- GridCell e1_cbign = e1cell(e, cbign);
- thing += "(e,1) a=c*BigN = ";
- if(e1_cbign.c()<0){
- thing += "invalid - int overflow";
- } else {
- thing += e1_cbign.description() + " --- " + squaresum(e1_cbign);
- }
- thing += "\n";
- thing += bufferadd();
- GridCell f1_cbignm1 = f1cell(f, cbignm1);
- thing += "(f,1) a=c*(BigN-1) = ";
- if(f1_cbignm1.c()<0){
- thing += "invalid - int overflow";
- } else {
- thing += f1_cbignm1.description() + " --- " + squaresum(f1_cbignm1);
- }
- thing += "\n";
- //The cells in (e,1) and (f,1) where a[t]=c(c-BigN+1) and c(c-BigN+2)
- thing += bufferadd();
- int cshadown = c*(c-bign+1);
- int cshadownp1 = c*(c-bign+2);
- GridCell e1_cshadown = e1cell(e, cshadown);
- thing += "(e,1) a=c*(c-BigN+1) = ";
- if(e1_cshadown.c()<0){
- thing += "invalid - int overflow";
- } else {
- thing += e1_cshadown.description() + " --- " + squaresum(e1_cshadown);
- }
- thing += "\n";
- thing += bufferadd();
- GridCell f1_cshadownp1 = e1cell(f, cshadownp1);
- thing += "(f,1) a=c*(c-BigN+2) = ";
- if(e1_bign.c()<0){
- thing += "invalid - int overflow";
- } else {
- thing += f1_cshadownp1.description() + " --- " + squaresum(f1_cshadownp1);
- }
- thing += "\n";
- //The cells in (e,1) and (f,1) where x+x=2f+1
- int twofp1 = (f*-2)+1;
- thing += xplusxstring("2f+1 (x=f or f+1)", twofp1, "filler", 2, e, f);
- //The cells in (e,1) and (f,1) where x+x=2d+1
- int twodp1 = (d*2)+1;
- thing += xplusxstring("2d+1", twodp1, "filler", 2, e, f);
- //The cells in (e,1) and (f,1) where x+x=BigN or BigN-1
- thing += xplusxstring("BigN", bign, "BigN-1", (bign-1), e, f);
- //The cells in (e,1) and (f,1) where x+x=c*BigN or c*BigN-1
- thing += xplusxstring("c*BigN", cbign, "c*(BigN-1)", cbignm1, e, f);
- //The cells in (e,1) and (f,1) where x+x=c(c-BigN+1) or c(c-BigN+2)
- thing += xplusxstring("c*(c-BigN+1)", cshadown, "c*(c-BigN+2)", cshadownp1, e, f);
- return thing;
- }
- public static String forpairs(ArrayList<ArrayList<Integer>> factors){
- String thing = "";
- for(int i=0; i<factors.size(); i++){
- if((factors.get(i).get(0)+factors.get(i).get(1))%2==0){
- int a = factors.get(i).get(0);
- int b = factors.get(i).get(1);
- thing += bufferadd();
- thing += factors.get(i);
- thing += "\n";
- buffer++;
- //The (e,n) cell
- GridCell paircell = new GridCell(a, b);
- thing += bufferadd();
- thing += "(e,n) cell = " + paircell.description();
- thing += "\n";
- //(a-1)(b-1) and /2
- int a1b1 = (a-1)*(b-1);
- int a1b1over2 = a1b1/2;
- thing += bufferadd();
- thing += "(a-1)(b-1) = " + a1b1 + ", /2 (BigN-n) = " + a1b1over2;
- thing += "\n";
- //the (x+n)(x+n) square, its triangles, and its nn+2d(n-1)+f-1 configuration
- int xplusnsquared = paircell.xplusnsquared();
- int tri1 = 0;
- int tri2 = 0;
- String triout = "error in trioutput";
- if(xplusnsquared%2==0){
- int overfour = xplusnsquared/4;
- for(int k=0; k<overfour; k++){
- int testtri1 = (k*(k-1))/2;
- int testtri2 = ((k-1)*(k-2))/2;
- if(overfour == testtri1+testtri2){
- tri1 = testtri1;
- tri2 = testtri2;
- triout = "(x+n)(x+n) is " + xplusnsquared + ", which is (4*" + tri1 + ")+(4*" + tri2 + ") (u=" + (k-1) + "&" + k + ")";
- }
- }
- } else {
- int minusone = xplusnsquared-1;
- tri1 = minusone/8;
- triout = "(x+n)(x+n) is " + xplusnsquared + ", which is (8*" + tri1 + ")+1 (u=" + paircell.u() + ")";
- }
- triout+="\n";
- triout+=bufferadd();
- triout+="nn+2d(n-1)+f-1 = " + (paircell.n()*paircell.n()) + "+" + (2*paircell.d()*(paircell.n()-1)) + "+" + (paircell.f()*-1) + "-1";
- thing += bufferadd();
- thing += triout;
- thing += "\n";
- //the 2(x+n)2(x+n) square, its triangles, and its nn+2d(n-1)+f-1 configuration
- int xplusnsquared2 = (2*paircell.xplusn())*(2*paircell.xplusn());
- String triout2 = "error in trioutput2";
- int tri12 = 0;
- int tri22 = 0;
- int overfour2 = xplusnsquared2/4; //since it's *2 it'll always be evens
- for(int k2 = 0; k2<overfour2; k2++){
- int testtri12 = (k2*(k2-1))/2;
- int testtri22 = ((k2-1)*(k2-2))/2;
- if(overfour2 == testtri12+testtri22){
- tri12 = testtri12;
- tri22 = testtri22;
- triout2 = "(2(x+n))(2(x+n)) is " + xplusnsquared2 + ", which is (4*" + tri12 + ")+(4*" + tri22 + ") (u=" + (k2-1) + "&" + k2 + ")";
- }
- }
- thing += bufferadd();
- thing += triout2;
- thing += "\n";
- //(0,n) a=aa b=bb
- thing += bufferadd();
- GridCell zeron_aabb_cell = zeron_aabb(paircell);
- int n_aabb = zeron_aabb_cell.n();
- thing += "(0,n) aa,bb = ";
- if(zeron_aabb_cell.c() < 0){
- thing += "invalid - int overflow";
- } else {
- thing += zeron_aabb_cell.description();
- }
- thing += "\n";
- //(0,n) a=ab b=ab
- thing += bufferadd();
- GridCell zeron_ab_ab_cell = zeron_ab_ab(paircell);
- thing += "(0,n) ab,ab = ";
- if(zeron_ab_ab_cell.c() < 0){
- thing += "invalid - int overflow";
- } else {
- thing += zeron_ab_ab_cell.description();
- }
- thing += "\n";
- //(0,n) a=a b=abb
- thing += bufferadd();
- GridCell zeron_a_abb_cell = zeron_a_abb(paircell);
- thing += "(0,n) a,abb = ";
- if(zeron_a_abb_cell.c() < 0){
- thing += "invalid - int overflow";
- } else {
- thing += zeron_a_abb_cell.description();
- }
- thing += "\n";
- //(0,n) a=b b=aab
- thing += bufferadd();
- GridCell zeron_b_aab_cell = zeron_b_aab(paircell);
- thing += "(0,n) b,aab = ";
- if(zeron_b_aab_cell.c() < 0){
- thing += "invalid - int overflow";
- } else {
- thing += zeron_b_aab_cell.description();
- }
- thing += "\n";
- //(0,n) a=1 b=cc
- thing += bufferadd();
- GridCell zeron_1cc_cell = zeron_1cc(paircell);
- thing += "(0,n) 1,cc = ";
- if(zeron_1cc_cell.c() < 0){
- thing += "invalid - int overflow";
- } else {
- thing += zeron_1cc_cell.description();
- }
- thing += "\n";
- //the cell in (1,1) where d is equal to the n value for the aa bb cell
- thing += bufferadd();
- GridCell oneone_d_aabbn_cell = oneone_d_aabbn(n_aabb);
- thing += "(1,1) d=aabbn = ";
- if(oneone_d_aabbn_cell.c() < 0){
- thing += "invalid - int overflow";
- } else {
- thing += oneone_d_aabbn_cell.description();
- }
- thing += "\n";
- //the cell in (0,1) where a is equal to the n value for the aa bb cell
- thing += bufferadd();
- GridCell zeroone_a_aabbn_cell = zeroone_a_aabbn(n_aabb);
- thing += "(0,1) a=aabbn = ";
- if(zeroone_a_aabbn_cell.c() < 0){
- thing += "invalid - int overflow";
- } else {
- thing += zeroone_a_aabbn_cell.description();
- }
- thing += "\n";
- //(e,1) na
- thing += bufferadd();
- GridCell e1_na = e1cell(paircell.e(), paircell.n()*paircell.a());
- thing += "(e,1) a=na = ";
- if(e1_na.c() < 0){
- thing += "invalid - int overflow";
- } else {
- thing += e1_na.description() + " --- " + squaresum(e1_na);
- }
- thing += "\n";
- //(f,1) a(n-1)
- thing += bufferadd();
- GridCell f1_na = f1cell(paircell.f(), (paircell.n()-1)*paircell.a());
- thing += "(f,1) a=a(n-1) = ";
- if(f1_na.c() < 0){
- thing += "invalid - int overflow";
- } else {
- thing += f1_na.description() + " --- " + squaresum(f1_na);
- }
- thing += "\n";
- //(e,1) bn
- thing += bufferadd();
- GridCell e1_bn = e1cell(paircell.e(), paircell.b()*paircell.n());
- thing += "(e,1) a=bn = ";
- if(e1_bn.c() < 0){
- thing += "invalid - int overflow";
- } else {
- thing += e1_bn.description() + " --- " + squaresum(e1_bn);
- }
- thing += "\n";
- //(f,1) b(n-1)
- thing += bufferadd();
- GridCell f1_bn = f1cell(paircell.f(), (paircell.n()-1)*paircell.b());
- thing += "(f,1) a=b(n-1) = ";
- if(f1_bn.c() < 0){
- thing += "invalid - int overflow";
- } else {
- thing += f1_bn.description() + " --- " + squaresum(f1_bn);
- }
- thing += "\n";
- //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)
- thing += xplusxstring("an", e1_na.a(), "a(n-1)", f1_na.a(), paircell.e(), paircell.f());
- thing += xplusxstring("bn", e1_bn.a(), "b(n-1)", f1_bn.a(), paircell.e(), paircell.f());
- buffer--;
- }
- }
- return thing;
- }
- public static ArrayList<ArrayList<Integer>> factorize(int c){
- ArrayList<ArrayList<Integer>> factors = new ArrayList<ArrayList<Integer>>();
- int highest = c;
- for(int i=1; i<highest; i++){
- if(c%i==0){
- ArrayList<Integer> pair = new ArrayList<Integer>();
- pair.add(i);
- pair.add(c/i);
- factors.add(pair);
- highest = c/i;
- }
- }
- return factors;
- }
- public static String bufferadd(){
- String returnbuffer = "";
- for(int i=0; i<buffer+1; i++){
- returnbuffer+=" ";
- }
- return returnbuffer;
- }
- public static GridCell zeron_aabb(GridCell cell){
- int a = cell.a()*cell.a();
- int b = cell.b()*cell.b();
- GridCell returncell = new GridCell(a,b);
- return returncell;
- }
- public static GridCell zeron_1cc(GridCell cell){
- int a = 1;
- int b = cell.c()*cell.c();
- GridCell returncell = new GridCell(a,b);
- return returncell;
- }
- public static GridCell zeron_a_abb(GridCell cell){
- int a = cell.a();
- int b = cell.b()*cell.b()*cell.a();
- GridCell returncell = new GridCell(a,b);
- return returncell;
- }
- public static GridCell zeron_ab_ab(GridCell cell){
- int a = cell.a()*cell.b();
- int b = cell.a()*cell.b();
- GridCell returncell = new GridCell(a,b);
- return returncell;
- }
- public static GridCell zeron_b_aab(GridCell cell){
- int a = cell.b();
- int b = cell.b()*cell.a()*cell.a();
- GridCell returncell = new GridCell(a,b);
- return returncell;
- }
- public static GridCell oneone_d_aabbn(int d){
- int e = 1;
- int n = 1;
- int f = -1*(e-((2*d)+1));
- int rootf = (int)Math.sqrt(f);
- int x = rootf-1;
- int a = d-x;
- int b = a+(2*x)+(2*n);
- GridCell returncell = new GridCell(a,b);
- return returncell;
- }
- public static GridCell zeroone_a_aabbn(int a){
- int e = 0;
- int n = 1;
- int x = (int)Math.sqrt((2*n*a)-e);
- int b = a+(2*x)+(2*n);
- GridCell returncell = new GridCell(a,b);
- return returncell;
- }
- public static String squaresum(GridCell cell){
- int e = cell.e();
- int a = cell.a();
- int t = cell.t();
- String returnstring = "";
- if(e%2==0){
- int eover2 = e/2;
- int asquares = a-eover2;
- int asquare = asquares/2;
- returnstring = "a[t] = sq+sq+e/2 = " + asquare + "+" + asquare + "+" + eover2;
- } else {
- int eminus1over2 = (e-1)/2;
- int afirstsquare = (t-1)*(t-1);
- int asecondsquare = t*t;
- returnstring = "a[t] = sq+(sq)+(e-1)/2 = " + afirstsquare + "+" + asecondsquare + "+" + eminus1over2;
- }
- return returnstring;
- }
- public static GridCell e1cell(int e, int value){
- int n = 1;
- int a = value;
- int x = (int)Math.sqrt((2*a*n)-e);
- int b = a+(2*x)+(2*n);
- GridCell returncell = new GridCell(a,b);
- return returncell;
- }
- public static GridCell f1cell(int f, int value){
- int n = 1;
- int a = value;
- int x = (int)Math.sqrt((2*a*n)-f);
- int b = a+(2*x)+(2*n);
- GridCell returncell = new GridCell(a,b,f);
- return returncell;
- }
- public static ArrayList<GridCell> xplusx(int value, int e, int f){
- if(value%2==0){
- value+=1; //not the best fix but I'll only be using it for odds anyway
- }
- int halflow = value/2;
- int halfhigh = halflow+1; //the two values that add to make value
- int xneg = 0;
- int xpos = 0;
- if(e%2==0){ //e has even x, f has odd x
- if(halflow%2==0){
- xpos = halflow;
- xneg = halfhigh;
- } else {
- xpos = halfhigh;
- xneg = halflow;
- }
- } else { //e has odd x, f has even x
- if(halflow%2==0){
- xpos = halfhigh;
- xneg = halflow;
- } else {
- xpos = halflow;
- xneg = halfhigh;
- }
- }
- int n = 1;
- int aneg = ((xneg*xneg)+f)/2;
- int apos = ((xpos*xpos)+e)/2;
- int bneg = aneg+2+(2*xneg);
- int bpos = apos+2+(2*xpos);
- GridCell fcell = new GridCell(aneg, bneg, f);
- GridCell ecell = new GridCell(apos, bpos);
- ArrayList<GridCell> returncells = new ArrayList<GridCell>();
- returncells.add(fcell);
- returncells.add(ecell);
- return returncells;
- }
- public static String xplusxstring(String type1, int value1, String type2, int value2, int e, int f){
- String thing = "";
- thing += bufferadd();
- if(value1%2!=0){
- thing += "The cells in (e,1) and (f,1) where x+x=" + type1 + "=" + value1 + "\n";
- buffer++;
- thing += bufferadd();
- ArrayList<GridCell> pair = xplusx(value1, e, f);
- thing += pair.get(0).description() + "\n" + bufferadd();
- thing += pair.get(1).description() + "\n";
- buffer--;
- } else if(value2%2!=0){
- thing += "The cells in (e,1) and (f,1) where x+x=" + type2 + "=" + value2 + "\n";
- buffer++;
- thing += bufferadd();
- ArrayList<GridCell> pair = xplusx(value2, e, f);
- thing += pair.get(0).description() + "\n" + bufferadd();
- thing += pair.get(1).description() + "\n";
- buffer--;
- } else {
- 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";
- buffer++;
- thing += bufferadd();
- ArrayList<GridCell> pair = xplusx(value1+1, e, f);
- thing += pair.get(0).description() + "\n" + bufferadd();
- thing += pair.get(1).description() + "\n";
- buffer--;
- }
- return thing;
- }
- }
- /**
- * Things to potentially add:
- * - all (0,n) cells for d*d
- * - the (e,n) cell for which (e+2n,n) is the solution record for each ab pair
- * - the (f,n) cell for which (f+2(n-1),n) is the solution record for each ab pair
- */
Advertisement
Add Comment
Please, Sign In to add comment