replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
edited according to Exelian comment
Source Link

The basic idea, since pi is an infinite long non-repeating decimal number all number sequences must occur within itsince pi is an infinite long non-repeating decimal number all number sequences must occur within it (read edit). Therefor your answer is inside pi!! As such we can just apply a regex search on pi searching for you answer. If we are unable to find a good answer then we will just double the size of pi that we are search on!

It really easy, in fact one could say that it is as easy as pi :)

Edit
Pi has not been proven to contain every sequence of finite numbers within it. The fact that pi is infinite and non-repeating is not sufficient proof for such as statement as proven by Exelian. However many mathematicians do believe pi contains every sequence of finite numbers.

The basic idea, since pi is an infinite long non-repeating decimal number all number sequences must occur within it. Therefor your answer is inside pi!! As such we can just apply a regex search on pi searching for you answer. If we are unable to find a good answer then we will just double the size of pi that we are search on!

It really easy, in fact one could say that it is as easy as pi :)

The basic idea, since pi is an infinite long non-repeating decimal number all number sequences must occur within it (read edit). Therefor your answer is inside pi!! As such we can just apply a regex search on pi searching for you answer. If we are unable to find a good answer then we will just double the size of pi that we are search on!

It really easy, in fact one could say that it is as easy as pi :)

Edit
Pi has not been proven to contain every sequence of finite numbers within it. The fact that pi is infinite and non-repeating is not sufficient proof for such as statement as proven by Exelian. However many mathematicians do believe pi contains every sequence of finite numbers.

fix code formatting, fix syntax coloring.
Source Link
Justin
  • 20.9k
  • 9
  • 62
  • 112

import java.math.BigDecimal; import java.math.RoundingMode; import java.util.regex.Matcher; import java.util.regex.Pattern;

import static java.lang.Math.sqrt;

public class myClass {

    private static final BigDecimal TWO = new BigDecimal("2");
    private static final BigDecimal FOUR = new BigDecimal("4");
    private static final BigDecimal FIVE = new BigDecimal("5");
    private static final BigDecimal TWO_THIRTY_NINE = new BigDecimal("239");

    public static BigDecimal pi(int numDigits) {

        int calcDigits = numDigits + 10;

        return FOUR.multiply((FOUR.multiply(arccot(FIVE, calcDigits)))
                .subtract(arccot(TWO_THIRTY_NINE, calcDigits)))
                .setScale(numDigits, RoundingMode.DOWN);
    }

    private static BigDecimal arccot(BigDecimal x, int numDigits) {

        BigDecimal unity = BigDecimal.ONE.setScale(numDigits,
                RoundingMode.DOWN);
        BigDecimal sum = unity.divide(x, RoundingMode.DOWN);
        BigDecimal xpower = new BigDecimal(sum.toString());
        BigDecimal term = null;

        boolean add = false;

        for (BigDecimal n = new BigDecimal("3"); term == null ||
                term.compareTo(BigDecimal.ZERO) != 0; n = n.add(TWO)) {

            xpower = xpower.divide(x.pow(2), RoundingMode.DOWN);
            term = xpower.divide(n, RoundingMode.DOWN);
            sum = add ? sum.add(term) : sum.subtract(term);
            add = !add;
        }
        return sum;
    }

    public static void main(String[] args) throws Exception {

        int sqrtThis = 3;
        int expectedPercision = 4;

        int intgerAnswer = (int) sqrt(sqrtThis);

        int cantThinkOfVarName = expectedPercision - String.valueOf(intgerAnswer).length();

        boolean done = false;
        int piPrecision = 10000 * expectedPercision;

        Double bestMatch = -1.0;

        while (done == false) {
            BigDecimal PI = pi(piPrecision);
            String piString = PI.toString();

            Pattern p = Pattern.compile(intgerAnswer + "[0-9]{" + cantThinkOfVarName + "}");
            Matcher m = p.matcher(piString);

            Double offset = sqrtThis + 1.0;

            while (m.find()) {
                Double d = Double.parseDouble(m.group(0));
                d = d / Math.pow(10, cantThinkOfVarName);

                if ((int) (d * d) == sqrtThis ||(int) (d * d) == sqrtThis + 1 ) {
                    done = true;

                    Double newOffSet = Math.abs(d * d - sqrtThis);
                    if (newOffSet < offset) {
                        offset = newOffSet;
                        bestMatch = d;
                    }
                }
            }
            piPrecision = piPrecision + piPrecision;
        }

        System.out.println(bestMatch);
    }
}
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static java.lang.Math.sqrt;

public class myClass {

    private static final BigDecimal TWO = new BigDecimal("2");
    private static final BigDecimal FOUR = new BigDecimal("4");
    private static final BigDecimal FIVE = new BigDecimal("5");
    private static final BigDecimal TWO_THIRTY_NINE = new BigDecimal("239");

    public static BigDecimal pi(int numDigits) {

        int calcDigits = numDigits + 10;

        return FOUR.multiply((FOUR.multiply(arccot(FIVE, calcDigits)))
                .subtract(arccot(TWO_THIRTY_NINE, calcDigits)))
                .setScale(numDigits, RoundingMode.DOWN);
    }

    private static BigDecimal arccot(BigDecimal x, int numDigits) {

        BigDecimal unity = BigDecimal.ONE.setScale(numDigits,
                RoundingMode.DOWN);
        BigDecimal sum = unity.divide(x, RoundingMode.DOWN);
        BigDecimal xpower = new BigDecimal(sum.toString());
        BigDecimal term = null;

        boolean add = false;

        for (BigDecimal n = new BigDecimal("3"); term == null ||
                term.compareTo(BigDecimal.ZERO) != 0; n = n.add(TWO)) {

            xpower = xpower.divide(x.pow(2), RoundingMode.DOWN);
            term = xpower.divide(n, RoundingMode.DOWN);
            sum = add ? sum.add(term) : sum.subtract(term);
            add = !add;
        }
        return sum;
    }

    public static void main(String[] args) throws Exception {

        int sqrtThis = 3;
        int expectedPercision = 4;

        int intgerAnswer = (int) sqrt(sqrtThis);

        int cantThinkOfVarName = expectedPercision - String.valueOf(intgerAnswer).length();

        boolean done = false;
        int piPrecision = 10000 * expectedPercision;

        Double bestMatch = -1.0;

        while (done == false) {
            BigDecimal PI = pi(piPrecision);
            String piString = PI.toString();

            Pattern p = Pattern.compile(intgerAnswer + "[0-9]{" + cantThinkOfVarName + "}");
            Matcher m = p.matcher(piString);

            Double offset = sqrtThis + 1.0;

            while (m.find()) {
                Double d = Double.parseDouble(m.group(0));
                d = d / Math.pow(10, cantThinkOfVarName);

                if ((int) (d * d) == sqrtThis ||(int) (d * d) == sqrtThis + 1 ) {
                    done = true;

                    Double newOffSet = Math.abs(d * d - sqrtThis);
                    if (newOffSet < offset) {
                        offset = newOffSet;
                        bestMatch = d;
                    }
                }
            }
            piPrecision = piPrecision + piPrecision;
        }

        System.out.println(bestMatch);
    }
}

Didn't feel like implementing input. To test code change sqrtThissqrtThis and expectedPercisionexpectedPercision.

import java.math.BigDecimal; import java.math.RoundingMode; import java.util.regex.Matcher; import java.util.regex.Pattern;

import static java.lang.Math.sqrt;

public class myClass {

    private static final BigDecimal TWO = new BigDecimal("2");
    private static final BigDecimal FOUR = new BigDecimal("4");
    private static final BigDecimal FIVE = new BigDecimal("5");
    private static final BigDecimal TWO_THIRTY_NINE = new BigDecimal("239");

    public static BigDecimal pi(int numDigits) {

        int calcDigits = numDigits + 10;

        return FOUR.multiply((FOUR.multiply(arccot(FIVE, calcDigits)))
                .subtract(arccot(TWO_THIRTY_NINE, calcDigits)))
                .setScale(numDigits, RoundingMode.DOWN);
    }

    private static BigDecimal arccot(BigDecimal x, int numDigits) {

        BigDecimal unity = BigDecimal.ONE.setScale(numDigits,
                RoundingMode.DOWN);
        BigDecimal sum = unity.divide(x, RoundingMode.DOWN);
        BigDecimal xpower = new BigDecimal(sum.toString());
        BigDecimal term = null;

        boolean add = false;

        for (BigDecimal n = new BigDecimal("3"); term == null ||
                term.compareTo(BigDecimal.ZERO) != 0; n = n.add(TWO)) {

            xpower = xpower.divide(x.pow(2), RoundingMode.DOWN);
            term = xpower.divide(n, RoundingMode.DOWN);
            sum = add ? sum.add(term) : sum.subtract(term);
            add = !add;
        }
        return sum;
    }

    public static void main(String[] args) throws Exception {

        int sqrtThis = 3;
        int expectedPercision = 4;

        int intgerAnswer = (int) sqrt(sqrtThis);

        int cantThinkOfVarName = expectedPercision - String.valueOf(intgerAnswer).length();

        boolean done = false;
        int piPrecision = 10000 * expectedPercision;

        Double bestMatch = -1.0;

        while (done == false) {
            BigDecimal PI = pi(piPrecision);
            String piString = PI.toString();

            Pattern p = Pattern.compile(intgerAnswer + "[0-9]{" + cantThinkOfVarName + "}");
            Matcher m = p.matcher(piString);

            Double offset = sqrtThis + 1.0;

            while (m.find()) {
                Double d = Double.parseDouble(m.group(0));
                d = d / Math.pow(10, cantThinkOfVarName);

                if ((int) (d * d) == sqrtThis ||(int) (d * d) == sqrtThis + 1 ) {
                    done = true;

                    Double newOffSet = Math.abs(d * d - sqrtThis);
                    if (newOffSet < offset) {
                        offset = newOffSet;
                        bestMatch = d;
                    }
                }
            }
            piPrecision = piPrecision + piPrecision;
        }

        System.out.println(bestMatch);
    }
}

Didn't feel like implementing input. To test code change sqrtThis and expectedPercision.

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static java.lang.Math.sqrt;

public class myClass {

    private static final BigDecimal TWO = new BigDecimal("2");
    private static final BigDecimal FOUR = new BigDecimal("4");
    private static final BigDecimal FIVE = new BigDecimal("5");
    private static final BigDecimal TWO_THIRTY_NINE = new BigDecimal("239");

    public static BigDecimal pi(int numDigits) {

        int calcDigits = numDigits + 10;

        return FOUR.multiply((FOUR.multiply(arccot(FIVE, calcDigits)))
                .subtract(arccot(TWO_THIRTY_NINE, calcDigits)))
                .setScale(numDigits, RoundingMode.DOWN);
    }

    private static BigDecimal arccot(BigDecimal x, int numDigits) {

        BigDecimal unity = BigDecimal.ONE.setScale(numDigits,
                RoundingMode.DOWN);
        BigDecimal sum = unity.divide(x, RoundingMode.DOWN);
        BigDecimal xpower = new BigDecimal(sum.toString());
        BigDecimal term = null;

        boolean add = false;

        for (BigDecimal n = new BigDecimal("3"); term == null ||
                term.compareTo(BigDecimal.ZERO) != 0; n = n.add(TWO)) {

            xpower = xpower.divide(x.pow(2), RoundingMode.DOWN);
            term = xpower.divide(n, RoundingMode.DOWN);
            sum = add ? sum.add(term) : sum.subtract(term);
            add = !add;
        }
        return sum;
    }

    public static void main(String[] args) throws Exception {

        int sqrtThis = 3;
        int expectedPercision = 4;

        int intgerAnswer = (int) sqrt(sqrtThis);

        int cantThinkOfVarName = expectedPercision - String.valueOf(intgerAnswer).length();

        boolean done = false;
        int piPrecision = 10000 * expectedPercision;

        Double bestMatch = -1.0;

        while (done == false) {
            BigDecimal PI = pi(piPrecision);
            String piString = PI.toString();

            Pattern p = Pattern.compile(intgerAnswer + "[0-9]{" + cantThinkOfVarName + "}");
            Matcher m = p.matcher(piString);

            Double offset = sqrtThis + 1.0;

            while (m.find()) {
                Double d = Double.parseDouble(m.group(0));
                d = d / Math.pow(10, cantThinkOfVarName);

                if ((int) (d * d) == sqrtThis ||(int) (d * d) == sqrtThis + 1 ) {
                    done = true;

                    Double newOffSet = Math.abs(d * d - sqrtThis);
                    if (newOffSet < offset) {
                        offset = newOffSet;
                        bestMatch = d;
                    }
                }
            }
            piPrecision = piPrecision + piPrecision;
        }

        System.out.println(bestMatch);
    }
}

Didn't feel like implementing input. To test code change sqrtThis and expectedPercision.

Source Link
Loading