Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.math.*;
- public class everything{
- //Buffer for formatting the factor tree String
- private static int buffer = 0;
- //If true, show the related information
- private static boolean factorc = false;
- private static boolean everything = false;
- private static boolean allforc = false;
- private static boolean allforab = false;
- private static boolean showdef = false;
- private static boolean showrootofd = false;
- private static boolean showbign = false;
- private static boolean showatbign = false;
- private static boolean showatcbign = false;
- private static boolean showatcshad = false;
- private static boolean showxx2f1 = false;
- private static boolean showxx2d1 = false;
- private static boolean showxxbign = false;
- private static boolean showxxcbign = false;
- private static boolean showxxcshad = false;
- private static boolean showen = false;
- private static boolean showa1b1 = false;
- private static boolean showxpnxpn = false;
- private static boolean show2xpnxpn = false;
- private static boolean show0naabb = false;
- private static boolean show0nabab = false;
- private static boolean show0nabba = false;
- private static boolean show0nbaab = false;
- private static boolean show0n1cc = false;
- private static boolean show11d = false;
- private static boolean show01a = false;
- private static boolean showe1an = false;
- private static boolean showf1anm1 = false;
- private static boolean showe1bn = false;
- private static boolean showf1bnm1 = false;
- private static boolean showxxan = false;
- private static boolean showxxbn = false;
- //Whether to bit trim (all odd) or not (all differences of two squares)
- private static boolean bittrim = false;
- public static void main(String[] args){
- Scanner scan = new Scanner(System.in);
- String ain = "";
- String bin = "";
- BigInteger a = BigInteger.valueOf(1);
- BigInteger b = BigInteger.valueOf(1);
- System.out.println("Do you want to input c, or a and b? y for c, n for a and b");
- factorc = yncheck();
- if(factorc){
- System.out.println("c=");
- ain = "1";
- bin = scan.nextLine();
- try{
- a = new BigInteger(ain);
- b = new BigInteger(bin);
- 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
- System.out.println("You need to input a c value which is the difference of two squares");
- return;
- }
- 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
- a = a.add(BigInteger.valueOf(1));
- b = b.divide(BigInteger.valueOf(2));
- }
- } catch(Exception e){
- System.out.println("Something was wrong with your c input (it needs to be a number)");
- return;
- }
- } else {
- System.out.println("a=");
- ain = scan.nextLine();
- System.out.println("b=");
- bin = scan.nextLine();
- try{
- a = new BigInteger(ain);
- b = new BigInteger(bin);
- } catch(Exception e){
- System.out.println("Something was wrong with your a and b input (they need to be numbers)");
- return;
- }
- }
- mainbools();
- BigCell cell = new BigCell(a, b);
- System.out.println(cell.description());
- System.out.println();
- System.out.println("=========================================");
- System.out.println();
- System.out.print("c = " + cell.c() + doeverything(cell.c()));
- buffer++;
- recursed(cell.d());
- recursee(cell.e());
- }
- /*This function allows the user to determine whether they want to analyze just c, just the ab pairs, or everything.*/
- public static void mainbools(){
- System.out.println("Before you get output, you'll need to go through a few options.");
- 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");
- bittrim = yncheck();
- System.out.println("Do you want to see everything? y/n");
- everything = yncheck();
- if(everything){
- allforc = true;
- allforab = true;
- showdef = true;
- showrootofd = true;
- showbign = true;
- showatbign = true;
- showatcbign = true;
- showatcshad = true;
- showxx2f1 = true;
- showxx2d1 = true;
- showxxbign = true;
- showxxcbign = true;
- showxxcshad = true;
- showen = true;
- showa1b1 = true;
- showxpnxpn = true;
- show2xpnxpn = true;
- show0naabb = true;
- show0nabab = true;
- show0nabba = true;
- show0nbaab = true;
- show0n1cc = true;
- show11d = true;
- show01a = true;
- showe1an = true;
- showf1anm1 = true;
- showe1bn = true;
- showf1bnm1 = true;
- showxxan = true;
- showxxbn = true;
- return;
- }
- System.out.println("Do you want to see information from c? y/n");
- allforc = yncheck();
- System.out.println("Do you want to see information from each ab pair? y/n");
- allforab = yncheck();
- if(allforc){
- boolsforc();
- if(allforab){
- System.out.println();
- }
- }
- if(allforab){
- boolsforab();
- }
- }
- /*This function allows the user to determine what information about c they want to see, if any.*/
- public static void boolsforc(){
- 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.");
- System.out.println("\t 1 --- d, e and f");
- System.out.println("\t 2 --- Root of d cell");
- System.out.println("\t 3 --- BigN and BigN-1");
- System.out.println("\t 4 --- (e,1) and (f,1) where a[t] = BigN and BigN-1");
- System.out.println("\t 5 --- (e,1) and (f,1) where a[t] = c*BigN and c*(BigN-1)");
- System.out.println("\t 6 --- (e,1) and (f,1) where a[t] = c(c-BigN+1) and c(c-BigN+2)");
- System.out.println("\t 7 --- (e,1) and (f,1) where x+x = 2f+1 (i.e. where x = f or f+1)");
- System.out.println("\t 8 --- (e,1) and (f,1) where x+x = 2d+1");
- System.out.println("\t 9 --- (e,1) and (f,1) where x+x = BigN or BigN-1");
- System.out.println("\t10 --- (e,1) and (f,1) where x+x = c*BigN or c*BigN-1");
- System.out.println("\t11 --- (e,1) and (f,1) where x+x = c(c-BigN+1) or c(c-BigN+2)");
- ArrayList<Integer> picks = picknumbers();
- if(picks.contains(1)){
- showdef = true;
- }
- if(picks.contains(2)){
- showrootofd = true;
- }
- if(picks.contains(3)){
- showbign = true;
- }
- if(picks.contains(4)){
- showatbign = true;
- }
- if(picks.contains(5)){
- showatcbign = true;
- }
- if(picks.contains(6)){
- showatcshad = true;
- }
- if(picks.contains(7)){
- showxx2f1 = true;
- }
- if(picks.contains(8)){
- showxx2d1 = true;
- }
- if(picks.contains(9)){
- showxxbign = true;
- }
- if(picks.contains(10)){
- showxxcbign = true;
- }
- if(picks.contains(11)){
- showxxcshad = true;
- }
- }
- /*This function allows the user to determine what information about a an b they want to see, if any.*/
- public static void boolsforab(){
- 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.");
- System.out.println("\t 1 --- the (e,n) cell");
- System.out.println("\t 2 --- (a-1)(b-1) and BigN-n");
- System.out.println("\t 3 --- the (x+n)(x+n) square and its triangles and nn+2d(n-1)+f-1 configuration");
- System.out.println("\t 4 --- the 2(x+n)(x+n) square and its triangles");
- System.out.println("\t 5 --- (0,n) a=aa b=bb");
- System.out.println("\t 6 --- (0,n) a=ab b=ab");
- System.out.println("\t 7 --- (0,n) a=a b=abb");
- System.out.println("\t 8 --- (0,n) a=b b=aab");
- System.out.println("\t 9 --- (0,n) a=1 b=cc");
- System.out.println("\t10 --- the cell in (1,1) where d is the n value for (0,n) a=aa b=bb");
- System.out.println("\t11 --- the cell in (0,1) where a is the n value for (0,n) a=aa b=bb");
- System.out.println("\t12 --- (e,1) a[t] = an");
- System.out.println("\t13 --- (f,1) a[t] = a(n-1)");
- System.out.println("\t14 --- (e,1) a[t] = bn");
- System.out.println("\t15 --- (f,1) a[t] = b(n-1)");
- System.out.println("\t16 --- (e,1) and (f,1) where x+x = an or a(n-1)");
- System.out.println("\t17 --- (e,1) and (f,1) where x+x = bn or b(n-1)");
- ArrayList<Integer> picks = picknumbers();
- if(picks.contains(1)){
- showen = true;
- }
- if(picks.contains(2)){
- showa1b1 = true;
- }
- if(picks.contains(3)){
- showxpnxpn = true;
- }
- if(picks.contains(4)){
- show2xpnxpn = true;
- }
- if(picks.contains(5)){
- show0naabb = true;
- }
- if(picks.contains(6)){
- show0nabab = true;
- }
- if(picks.contains(7)){
- show0nabba = true;
- }
- if(picks.contains(8)){
- show0nbaab = true;
- }
- if(picks.contains(9)){
- show0n1cc = true;
- }
- if(picks.contains(10)){
- show11d = true;
- }
- if(picks.contains(11)){
- show01a = true;
- }
- if(picks.contains(12)){
- showe1an = true;
- }
- if(picks.contains(13)){
- showf1anm1 = true;
- }
- if(picks.contains(14)){
- showe1bn = true;
- }
- if(picks.contains(15)){
- showf1bnm1 = true;
- }
- if(picks.contains(16)){
- showxxan = true;
- }
- if(picks.contains(17)){
- showxxbn = true;
- }
- }
- /*When the user is prompted to input a number, this function will make sure input is valid.*/
- public static ArrayList<Integer> picknumbers(){
- ArrayList<Integer> picks = new ArrayList<Integer>();
- Scanner scan = new Scanner(System.in);
- String ints = scan.nextLine();
- String[] parts = ints.split(" ");
- for(int i=0; i<parts.length; i++){
- try {
- int test = Integer.parseInt(parts[i]);
- picks.add(test);
- } catch(Exception e) {
- }
- }
- return picks;
- }
- /*When the user is prompted to input y/n, this function will make sure input is valid.*/
- public static boolean yncheck(){
- Scanner scan = new Scanner(System.in);
- for(;;){
- String line = scan.nextLine();
- if(line.equals("y") || line.equals("Y")){
- return true;
- } else if(line.equals("n") || line.equals("N")){
- return false;
- }
- System.out.println("Please input y or n");
- }
- }
- /*This function creates the d branches down the factor tree.*/
- public static void recursed(BigInteger d){
- String dstring = "";
- if(bittrim){
- if((d.compareTo(BigInteger.valueOf(0)) == 1) && ((d.mod(BigInteger.valueOf(2))).equals(BigInteger.valueOf(0)))){
- while((d.mod(BigInteger.valueOf(2))).equals(BigInteger.valueOf(0))){
- dstring += d + " / ";
- d = d.divide(BigInteger.valueOf(2));
- }
- }
- } else {
- if((d.compareTo(BigInteger.valueOf(0)) == 1) && ((d.mod(BigInteger.valueOf(2))).equals(BigInteger.valueOf(0)))){
- if(!((d.mod(BigInteger.valueOf(4))).equals(BigInteger.valueOf(0)))){
- dstring += d + " / ";
- d = d.divide(BigInteger.valueOf(2));
- }
- }
- }
- dstring += d;
- for(int i=0; i<buffer; i++){
- System.out.print(" ");
- }
- System.out.print("d = " + dstring);
- if((d.compareTo(BigInteger.valueOf(3))) == 1){
- System.out.println(" ---" + doeverything(d));
- BigInteger d2 = sqrt(d);
- BigInteger e = d.subtract(d2.multiply(d2));
- buffer++;
- recursed(d2);
- recursee(e);
- buffer--;
- } else {
- System.out.println();
- }
- }
- /*This function creates the e branches of the factor tree.*/
- public static void recursee(BigInteger e){
- String estring = "";
- if(bittrim){
- if((e.compareTo(BigInteger.valueOf(0)) == 1) && ((e.mod(BigInteger.valueOf(2))).equals(BigInteger.valueOf(0)))){
- while((e.mod(BigInteger.valueOf(2))).equals(BigInteger.valueOf(0))){
- estring += e + " / ";
- e = e.divide(BigInteger.valueOf(2));
- }
- }
- } else {
- if((e.compareTo(BigInteger.valueOf(0)) == 1) && ((e.mod(BigInteger.valueOf(2))).equals(BigInteger.valueOf(0)))){
- if(!((e.mod(BigInteger.valueOf(4))).equals(BigInteger.valueOf(0)))){
- estring += e + " / ";
- e = e.divide(BigInteger.valueOf(2));
- }
- }
- }
- estring += e;
- for(int i=0; i<buffer; i++){
- System.out.print(" ");
- }
- System.out.print("e = " + estring);
- if((e.compareTo(BigInteger.valueOf(3))) == 1){
- System.out.println(" ---" + doeverything(e));
- BigInteger d = sqrt(e);
- BigInteger e2 = e.subtract(d.multiply(d));
- buffer++;
- recursed(d);
- recursee(e2);
- buffer--;
- } else {
- System.out.println();
- System.out.println();
- }
- }
- /*This function creates the output String by calling the other relevant functions.*/
- public static String doeverything(BigInteger c){
- ArrayList<ArrayList<BigInteger>> factors = factorize(c);
- String thing = "\n";
- if(allforc){
- thing += forc(c);
- }
- if(allforab){
- thing += forpairs(factors);
- }
- return thing;
- }
- /*This function generates a String containing all relevant directly calculable information for each number down the factor tree.*/
- public static String forc(BigInteger c){
- String thing = "";
- BigInteger d = sqrt(c);
- BigInteger e = c.subtract(d.multiply(d));
- BigInteger f = e.subtract((d.multiply(BigInteger.valueOf(2))).add(BigInteger.valueOf(1)));
- BigInteger bign = ((c.add(BigInteger.valueOf(1))).divide(BigInteger.valueOf(2))).subtract(d);
- BigInteger cbign = c.multiply(bign);
- BigInteger cbignm1 = c.multiply(bign.subtract(BigInteger.valueOf(1)));
- BigInteger cshadown = c.multiply(c.subtract(bign).add(BigInteger.valueOf(1)));
- BigInteger cshadownp1 = c.multiply(c.subtract(bign).add(BigInteger.valueOf(2)));
- //directly calculables
- if(showdef){
- thing += bufferadd();
- thing += "d = " + d + ", e = " + e + ", f = " + f + "\n";
- }
- //Root of d
- if(showrootofd){
- thing += bufferadd();
- BigInteger rootofd_a = d;
- BigInteger rootofd_b = d.multiply(BigInteger.valueOf(9));
- BigCell rootofd = new BigCell(rootofd_a, rootofd_b);
- thing += "Root of d: " + rootofd.description();
- thing += "\n";
- }
- //BigN and BigN-1
- if(showbign){
- thing += bufferadd();
- thing += "BigN = " + bign + ", BigN-1 = " + (bign.subtract(BigInteger.valueOf(1)));
- thing += "\n";
- }
- //The cells in (e,1) and (f,1) where a[t]=BigN and a[t]=BigN-1
- if(showatbign){
- thing += bufferadd();
- BigCell e1_bign = e1cell(e, bign);
- thing += "(e,1) a=BigN = ";
- thing += e1_bign.description() + " --- " + squaresum(e1_bign);
- thing += "\n";
- thing += bufferadd();
- BigCell f1_bign = f1cell(f, (bign.subtract(BigInteger.valueOf(1))));
- thing += "(f,1) a=BigN-1 = ";
- 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)
- if(showatcbign){
- thing += bufferadd();
- BigCell e1_cbign = e1cell(e, cbign);
- thing += "(e,1) a=c*BigN = ";
- thing += e1_cbign.description() + " --- " + squaresum(e1_cbign);
- thing += "\n";
- thing += bufferadd();
- BigCell f1_cbignm1 = f1cell(f, cbignm1);
- thing += "(f,1) a=c*(BigN-1) = ";
- 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)
- if(showatcshad){
- thing += bufferadd();
- BigCell e1_cshadown = e1cell(e, cshadown);
- thing += "(e,1) a=c*(c-BigN+1) = ";
- thing += e1_cshadown.description() + " --- " + squaresum(e1_cshadown);
- thing += "\n";
- thing += bufferadd();
- BigCell f1_cshadownp1 = f1cell(f, cshadownp1);
- thing += "(f,1) a=c*(c-BigN+2) = ";
- thing += f1_cshadownp1.description() + " --- " + squaresum(f1_cshadownp1);
- thing += "\n";
- }
- //The cells in (e,1) and (f,1) where x+x=2f+1
- if(showxx2f1){
- BigInteger twofp1 = (f.multiply(BigInteger.valueOf(-2))).add(BigInteger.valueOf(1));
- thing += xplusxstring("2f+1 (x=f or f+1)", twofp1, "filler", BigInteger.valueOf(2), e, f);
- }
- //The cells in (e,1) and (f,1) where x+x=2d+1
- if(showxx2d1){
- BigInteger twodp1 = (d.multiply(BigInteger.valueOf(2))).add(BigInteger.valueOf(1));
- thing += xplusxstring("2d+1", twodp1, "filler", BigInteger.valueOf(2), e, f);
- }
- //The cells in (e,1) and (f,1) where x+x=BigN or BigN-1
- if(showxxbign){
- thing += xplusxstring("BigN", bign, "BigN-1", (bign.subtract(BigInteger.valueOf(1))), e, f);
- }
- //The cells in (e,1) and (f,1) where x+x=c*BigN or c*BigN-1
- if(showxxcbign){
- 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)
- if(showxxcshad){
- thing += xplusxstring("c*(c-BigN+1)", cshadown, "c*(c-BigN+2)", cshadownp1, e, f);
- }
- return thing;
- }
- /*This function generates a String containing all relevant information for each factor pair.*/
- public static String forpairs(ArrayList<ArrayList<BigInteger>> factors){
- String thing = "";
- for(int i=0; i<factors.size(); i++){
- if(((factors.get(i).get(0).add(factors.get(i).get(1))).mod(BigInteger.valueOf(2))).equals(BigInteger.valueOf(0))){
- BigInteger a = factors.get(i).get(0);
- BigInteger b = factors.get(i).get(1);
- BigCell paircell = new BigCell(a, b);
- BigCell zeron_aabb_cell = zeron_aabb(paircell);
- BigInteger n_aabb = zeron_aabb_cell.n();
- BigCell e1_na = e1cell(paircell.e(), (paircell.n().multiply(paircell.a())));
- BigCell f1_na = f1cell(paircell.f(), (paircell.a().multiply(paircell.n().subtract(BigInteger.valueOf(1)))));
- BigCell e1_bn = e1cell(paircell.e(), (paircell.b().multiply(paircell.n())));
- BigCell f1_bn = f1cell(paircell.f(), (paircell.b().multiply(paircell.n().subtract(BigInteger.valueOf(1)))));
- thing += bufferadd();
- thing += factors.get(i);
- thing += "\n";
- buffer++;
- //The (e,n) cell
- if(showen){
- thing += bufferadd();
- thing += "(e,n) cell = " + paircell.description();
- thing += "\n";
- }
- //(a-1)(b-1) and /2
- if(showa1b1){
- BigInteger a1b1 = (a.subtract(BigInteger.valueOf(1))).multiply(b.subtract(BigInteger.valueOf(1)));
- BigInteger a1b1over2 = a1b1.divide(BigInteger.valueOf(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
- if(showxpnxpn){
- BigInteger xplusnsquared = paircell.xplusnsquared();
- BigInteger tri1 = BigInteger.valueOf(0);
- BigInteger tri2 = BigInteger.valueOf(0);
- String triout = "error in trioutput";
- if((xplusnsquared.mod(BigInteger.valueOf(2))).equals(BigInteger.valueOf(0))){
- BigInteger overfour = xplusnsquared.divide(BigInteger.valueOf(4));
- BigInteger largebase = sqrt(overfour);
- BigInteger smallbase = largebase.subtract(BigInteger.valueOf(1));
- tri1 = (largebase.multiply(largebase.subtract(BigInteger.valueOf(1)))).divide(BigInteger.valueOf(2));
- tri2 = (smallbase.multiply(smallbase.subtract(BigInteger.valueOf(1)))).divide(BigInteger.valueOf(2));
- triout = "(x+n)(x+n) is " + xplusnsquared + ", which is (4*" + tri1 + ")+(4*" + tri2 + ") (u=" + largebase + "&" + smallbase + ")";
- } else {
- BigInteger minusone = xplusnsquared.subtract(BigInteger.valueOf(1));
- tri1 = minusone.divide(BigInteger.valueOf(8));
- triout = "(x+n)(x+n) is " + xplusnsquared + ", which is (8*" + tri1 + ")+1 (u=" + paircell.u() + ")";
- }
- triout+="\n";
- triout+=bufferadd();
- BigInteger nn = paircell.n().multiply(paircell.n());
- BigInteger twodnm1 = (paircell.d().multiply(BigInteger.valueOf(2))).multiply(paircell.n().subtract(BigInteger.valueOf(1)));
- BigInteger posf = paircell.f().multiply(BigInteger.valueOf(-1));
- triout+="nn+2d(n-1)+f-1 = " + nn + "+" + twodnm1 + "+" + posf + "-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
- if(show2xpnxpn){
- BigInteger xplusnsquared2 = (paircell.xplusn().multiply(BigInteger.valueOf(2))).multiply(paircell.xplusn().multiply(BigInteger.valueOf(2)));
- String triout2 = "error in trioutput2";
- BigInteger tri12 = BigInteger.valueOf(0);
- BigInteger tri22 = BigInteger.valueOf(0);
- BigInteger overfour2 = xplusnsquared2.divide(BigInteger.valueOf(4)); //since it's *2 it'll always be evens
- BigInteger largebase2 = sqrt(overfour2);
- BigInteger smallbase2 = largebase2.subtract(BigInteger.valueOf(1));
- tri12 = (largebase2.multiply(largebase2.subtract(BigInteger.valueOf(1)))).divide(BigInteger.valueOf(2));
- tri22 = (smallbase2.multiply(smallbase2.subtract(BigInteger.valueOf(1)))).divide(BigInteger.valueOf(2));
- triout2 = "2(x+n)2(x+n) is " + xplusnsquared2 + ", which is (4*" + tri12 + ")+(4*" + tri22 + ") (u=" + largebase2 + "&" + smallbase2 + ")";
- thing += bufferadd();
- thing += triout2;
- thing += "\n";
- }
- //(0,n) a=aa b=bb
- if(show0naabb){
- thing += bufferadd();
- thing += "(0,n) aa,bb = ";
- thing += zeron_aabb_cell.description();
- thing += "\n";
- }
- //(0,n) a=ab b=ab
- if(show0nabab){
- thing += bufferadd();
- BigCell zeron_ab_ab_cell = zeron_ab_ab(paircell);
- thing += "(0,n) ab,ab = ";
- thing += zeron_ab_ab_cell.description();
- thing += "\n";
- }
- //(0,n) a=a b=abb
- if(show0nabba){
- thing += bufferadd();
- BigCell zeron_a_abb_cell = zeron_a_abb(paircell);
- thing += "(0,n) a,abb = ";
- thing += zeron_a_abb_cell.description();
- thing += "\n";
- }
- //(0,n) a=b b=aab
- if(show0nbaab){
- thing += bufferadd();
- BigCell zeron_b_aab_cell = zeron_b_aab(paircell);
- thing += "(0,n) b,aab = ";
- thing += zeron_b_aab_cell.description();
- thing += "\n";
- }
- //(0,n) a=1 b=cc
- if(show0n1cc){
- thing += bufferadd();
- BigCell zeron_1cc_cell = zeron_1cc(paircell);
- thing += "(0,n) 1,cc = ";
- 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
- if(show11d){
- thing += bufferadd();
- BigCell oneone_d_aabbn_cell = oneone_d_aabbn(n_aabb);
- thing += "(1,1) d=aabbn = ";
- 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
- if(show01a){
- thing += bufferadd();
- BigCell zeroone_a_aabbn_cell = zeroone_a_aabbn(n_aabb);
- thing += "(0,1) a=aabbn = ";
- thing += zeroone_a_aabbn_cell.description();
- thing += "\n";
- }
- //(e,1) na
- if(showe1an){
- thing += bufferadd();
- thing += "(e,1) a=na = ";
- thing += e1_na.description() + " --- " + squaresum(e1_na);
- thing += "\n";
- }
- //(f,1) a(n-1)
- if(showf1anm1){
- thing += bufferadd();
- thing += "(f,1) a=a(n-1) = ";
- thing += f1_na.description() + " --- " + squaresum(f1_na);
- thing += "\n";
- }
- //(e,1) bn
- if(showe1bn){
- thing += bufferadd();
- thing += "(e,1) a=bn = ";
- thing += e1_bn.description() + " --- " + squaresum(e1_bn);
- thing += "\n";
- }
- //(f,1) b(n-1)
- if(showf1bnm1){
- thing += bufferadd();
- thing += "(f,1) a=b(n-1) = ";
- 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) (whichever's odd, if any)
- if(showxxan){
- thing += xplusxstring("an", e1_na.a(), "a(n-1)", f1_na.a(), paircell.e(), paircell.f());
- }
- //the cells in (e,1) and (f,1) for which x+x=bn or b(n-1) (whichever's odd, if any)
- if(showxxbn){
- thing += xplusxstring("bn", e1_bn.a(), "b(n-1)", f1_bn.a(), paircell.e(), paircell.f());
- }
- buffer--;
- }
- }
- return thing;
- }
- /*This function creates an ArrayList containing each pair of factors that multiply together to give your given c.*/
- public static ArrayList<ArrayList<BigInteger>> factorize(BigInteger c){
- ArrayList<ArrayList<BigInteger>> factorpairs = new ArrayList<ArrayList<BigInteger>>();
- ArrayList<BigInteger> primefactors = primefactors(c);
- Collections.sort(primefactors);
- ArrayList<Integer> factorcount = new ArrayList<Integer>();
- int count=0;
- for(int i=0; i<primefactors.size(); i++){
- if(i>0){
- if(primefactors.get(i-1).equals(primefactors.get(i))){
- count++;
- } else {
- factorcount.add(count+1);
- count = 0;
- }
- }
- }
- factorcount.add(count+1);
- Set<BigInteger> set = new HashSet<>(primefactors);
- primefactors.clear();
- primefactors.addAll(set);
- Collections.sort(primefactors);
- ArrayList<BigInteger> allfactors = new ArrayList<BigInteger>();
- boolean divisible = true;
- for(int l=0; l<primefactors.size(); l++){
- allfactors.add(primefactors.get(l));
- }
- int lower = 0;
- int higher = primefactors.size(); //when it starts it'll try 0 to the end of primefactors in allfactors
- while(divisible){
- divisible = false;
- for(int j=0; j<primefactors.size(); j++){
- //if allfactors[k] is divisible by primefactors[0] factorcount[0] times
- BigInteger exp = primefactors.get(j).pow(factorcount.get(j));
- for(int k=lower; k<higher; k++){
- if((!(allfactors.get(k).mod(exp).equals(BigInteger.valueOf(0)))) && !(allfactors.contains(allfactors.get(k).multiply(primefactors.get(j))))){ //if allfactors%primefactor^exp == 0
- allfactors.add(allfactors.get(k).multiply(primefactors.get(j)));
- divisible = true;
- }
- }
- }
- lower = higher;
- higher = allfactors.size();
- }
- allfactors.add(BigInteger.valueOf(1));
- Collections.sort(allfactors);
- lower = 0;
- higher = allfactors.size()-1;
- while(lower<=higher){
- 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)
- ArrayList<BigInteger> temppair = new ArrayList<BigInteger>();
- temppair.add(allfactors.get(lower));
- temppair.add(allfactors.get(higher));
- factorpairs.add(temppair);
- }
- lower++;
- higher--;
- }
- if(factorpairs.get(0).get(0).equals(BigInteger.valueOf(1)) && factorpairs.get(0).get(1).equals(BigInteger.valueOf(1))){ //primes
- factorpairs = new ArrayList<ArrayList<BigInteger>>();
- ArrayList<BigInteger> primepair = new ArrayList<BigInteger>();
- primepair.add(BigInteger.valueOf(1));
- primepair.add(c);
- factorpairs.add(primepair);
- }
- return factorpairs;
- }
- /*This function finds all prime factors of a given number.*/
- public static ArrayList<BigInteger> primefactors(BigInteger cfixed){
- BigInteger c = cfixed;
- ArrayList<BigInteger> factors = new ArrayList<BigInteger>();
- if(c.equals(BigInteger.valueOf(1))){
- factors.add(BigInteger.valueOf(1));
- return factors;
- }
- BigInteger lower = BigInteger.valueOf(2);
- BigInteger higher = sqrt(c).add(BigInteger.valueOf(1));
- while(lower.compareTo(higher) == -1){
- while((c.mod(lower).equals(BigInteger.valueOf(0))) && !(c.equals(lower))){
- factors.add(lower);
- c = c.divide(lower);
- factors.add(c);
- higher = sqrt(c).add(BigInteger.valueOf(1));
- }
- if(lower.equals(BigInteger.valueOf(2))){
- lower = lower.add(BigInteger.valueOf(1));
- } else {
- lower = lower.add(BigInteger.valueOf(2));
- }
- }
- for(int i=0; i<factors.size(); i++){
- for(int j=0; j<factors.size(); j++){
- if( ((factors.get(i)).mod(factors.get(j)).equals(BigInteger.valueOf(0))) && !(factors.get(i).equals(factors.get(j)))){
- factors.remove(i);
- j=factors.size();
- }
- }
- }
- return factors;
- }
- /*This function adds a buffer to the String (increasing in width down each branch of the factor tree) for presentation purposes.*/
- public static String bufferadd(){
- String returnbuffer = "";
- for(int i=0; i<buffer+1; i++){
- returnbuffer+=" ";
- }
- return returnbuffer;
- }
- /*This function generates the cell in (0,n) where a=aa and b=bb.*/
- public static BigCell zeron_aabb(BigCell cell){
- BigInteger a = cell.a().multiply(cell.a());
- BigInteger b = cell.b().multiply(cell.b());
- BigCell returncell = new BigCell(a,b);
- return returncell;
- }
- /*This function generates the cell in (0,n) where a=1 and b=cc.*/
- public static BigCell zeron_1cc(BigCell cell){
- BigInteger a = BigInteger.valueOf(1);
- BigInteger b = cell.c().multiply(cell.c());
- BigCell returncell = new BigCell(a,b);
- return returncell;
- }
- /*This function generates the cell in (0,n) where a=a and b=abb.*/
- public static BigCell zeron_a_abb(BigCell cell){
- BigInteger a = cell.a();
- BigInteger b = cell.b().multiply(cell.b().multiply(cell.a()));
- BigCell returncell = new BigCell(a,b);
- return returncell;
- }
- /*This function generates the cell in (0,n) where a=c and b=c.*/
- public static BigCell zeron_ab_ab(BigCell cell){
- BigInteger a = cell.a().multiply(cell.b());
- BigInteger b = cell.a().multiply(cell.b());
- BigCell returncell = new BigCell(a,b);
- return returncell;
- }
- /*This function generates the cell in (0,n) where a=b and b=aab.*/
- public static BigCell zeron_b_aab(BigCell cell){
- BigInteger a = cell.b();
- BigInteger b = cell.b().multiply(cell.a().multiply(cell.a()));
- BigCell returncell = new BigCell(a,b);
- return returncell;
- }
- /*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.*/
- public static BigCell oneone_d_aabbn(BigInteger d){
- BigInteger e = BigInteger.valueOf(1);
- BigInteger n = BigInteger.valueOf(1);
- BigInteger f = (e.subtract((d.multiply(BigInteger.valueOf(2))).add(BigInteger.valueOf(1)))).multiply(BigInteger.valueOf(-1));
- BigInteger rootf = sqrt(f);
- BigInteger x = rootf.subtract(BigInteger.valueOf(1));
- BigInteger a = d.subtract(x);
- BigInteger b = a.add((x.multiply(BigInteger.valueOf(2))).add(n.multiply(BigInteger.valueOf(2))));
- BigCell returncell = new BigCell(a,b);
- return returncell;
- }
- /*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.*/
- public static BigCell zeroone_a_aabbn(BigInteger a){
- BigInteger e = BigInteger.valueOf(0);
- BigInteger n = BigInteger.valueOf(1);
- BigInteger twoname = (n.multiply(a.multiply(BigInteger.valueOf(2)))).subtract(e);
- BigInteger x = sqrt(twoname);
- BigInteger b = a.add((x.multiply(BigInteger.valueOf(2))).add(n.multiply(BigInteger.valueOf(2))));
- BigCell returncell = new BigCell(a,b);
- return returncell;
- }
- /*This function calculates the two consecutive squares or the doubled square that make up a[t] in (e,1) and (f,1).*/
- public static String squaresum(BigCell cell){
- BigInteger e = cell.e();
- BigInteger a = cell.a();
- BigInteger t = cell.t();
- String returnstring = "";
- if((e.mod(BigInteger.valueOf(2))).equals(BigInteger.valueOf(0))){
- BigInteger eover2 = e.divide(BigInteger.valueOf(2));
- BigInteger asquares = a.subtract(eover2);
- BigInteger asquare = asquares.divide(BigInteger.valueOf(2));
- returnstring = "a[t] = sq+sq+e/2 = " + asquare + "+" + asquare + "+" + eover2;
- } else {
- BigInteger eminus1over2 = (e.subtract(BigInteger.valueOf(1))).divide(BigInteger.valueOf(2));
- BigInteger afirstsquare = (t.subtract(BigInteger.valueOf(1))).multiply(t.subtract(BigInteger.valueOf(1)));
- BigInteger asecondsquare = t.multiply(t);
- returnstring = "a[t] = sq+(sq)+(e-1)/2 = " + afirstsquare + "+" + asecondsquare + "+" + eminus1over2;
- }
- return returnstring;
- }
- /*This function calculates the cell in (e,1) where a[t] equals a given value (which is assumed to be valid). */
- public static BigCell e1cell(BigInteger e, BigInteger value){
- BigInteger n = BigInteger.valueOf(1);
- BigInteger a = value;
- BigInteger twoname = (n.multiply(a.multiply(BigInteger.valueOf(2)))).subtract(e);
- BigInteger x = sqrt(twoname);
- BigInteger b = a.add(x.multiply(BigInteger.valueOf(2))).add(n.multiply(BigInteger.valueOf(2)));
- BigCell returncell = new BigCell(a,b);
- return returncell;
- }
- /*This function calculates the cell in (f,1) where a[t] equals a given value (which is assumed to be valid). */
- public static BigCell f1cell(BigInteger f, BigInteger value){
- BigInteger n = BigInteger.valueOf(1);
- BigInteger a = value;
- BigInteger twonamf = (n.multiply(a.multiply(BigInteger.valueOf(2)))).subtract(f);
- BigInteger x = sqrt(twonamf);
- BigInteger b = a.add(x.multiply(BigInteger.valueOf(2))).add(n.multiply(BigInteger.valueOf(2)));
- BigCell returncell = new BigCell(a,b,f);
- return returncell;
- }
- /*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).*/
- public static ArrayList<BigCell> xplusx(BigInteger value, BigInteger e, BigInteger f){
- if((value.mod(BigInteger.valueOf(2))).equals(BigInteger.valueOf(0))){
- value = value.add(BigInteger.valueOf(1)); //not the best fix but I'll only be using it for odds anyway
- }
- BigInteger halflow = value.divide(BigInteger.valueOf(2));
- BigInteger halfhigh = halflow.add(BigInteger.valueOf(1)); //the two values that add to make value
- BigInteger xneg = BigInteger.valueOf(0);
- BigInteger xpos = BigInteger.valueOf(0);
- if((e.mod(BigInteger.valueOf(2))).equals(BigInteger.valueOf(0))){ //e has even x, f has odd x
- if((halflow.mod(BigInteger.valueOf(2))).equals(BigInteger.valueOf(0))){
- xpos = halflow;
- xneg = halfhigh;
- } else {
- xpos = halfhigh;
- xneg = halflow;
- }
- } else { //e has odd x, f has even x
- if((halflow.mod(BigInteger.valueOf(2))).equals(BigInteger.valueOf(0))){
- xpos = halfhigh;
- xneg = halflow;
- } else {
- xpos = halflow;
- xneg = halfhigh;
- }
- }
- BigInteger n = BigInteger.valueOf(1);
- BigInteger aneg = ((xneg.multiply(xneg)).add(f)).divide(BigInteger.valueOf(2));
- BigInteger apos = ((xpos.multiply(xpos)).add(e)).divide(BigInteger.valueOf(2));
- BigInteger bneg = aneg.add(BigInteger.valueOf(2)).add(xneg.multiply(BigInteger.valueOf(2)));
- BigInteger bpos = apos.add(BigInteger.valueOf(2)).add(xpos.multiply(BigInteger.valueOf(2)));
- BigCell fcell = new BigCell(aneg, bneg, f);
- BigCell ecell = new BigCell(apos, bpos);
- ArrayList<BigCell> returncells = new ArrayList<BigCell>();
- returncells.add(fcell);
- returncells.add(ecell);
- return returncells;
- }
- /*This function turns the staircase x cells from row 1 (in the function directly above) into a String.*/
- public static String xplusxstring(String type1, BigInteger value1, String type2, BigInteger value2, BigInteger e, BigInteger f){
- String thing = "";
- thing += bufferadd();
- if(value1.mod(BigInteger.valueOf(2)).equals(BigInteger.valueOf(1))){
- thing += "The cells in (e,1) and (f,1) where x+x=" + type1 + "=" + value1 + "\n";
- buffer++;
- thing += bufferadd();
- ArrayList<BigCell> pair = xplusx(value1, e, f);
- thing += pair.get(0).description() + "\n" + bufferadd();
- thing += pair.get(1).description() + "\n";
- buffer--;
- } else if(value2.mod(BigInteger.valueOf(2)).equals(BigInteger.valueOf(1))){
- thing += "The cells in (e,1) and (f,1) where x+x=" + type2 + "=" + value2 + "\n";
- buffer++;
- thing += bufferadd();
- ArrayList<BigCell> 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.add(BigInteger.valueOf(1))) + " (using x from (e,1) and (f,1)):\n";
- buffer++;
- thing += bufferadd();
- ArrayList<BigCell> pair = xplusx((value1.add(BigInteger.valueOf(1))), e, f);
- thing += pair.get(0).description() + "\n" + bufferadd();
- thing += pair.get(1).description() + "\n";
- buffer--;
- }
- return thing;
- }
- /*BigInteger sqrt method. Not the one Chris gave us - that didn't work properly for some reason.*/
- public static BigInteger sqrt(BigInteger x) {
- if(x.equals(BigInteger.ZERO)){
- return BigInteger.ZERO;
- }
- BigInteger div = BigInteger.ZERO.setBit(x.bitLength()/2);
- BigInteger div2 = div;
- BigInteger returnvalue = BigInteger.valueOf(0);
- // Loop until we hit the same value twice in a row, or wind
- // up alternating.
- for(;;) {
- BigInteger y = div.add(x.divide(div)).shiftRight(1);
- if (y.equals(div) || y.equals(div2)){
- returnvalue = y;
- break;
- }
- div2 = div;
- div = y;
- }
- BigInteger testsquare = returnvalue.multiply(returnvalue);
- BigInteger testp1square = (returnvalue.add(BigInteger.valueOf(1))).multiply(returnvalue.add(BigInteger.valueOf(1)));
- if((testsquare.equals(x)) || (((testsquare.compareTo(x)) == -1) && ((testp1square.compareTo(x)) == 1))){
- return returnvalue;
- } else {
- return returnvalue.subtract(BigInteger.valueOf(1));
- }
- }
- }
- /**
- * 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