Project Euler: Problem 80 – Square root digital expansion

Problem 80:

It is well known that if the square root of a natural number is not an integer, then it is irrational. The decimal expansion of such square roots is infinite without any repeating pattern at all.
The square root of two is 1.41421356237309504880…, and the digital sum of the first one hundred decimal digits is 475.
For the first one hundred natural numbers, find the total of the digital sums of the first one hundred decimal digits for all the irrational square roots.


Since I need a lot of digits / precision, I need to use the sister class of BigInteger: BigDecimal. I saw BigDecimal had no square root method, so I was going to use the power method, but it only accepts integers (so I can’t raise a decimal to the 1/2 power, i.e. square root). So I had to create a square root method myself (after a little research).
With that completed, I just needed to check to see if the resulting square root was an integer or decimal. From there, just add the first 100 decimal values together for all the non-integer square roots.

int answer = 0;

MathContext precision = new MathContext(105);
for(int i = 1; i <= 100; i++) {
    BigDecimal temp = EulerUtils.sqrt(BigDecimal.valueOf(i), precision);
    String number = temp.toString();
    if(number.indexOf(".") == -1) {
    } else {
        number = number.substring(0,101);
        number = number.replace(".","");
        for(char c:number.toCharArray()) {
            answer += Integer.parseInt(""+c);

Note: sqrt() is a method I wrote to calculate the square root of a BigDecimal up to a given precision.

Project Euler: Problem 26 – Reciprocal cycles

Problem 26:

A unit fraction contains 1 in the numerator. The decimal representation of the unit fractions with denominators 2 to 10 are given:

1/2 = 0.5
1/3 = 0.(3)
1/4 = 0.25
1/5 = 0.2
1/6 = 0.1(6)
1/7 = 0.(142857)
1/8 = 0.125
1/9 = 0.(1)
1/10 = 0.1

Where 0.1(6) means 0.166666…, and has a 1-digit recurring cycle. It can be seen that 1/7 has a 6-digit recurring cycle.
Find the value of d < 1000 for which 1/d contains the longest recurring cycle in its decimal fraction part.


I had no idea how to easily calculate the length of a recurring cycle, especially for odd, made up cases such as: 0.111567111567…. How would I know, programmatically, if once I reach “1115” that it is no longer a recurring cycle (of ones), but to keep going to include “67” to complete the cycle?

//Did not implement

While researching Repeating Decimals, I found out these are called cyclic numbers.  Upon researching more about cyclic numbers (and how to calculate them), I came across the Wikipedia article that appeared to go up to (but not exceeding) 1000.