Project Euler > Problem 130 > Composites with prime repunit property (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.

You are given that for all primes, p [>] 5, that p [−] 1 is divisible by A(p). For example, when p = 41, A(41) = 5, and 40 is divisible by 5.

However, there are rare composite values for which this is also true; the first five examples being 91, 259, 451, 481, and 703.

Find the sum of the first twenty-five composite values of n for which
GCD(n, 10) = 1 and n [−] 1 is divisible by A(n).


Solution:

443839

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

public interface EulerSolution{

public String run();

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


public final class p030 implements EulerSolution {

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


public String run() {
// As stated in the problem, 1 = 1^5 is excluded.
// If a number has at least n >= 7 digits, then even if every digit is 9,
// n * 9^5 is still less than the number (which is at least 10^n).
int sum = 0;
for (int i = 2; i < 1000000; i++) {
if (i == fifthPowerDigitSum(i))
sum += i;
}
return Integer.toString(sum);
}


private static int fifthPowerDigitSum(int x) {
int sum = 0;
while (x != 0) {
int y = x % 10;
sum += y * y * y * y * y;
x /= 10;
}
return sum;
}

}


No comments :

Post a Comment

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact