how to writh method of log

Do you have a compilation error or a difficult problem to solve? Ask for help here
Post Reply
maty.tsoraro@gmail.com
Posts: 32
Joined: Thu Dec 21, 2017 10:06 pm

how to writh method of log

Post by maty.tsoraro@gmail.com » Wed Feb 21, 2018 4:33 am

hi

this is the URL link of the code
http://www.beta.browxy.com#USER_157857


any idea of to write the method for log2(n)?
thanks

dbremmen@gmail.com
Posts: 63
Joined: Sun Jan 07, 2018 6:30 pm

Re: how to writh method of log

Post by dbremmen@gmail.com » Wed Feb 21, 2018 2:54 pm

Hi!
To use logarithm you need to import the Math class and use the method log

Code: Select all

import java.lang.Math;

public class Calc {
    public static void main(String[] args){
      System.out.println (calc(16));
    }

    public static double calc (int n) {
        if(n <=2 )
        return 1 ;
        return (calc(n/2) * Math.log(n));
    }
}

maty.tsoraro@gmail.com
Posts: 32
Joined: Thu Dec 21, 2017 10:06 pm

Re: how to writh method of log

Post by maty.tsoraro@gmail.com » Thu Feb 22, 2018 4:29 am

hi,
getting an error

symbol: method log2(int)
location: class java.lang.Math

also,i need the log to be with base 2
is ther a way to writhe the method log without importint the class math ?

dbremmen@gmail.com
Posts: 63
Joined: Sun Jan 07, 2018 6:30 pm

Re: how to writh method of log

Post by dbremmen@gmail.com » Thu Feb 22, 2018 3:43 pm

Hi!

The log2 method is not a valid method from the Math class. Also there is no log2 method in the standard java JDK.
To have a log2 functionality you should do something like this:

Math.log(x)/Math.log(2)

See: http://www.giannistsakiris.com/2010/01/ ... n-in-java/

maty.tsoraro@gmail.com
Posts: 32
Joined: Thu Dec 21, 2017 10:06 pm

Re: how to writh method of log

Post by maty.tsoraro@gmail.com » Thu Feb 22, 2018 10:38 pm

Thanks a lot :P

Post Reply