fork download
  1. /* package whatever; // don't place package name!
  2. cd "C:\Users\kmsco\Documents\java coding"
  3. "C:\Program Files\Java\jdk-18\bin\javac.exe" SqrtByHand.java
  4. "C:\Program Files\Java\jdk-18\bin\java.exe" SqrtByHand
  5.  
  6. https://i...content-available-to-author-only...e.com/wid9mq*/
  7.  
  8. import java.util.*; // NumberFormat
  9. import java.lang.*;
  10. import java.io.*;
  11. import java.lang.Math;
  12. import java.text.*; // NumberFormat
  13.  
  14. /* Name of the class has to be "Main" only if the class is public. */
  15. class SqrtByHand
  16. {
  17. //https://w...content-available-to-author-only...d.com/article/2245549/java-101-class-and-object-initialization-in-java.html
  18. static double num = 0;
  19. static double pos = 0;
  20.  
  21. public SqrtByHand(double test)
  22. {
  23. this.num = test;
  24. }
  25.  
  26. public static int digitCounter() // don't call this from main
  27. {
  28. int count = 0; //, num = 0003452;
  29. int wholeNum = (int) num;
  30.  
  31. while (wholeNum != 0)
  32. {
  33. // wholeNum = wholeNum/10
  34. wholeNum /= 10;
  35. ++count;
  36. }
  37. //System.out.println("Number of digits: " + count);
  38. return count;
  39. }
  40.  
  41. public static double firstStep()
  42. {
  43. double numDigits = (double) digitCounter();
  44. double findFirst = 0;
  45. double firstDigit = 0;
  46.  
  47. if(numDigits % 2 == 1) // odd
  48. {
  49. // 71174
  50. // 10^(numDigits-1) = 10^4 = 10,000
  51. // 71174 % 10,000 = 1174 // 71174-1174 = 70,000 / 10^4 = 7
  52. // or 71174 / 10^4 = 7.1174 truncated = Math.round(nontruncated - 0.5f); // https://w...content-available-to-author-only...a.ca/en/computing-science/media-library/teaching-resources/java/truncation-rounding.html
  53. // if answer < 100 ans*100+next2
  54.  
  55. pos = numDigits - 1;
  56. findFirst = num / Math.pow(10,pos); // 10 ^ num of digits-1; 71174 / 10^4 = 7.1174
  57. firstDigit = Math.round(findFirst - 0.5f); // 7 // https://w...content-available-to-author-only...a.ca/en/computing-science/media-library/teaching-resources/java/truncation-rounding.html
  58. }
  59.  
  60. else //if(numDigits % 2 == 0) // even
  61. {
  62. // 711740
  63. // 10^(numDigits-2) = 10^4 = 10,000
  64. // 711740 % 10,000 = 1740 // 711740-1740 = 710,000 / 10^4 = 71
  65. // or 711740 / 10^4 = 71.1740 truncated = Math.round(nontruncated - 0.5f); // https://w...content-available-to-author-only...a.ca/en/computing-science/media-library/teaching-resources/java/truncation-rounding.html
  66. // if answer < 100 ans*100+next2
  67.  
  68. pos = numDigits - 2;
  69. findFirst = num / Math.pow(10,pos); // 711740 / 10^4 = 71.174
  70. firstDigit = Math.round(findFirst - 0.5f); // 71 // https://w...content-available-to-author-only...a.ca/en/computing-science/media-library/teaching-resources/java/truncation-rounding.html
  71. }
  72.  
  73. return firstDigit;
  74. }
  75.  
  76. public static int closestSquareable() // only runs first time
  77. {
  78. double firstDigit = firstStep();
  79.  
  80. int justUnder = 0, count = 0;
  81. for (count = 0; justUnder < firstDigit; count ++) // count*count < firstDigit
  82. {
  83. justUnder = count * count;
  84. /*System.out.println("justUnder = count * count: ");
  85. System.out.print(justUnder);
  86. System.out.print(" = ");
  87. System.out.print(count);
  88. System.out.print(" * ");
  89. System.out.println(count);*/
  90.  
  91. if(justUnder > firstDigit)
  92. break;
  93. }
  94. return count-1;
  95. }
  96.  
  97. public static double used()
  98. {
  99. return 0;
  100. }
  101.  
  102. public static double difference() //(nextPair, answerDoubled) // numbers on the side
  103. {
  104. return 0; // (nextPair - answerDoubled);
  105. }
  106.  
  107. public static double next2nums()
  108. {
  109. double findFirst = num / Math.pow(10,pos); // 71174 / 10^2 = 711.74
  110. System.out.println("findFirst = " + findFirst);
  111. double selectDigits = Math.round(findFirst - 0.5f); // 711 // https://w...content-available-to-author-only...a.ca/en/computing-science/media-library/teaching-resources/java/truncation-rounding.html
  112. System.out.println("selectDigits = " + selectDigits);
  113. double next2Digits = num - (selectDigits*Math.pow(10,pos)); // 71174-(711*10^2) = 71100 = 74
  114. System.out.println("next2Digits = " + next2Digits);
  115. // double used =
  116.  
  117. double next = (difference() * 100) + next2Digits;
  118. //for(int alpha=0; alpha<numDigits; alpha++)
  119. {
  120.  
  121. }
  122.  
  123. pos -= 2;
  124.  
  125. return next;
  126. }
  127.  
  128. public static void print()
  129. {
  130. double closestSquare = closestSquareable()*closestSquareable();
  131. NumberFormat formatter = new DecimalFormat("0"); // # = Digit, zero shows as absent; 0 = Digit // https://d...content-available-to-author-only...e.com/javase/8/docs/api/java/text/DecimalFormat.html#Special%20Pattern%20Characters
  132.  
  133. System.out.println();
  134. // 266.76
  135. System.out.println("√" + num); // *71174*
  136. System.out.println("-" + (formatter.format(closestSquare)) + "\t-> (" +closestSquareable()+ "*" +closestSquareable()+ ")"); // 4 -> (2*2)
  137. System.out.println(" ------"); // equals
  138. System.out.print(" " +(formatter.format(firstStep()-closestSquare))); System.out.println(" \u2193"); // difference // U+2193 = ↓
  139. System.out.print(" " +(formatter.format(firstStep()-closestSquare))); System.out.print(next2nums()); System.out.print("\t-> "); // /311
  140. // 276 -> (46*6)
  141. // 3574
  142. // 3156 -> (526*6)
  143. // 41800 -> (.)
  144. // 37289 -> (5327*7)
  145. // 451100
  146. // 320076 -> (53346*6)
  147. // 131024
  148. }
  149.  
  150. public static void main (String[] args) throws java.lang.Exception
  151. {
  152. double test = 71174;
  153. SqrtByHand number = new SqrtByHand(test);
  154. System.out.println("The actual square root of " +test+ " is " +Math.sqrt(test));
  155.  
  156. System.out.println(test+ " has " +digitCounter()+ " digits greater than 1.");
  157.  
  158. System.out.println("test % digitCounter() = " + (test % digitCounter()));
  159.  
  160. System.out.println("test % 100,000 = " + (test % 100000));
  161. System.out.println("test % 10,000 = " + (test % 10000));
  162. System.out.println("test % 1,000 = " + (test % 1000));
  163. System.out.println("test % 100 = " + (test % 100));
  164. System.out.println("test % 10 = " + (test % 10));
  165.  
  166. System.out.println("test / 100,000 = " + (test / 100000));
  167. System.out.println("test / 10,000 = " + (test / 10000));
  168. System.out.println("test / 1,000 = " + (test / 1000));
  169. System.out.println("test / 100 = " + (test / 100));
  170. System.out.println("test / 10 = " + (test / 10));
  171.  
  172. print();
  173. }
  174. }
Success #stdin #stdout 0.24s 59244KB
stdin
Standard input is empty
stdout
The actual square root of 71174.0 is 266.78455727421704
71174.0 has 5 digits greater than 1.
test % digitCounter() = 4.0
test % 100,000 = 71174.0
test % 10,000 = 1174.0
test % 1,000 = 174.0
test % 100 = 74.0
test % 10 = 4.0
test / 100,000 = 0.71174
test / 10,000 = 7.1174
test / 1,000 = 71.174
test / 100 = 711.74
test / 10 = 7117.4

√71174.0
-4	-> (2*2)
 ------
 3 ↓
 3findFirst = 7.1174
selectDigits = 7.0
next2Digits = 1174.0
1174.0	->