V
V
Volodymyr Palamar2019-12-03 17:26:50
Java
Volodymyr Palamar, 2019-12-03 17:26:50

How to subtract the root and write beautifully?

I have a problem. I need to subtract the root of 388, but the result of 19.69772 does not suit me, I need 2√97.
I tried to get the algorithm from the photomath program but to no avail.
In theory it should turn out √4 • √97 = √2²•√97= 2•√97
BUT HOW TO DO IT IN JAVA????

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Cheremisin, 2019-12-03
@GORNOSTAY25

It's not entirely clear why you don't like the result?
In Python: math.sqrt(388) = 19.697715603592208
In Java: Math.sqrt(388) = 19.697715603592208
You can take the remainder (%) of division by 2 or 4 (and so on), and then take the root of the division result.

int number = 388;
    int tail = number;
    int mul = 1;
    if(number % 2 == 0) {
      mul = 2; tail = number/mul;
    }
    if(number % 3 == 0) {
      mul = 3; tail = number/mul;
    }
    if(number % 4 == 0) {
      mul = 4; tail = number/mul;
    }
    if(number % 5 == 0) {
      mul = 5; tail = number/mul;
    }
    if(number % 6 == 0) {
      mul = 6; tail = number/mul;
    }
    if(number % 7 == 0) {
      mul = 7; tail = number/mul;
    }
    if(number % 8 == 0) {
      mul = 8; tail = number/mul;
    }
    if(number % 9 == 0) {
      mul = 9; tail = number/mul;
    }
    
    System.out.printf("%d = %d√%d = %d * %.20f\n", number, mul, tail, mul, Math.sqrt(tail));

Result HA! Optimized a bit!
int number = 388;
    int tail = number;
    int mul = 1;
    for(int i=2; i<number/2; i++) {
      if(number % i == 0) {
        mul = i; tail = number/mul;
      }
    }
    
    System.out.println(Math.sqrt(tail));
    System.out.printf("%d = %d√%d = %d * %.20f\n", number, mul, tail, mul, Math.sqrt(tail));

Result!
388 = 97√4 = 97 * 2,00000000000000000000

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question