**Problem:**

The minimum number of cubes to cover every visible face on a cuboid measuring 3 x 2 x 1 is twenty-two.

If we then add a second layer to this solid it would require forty-six cubes to cover every visible face, the third layer would require seventy-eight cubes, and the fourth layer would require one-hundred and eighteen cubes to cover every visible face.

However, the first layer on a cuboid measuring 5 x 1 x 1 also requires twenty-two cubes; similarly the first layer on cuboids measuring 5 x 3 x 1, 7 x 2 x 1, and 11 x 1 x 1 all contain forty-six cubes.

We shall define C(n) to represent the number of cuboids that contain n cubes in one of its layers. So C(22) = 2, C(46) = 4, C(78) = 5, and C(118) = 8.

It turns out that 154 is the least value of n for which C(n) = 10.

Find the least value of n for which C(n) = 1000.

If we then add a second layer to this solid it would require forty-six cubes to cover every visible face, the third layer would require seventy-eight cubes, and the fourth layer would require one-hundred and eighteen cubes to cover every visible face.

However, the first layer on a cuboid measuring 5 x 1 x 1 also requires twenty-two cubes; similarly the first layer on cuboids measuring 5 x 3 x 1, 7 x 2 x 1, and 11 x 1 x 1 all contain forty-six cubes.

We shall define C(n) to represent the number of cuboids that contain n cubes in one of its layers. So C(22) = 2, C(46) = 4, C(78) = 5, and C(118) = 8.

It turns out that 154 is the least value of n for which C(n) = 10.

Find the least value of n for which C(n) = 1000.

**Solution:**

983

**Code:**

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

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

public interface EulerSolution{

public String run();

}

/*

* Solution to Project Euler problem 26

* By Nayuki Minase

*

* http://nayuki.eigenstate.org/page/project-euler-solutions

* https://github.com/nayuki/Project-Euler-solutions

*/

import java.util.HashMap;

import java.util.Map;

public final class p026 implements EulerSolution {

public static void main(String[] args) {

System.out.println(new p026().run());

}

public String run() {

int bestNumber = 0;

int bestLength = 0;

for (int i = 1; i <= 1000; i++) {

int len = getCycleLength(i);

if (len > bestLength) {

bestNumber = i;

bestLength = len;

}

}

return Integer.toString(bestNumber);

}

private static int getCycleLength(int n) {

Map<Integer,Integer> stateToIter = new HashMap<Integer,Integer>();

int state = 1;

int iter = 0;

while (!stateToIter.containsKey(state)) {

stateToIter.put(state, iter);

state = state * 10 % n;

iter++;

}

return iter - stateToIter.get(state);

}

}

## No comments :

## Post a Comment