Skip to content

Commit

Permalink
Finished all problems I am going to do in Java, still need to encapsu…
Browse files Browse the repository at this point in the history
…late packages
  • Loading branch information
dhermes committed Jun 18, 2014
1 parent 73cc246 commit 83b1677
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
25 changes: 25 additions & 0 deletions java/complete/no029.java
@@ -0,0 +1,25 @@
import java.math.BigInteger;
import java.util.HashMap;

class no029 {
public static int main(boolean verbose) {
int n = 100;
HashMap<BigInteger, Boolean> powers = new HashMap<BigInteger, Boolean>();
for (int base = 2; base < n + 1; base++) {
BigInteger base_as_big_int = BigInteger.valueOf(base);
for (int exponent = 2; exponent < n + 1; exponent++) {
powers.put(base_as_big_int.pow(exponent), true);
}
}

return powers.keySet().size();
}

public static int main() {
return main(false);
}

public static void main(String[] args) {
System.out.println(main(true));
}
}
73 changes: 73 additions & 0 deletions java/complete/no030.java
@@ -0,0 +1,73 @@
import java.util.Arrays;

class no030 {
public static int array_sum(int[] arr) {
if (arr == null || arr.length == 0) {
return 0;
} else {
int result = 0;
for (int i = 0; i < arr.length; i++) {
result += arr[i];
}
return result;
}
}

public static String join(String join_val, int[] arr) {
String result = "";
int length = arr.length;
for (int i = 0; i < length - 1; i++) {
result += Integer.toString(arr[i]) + join_val;
}

if (length > 0) {
result += Integer.toString(arr[length - 1]);
}

return result;
}

public static int sum_of_digits_powers(int n, int power) {
int result = 0;
int left_most_n = n;
while (left_most_n != 0) {
result += (int) Math.pow(left_most_n % 10, power);
left_most_n /= 10; // Integer division intended
}
return result;
}

public static String main(boolean verbose) {
int curr_size = 8;
int[] valid = new int[curr_size];
int curr_index = 0;
for (int i = 2; i < 999999 + 1; i++) {
if (sum_of_digits_powers(i, 5) == i) {
if (curr_index == curr_size) {
curr_size *= 2;
valid = Arrays.copyOf(valid, curr_size);
}

valid[curr_index] = i;
curr_index++;
}
}
valid = Arrays.copyOf(valid, curr_index);

String result = Integer.toString(array_sum(valid));
if (verbose) {
result += ".\nThe numbers satisfying this property are: ";
result += join(", ", valid) + ".";
}

return result;
}

public static String main() {
return main(false);
}

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

0 comments on commit 83b1677

Please sign in to comment.