-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution29.java
50 lines (43 loc) · 1.49 KB
/
Solution29.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//Problem 29 - Project Euler
/*
GOAL:
Find # of distinct terms in the sequence generated by a^b for 2 ≤ a ≤ 100 and 2 ≤ b ≤ 100
LOGIC:
STEP1:
1st Loop to get base (a) abd 2nd Loop to calculate powers from 2 to 100 (b)
STEP2:
Convert the BigIntger Powers to string and stroe in an array
STEP3:
Pass the string array to an Hashset, get its size - 1, as null is not counted
*/
import java.io.*;
import java.util.*;
import java.math.BigInteger;
public class Solution29 {
public static void main(String[] args) {
String[] big_hold = new String[10000];
int id = 0;
BigInteger start = BigInteger.ONE;
String store_temp;
//get base
for(int i=2; i<=5; i++){
store_temp = Integer.toString(i);
BigInteger big = new BigInteger(store_temp);
//find all powers from 2 to 100
for (int j=2; j<=5; j++) {
start = big.pow(j);
store_temp = start.toString();
System.out.println(store_temp);
big_hold[id] = store_temp;
id++;
}
}
//A collection that contains no duplicate elements.
//More formally, sets contain no pair of elements e1 and e2 such that e1.equals(e2), and at most one null element.
Set<String> refined_string = new HashSet<String>(Arrays.asList(big_hold));
int size = refined_string.size();
System.out.println(refined_string);//1st element is null
System.out.println(size);
}
}
//ANSWER: 9183