Page 1 of 1

how to writh method of log

Posted: Wed Feb 21, 2018 4:33 am
by maty.tsoraro@gmail.com
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

Re: how to writh method of log

Posted: Wed Feb 21, 2018 2:54 pm
by dbremmen@gmail.com
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));
    }
}

Re: how to writh method of log

Posted: Thu Feb 22, 2018 4:29 am
by maty.tsoraro@gmail.com
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 ?

Re: how to writh method of log

Posted: Thu Feb 22, 2018 3:43 pm
by dbremmen@gmail.com
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/

Re: how to writh method of log

Posted: Thu Feb 22, 2018 10:38 pm
by maty.tsoraro@gmail.com
Thanks a lot :P