codewars039: Odd/Even number of divisors

Sourcehtml

package codewars;
//https://www.codewars.com/kata/55830eec3e6b6c44ff000040/train/java
import java.math.BigInteger;
public class Oddity{
    public static String oddity(BigInteger n){
        BigInteger low = BigInteger.ONE;
        final BigInteger TWO = new BigInteger("2");
        BigInteger high = n;
        while(low.compareTo(high) <= 0){
            BigInteger mid = null;
            if(BigInteger.ZERO.compareTo(low.add(high).mod(TWO)) == 0){
                mid = low.add(high).divide(TWO);
            }else{
                mid = low.add(high).divide(TWO).add(BigInteger.ONE);
            }    
            int temp = mid.multiply(mid).compareTo(n);
            if(temp == 0){
                return "odd";
            }else if(temp < 0){
                low = mid.add(BigInteger.ONE);
            }else{
                high = mid.substract(BigInteger.ONE);
            }       
        }   
        return "even"; 
    }    
}

To judge if a natural number can be another natural number's square java

How to implement sqrt for BigInteger in Javaide

How to get all the divisors of a number.net

import static org.junit.Assert.assertEquals;
import org.junit.Test;
import java.math.BigInteger;
public class OddityTest{
    @Test    
    public void exampleTests(){
        assertEquals("odd", Oddity.oddity(BigInteger.valueOf(4)));
        assertEqulas("even", Oddity.oddity(BigInteger.valueOf(12)));
    }    
}
相關文章
相關標籤/搜索