**Problem:**

It is possible to write five as a sum in exactly six different ways:

4 + 1

3 + 2

3 + 1 + 1

2 + 2 + 1

2 + 1 + 1 + 1

1 + 1 + 1 + 1 + 1

How many different ways can one hundred be written as a sum of at least two positive integers?

4 + 1

3 + 2

3 + 1 + 1

2 + 2 + 1

2 + 1 + 1 + 1

1 + 1 + 1 + 1 + 1

How many different ways can one hundred be written as a sum of at least two positive integers?

**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