Project Euler > Problem 129 > Repunit divisibility (Java Solution)

Problem:

A number consisting entirely of ones is called a repunit. We shall define R(k) to be a repunit of length k; for example, R(6) = 111111.

Given that n is a positive integer and GCD(n, 10) = 1, it can be shown that there always exists a value, k, for which R(k) is divisible by n, and let A(n) be the least such value of k; for example, A(7) = 6 and A(41) = 5.

The least value of n for which A(n) first exceeds ten is 17.

Find the least value of n for which A(n) first exceeds one-million.


Solution:

9183

Code:
The solution may include methods that will be found here: Library.java .

public interface EulerSolution{

public String run();

}
/* 
* Solution to Project Euler problem 29
* By Nayuki Minase
*
* http://nayuki.eigenstate.org/page/project-euler-solutions
* https://github.com/nayuki/Project-Euler-solutions
*/

import java.math.BigInteger;
import java.util.HashSet;
import java.util.Set;


public final class p029 implements EulerSolution {

public static void main(String[] args) {
System.out.println(new p029().run());
}


public String run() {
Set<BigInteger> generated = new HashSet<BigInteger>();
for (int a = 2; a <= 100; a++) {
for (int b = 2; b <= 100; b++)
generated.add(BigInteger.valueOf(a).pow(b));
}
return Integer.toString(generated.size());
}

}

No comments:

Post a Comment