diff options
| author | Dave Jacoby <jacoby.david@gmail.com> | 2022-06-16 18:07:18 -0400 |
|---|---|---|
| committer | Dave Jacoby <jacoby.david@gmail.com> | 2022-06-16 18:07:18 -0400 |
| commit | e28b4f047990a21b3e98b4e7b6b9e21f499d388a (patch) | |
| tree | 39ed88bfdf9d2689042d534606601a19edc0bf9b /challenge-169/michael-dicicco/java/ch1.java | |
| parent | b9ec9ea5dac6ae173154732b1c3f997676f798b9 (diff) | |
| parent | 5c4b8df3cd8a191dd7c42fc49e12b5dfc3a02cc2 (diff) | |
| download | perlweeklychallenge-club-e28b4f047990a21b3e98b4e7b6b9e21f499d388a.tar.gz perlweeklychallenge-club-e28b4f047990a21b3e98b4e7b6b9e21f499d388a.tar.bz2 perlweeklychallenge-club-e28b4f047990a21b3e98b4e7b6b9e21f499d388a.zip | |
Merge branch 'master' of https://github.com/manwar/perlweeklychallenge-club
Diffstat (limited to 'challenge-169/michael-dicicco/java/ch1.java')
| -rw-r--r-- | challenge-169/michael-dicicco/java/ch1.java | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/challenge-169/michael-dicicco/java/ch1.java b/challenge-169/michael-dicicco/java/ch1.java new file mode 100644 index 0000000000..d9978ca311 --- /dev/null +++ b/challenge-169/michael-dicicco/java/ch1.java @@ -0,0 +1,54 @@ +import java.util.ArrayList; +import java.util.List; + +/** + * + * @author mddicicco + */ +public class ch1 { + + /** + * @param args the command line arguments + */ + public static void main(String[] args) { + List<String> brilliants = new ArrayList<>(); + for (long i = 4L; i < 300L; i++) { + if (isBrilliant(primeFactors(i))){ + brilliants.add(Long.toString(i)); + } + } + System.out.println(String.join(", ", brilliants)); + } + + public static Boolean isBrilliant(List<Long> factors) { + return (factors.size() == 2 && factors.get(0).toString().length() == factors.get(1).toString().length()); + } + + public static List<Long> primeFactors(long n) { + List<Long> output = new ArrayList<>(); + + // Print the number of 2s that divide n + while (n % 2L == 0L) { + output.add(2L); + n /= 2L; + } + + // n must be odd at this point. So we can + // skip one element (Note i = i +2) + for (long i = 3L; i <= Math.sqrt(n); i += 2L) { + // While i divides n, print i and divide n + while (n % i == 0) { + output.add(i); + n /= i; + } + } + + // This condition is to handle the case when + // n is a prime number greater than 2 + if (n > 2L) { + output.add(n); + } + return output; + } + +} |
