Pages

Tuesday, September 8, 2009

Euler Problem 26 solution

Time (s): ~0.021
package margusmartseppcode.From_20_to_29;

public class Problem_26 {
 // Brent's algorithm
 public static void main(String[] args) {
  int r = 1, c = 0, power, lam, t, tr, h, hr;
  for (int i = 999; i > 1; i--) {
   power = lam = 1;
   tr = (1 % i) * 10;
   hr = (tr % i) * 10;
   t = 1 / i;
   h = tr / i;

   while (t != h || tr != hr) {
    if (power == lam) {
     t = h;
     tr = hr;
     power *= 2;
     lam = 0;
    }
    h = hr / i;
    hr = (hr % i) * 10;
    lam++;
   }
   if (lam > c) {
    c = lam;
    r = i;
   }
  }
  System.out.println(r);
 }
}

No comments:

Post a Comment