/* package whatever; // don't place package name!
cd "C:\Users\kmsco\Documents\java coding"
"C:\Program Files\Java\jdk-18\bin\javac.exe" SqrtByHand.java
"C:\Program Files\Java\jdk-18\bin\java.exe" SqrtByHand
https://i...content-available-to-author-only...e.com/wid9mq*/
import java.util.*; // NumberFormat
import java.lang.*;
import java.io.*;
import java.lang.Math;
import java.text.*; // NumberFormat
/* Name of the class has to be "Main" only if the class is public. */
class SqrtByHand
{
//https://w...content-available-to-author-only...d.com/article/2245549/java-101-class-and-object-initialization-in-java.html
static double num = 0;
static double pos = 0;
public SqrtByHand(double test)
{
this.num = test;
}
public static int digitCounter() // don't call this from main
{
int count = 0; //, num = 0003452;
int wholeNum = (int) num;
while (wholeNum != 0)
{
// wholeNum = wholeNum/10
wholeNum /= 10;
++count;
}
//System.out.println("Number of digits: " + count);
return count;
}
public static double firstStep()
{
double numDigits = (double) digitCounter();
double findFirst = 0;
double firstDigit = 0;
if(numDigits % 2 == 1) // odd
{
// 71174
// 10^(numDigits-1) = 10^4 = 10,000
// 71174 % 10,000 = 1174 // 71174-1174 = 70,000 / 10^4 = 7
// 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
// if answer < 100 ans*100+next2
pos = numDigits - 1;
findFirst
= num
/ Math.
pow(10,pos
); // 10 ^ num of digits-1; 71174 / 10^4 = 7.1174 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 }
else //if(numDigits % 2 == 0) // even
{
// 711740
// 10^(numDigits-2) = 10^4 = 10,000
// 711740 % 10,000 = 1740 // 711740-1740 = 710,000 / 10^4 = 71
// 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
// if answer < 100 ans*100+next2
pos = numDigits - 2;
findFirst
= num
/ Math.
pow(10,pos
); // 711740 / 10^4 = 71.174 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 }
return firstDigit;
}
public static int closestSquareable() // only runs first time
{
double firstDigit = firstStep();
int justUnder = 0, count = 0;
for (count = 0; justUnder < firstDigit; count ++) // count*count < firstDigit
{
justUnder = count * count;
/*System.out.println("justUnder = count * count: ");
System.out.print(justUnder);
System.out.print(" = ");
System.out.print(count);
System.out.print(" * ");
System.out.println(count);*/
if(justUnder > firstDigit)
break;
}
return count-1;
}
public static double used()
{
return 0;
}
public static double difference() //(nextPair, answerDoubled) // numbers on the side
{
return 0; // (nextPair - answerDoubled);
}
public static double next2nums()
{
double findFirst
= num
/ Math.
pow(10,pos
); // 71174 / 10^2 = 711.74 System.
out.
println("findFirst = " + findFirst
); 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 System.
out.
println("selectDigits = " + selectDigits
); double next2Digits
= num
- (selectDigits
*Math.
pow(10,pos
)); // 71174-(711*10^2) = 71100 = 74 System.
out.
println("next2Digits = " + next2Digits
); // double used =
double next = (difference() * 100) + next2Digits;
//for(int alpha=0; alpha<numDigits; alpha++)
{
}
pos -= 2;
return next;
}
public static void print()
{
double closestSquare = closestSquareable()*closestSquareable();
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
// 266.76
System.
out.
println("√" + num
); // *71174* System.
out.
println("-" + (formatter.
format(closestSquare
)) + "\t-> (" +closestSquareable
()+ "*" +closestSquareable
()+ ")"); // 4 -> (2*2) System.
out.
println(" ------"); // equals System.
out.
print(" " +(formatter.
format(firstStep
()-closestSquare
))); System.
out.
println(" \u2193"); // difference // U+2193 = ↓ System.
out.
print(" " +(formatter.
format(firstStep
()-closestSquare
))); System.
out.
print(next2nums
()); System.
out.
print("\t-> "); // /311 // 276 -> (46*6)
// 3574
// 3156 -> (526*6)
// 41800 -> (.)
// 37289 -> (5327*7)
// 451100
// 320076 -> (53346*6)
// 131024
}
{
double test = 71174;
SqrtByHand number = new SqrtByHand(test);
System.
out.
println("The actual square root of " +test
+ " is " +Math.
sqrt(test
));
System.
out.
println(test
+ " has " +digitCounter
()+ " digits greater than 1.");
System.
out.
println("test % digitCounter() = " + (test
% digitCounter
()));
System.
out.
println("test % 100,000 = " + (test
% 100000)); System.
out.
println("test % 10,000 = " + (test
% 10000)); System.
out.
println("test % 1,000 = " + (test
% 1000)); System.
out.
println("test % 100 = " + (test
% 100)); System.
out.
println("test % 10 = " + (test
% 10));
System.
out.
println("test / 100,000 = " + (test
/ 100000)); System.
out.
println("test / 10,000 = " + (test
/ 10000)); System.
out.
println("test / 1,000 = " + (test
/ 1000)); System.
out.
println("test / 100 = " + (test
/ 100)); System.
out.
println("test / 10 = " + (test
/ 10));
print();
}
}