Java Decimal Format isprime(double x)

Here you can find the source of isprime(double x)

Description

isprime

License

Mozilla Public License

Declaration

public static double isprime(double x) 

Method Source Code

//package com.java2s;
/** This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *//*from  ww w .j  av a2s .  co  m*/

import java.text.*;
import java.math.*;

public class Main {
    public static double isprime(double x) {
        if ((Math.round(x) != toFix(x, 12)) || (x < 2)) {
            throw new IllegalArgumentException("isPrime(n), where n >= 2 is an integer");
        } else {
            long n = Math.round(x);
            for (long i = 2; i <= Math.sqrt(n); i++) {
                double factor = n * 1.0 / i;
                if (Math.round(factor) == toFix(factor, 12))
                    return 0;
            }
            return 1;
        }
    }

    public static double toFix(double x, int dp) {
        String format = "";
        for (int i = 0; i < dp; i++) {
            format = format + "#";
        }
        return Double.parseDouble((new DecimalFormat("#0." + format)).format(x));
    }

    public static String toFix(BigDecimal x, int dp) {
        String format = "";
        for (int i = 0; i < dp; i++) {
            format = format + "#";
        }
        return (new DecimalFormat("#0." + format)).format(x);
    }
}

Related

  1. getStringRepresentationForDouble(double value)
  2. getTwoPoint(double val)
  3. getValue(double value)
  4. isDouble(String _str)
  5. isLikelyDouble(long value)
  6. isValidDouble(String value, int decimals)
  7. Julian_Cal(double JDN)
  8. listToString(List list)
  9. numToString(double num)