aboutsummaryrefslogtreecommitdiff
path: root/challenge-169/adam-russell/java/ch-2.java
diff options
context:
space:
mode:
authordrbaggy <js5@sanger.ac.uk>2022-06-20 06:38:34 +0100
committerdrbaggy <js5@sanger.ac.uk>2022-06-20 06:38:34 +0100
commitcf74c017563b79bd9e1cceb10990bf13e40125f7 (patch)
tree2d4ab159f92f5651a4e6fd3262e259adec31040e /challenge-169/adam-russell/java/ch-2.java
parent26e9c6ce6f6f1815334d8a6e6599644b5196c7ac (diff)
parentb13601bf76ae50a91eae332a5faf08c99ad6300f (diff)
downloadperlweeklychallenge-club-cf74c017563b79bd9e1cceb10990bf13e40125f7.tar.gz
perlweeklychallenge-club-cf74c017563b79bd9e1cceb10990bf13e40125f7.tar.bz2
perlweeklychallenge-club-cf74c017563b79bd9e1cceb10990bf13e40125f7.zip
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-169/adam-russell/java/ch-2.java')
-rw-r--r--challenge-169/adam-russell/java/ch-2.java54
1 files changed, 54 insertions, 0 deletions
diff --git a/challenge-169/adam-russell/java/ch-2.java b/challenge-169/adam-russell/java/ch-2.java
new file mode 100644
index 0000000000..47c8d7813e
--- /dev/null
+++ b/challenge-169/adam-russell/java/ch-2.java
@@ -0,0 +1,54 @@
+import java.util.ArrayList;
+
+class Achilles{
+ private static final double EPSILON = 1e-15;
+ private static ArrayList primeFactors(long n){
+ ArrayList factors = new ArrayList();
+ while (n % 2 == 0){
+ factors.add(new Long(2));
+ n = n / 2;
+ }
+ for(long i = 3; i <= Math.sqrt(n); i = i + 2){
+ while (n % i == 0){
+ factors.add(new Long(i));
+ n = n / i;
+ }
+ }
+ if(n > 2)
+ factors.add(new Long(n));
+ return factors;
+ }
+
+ private static boolean isAchilles(int n){
+ ArrayList factors = primeFactors(n);
+ for(int i = 0; i < factors.size(); i++){
+ if(n % Math.pow(((Long)factors.get(i)).longValue() + 0.0, 2) != 0)
+ return false;
+ }
+ for(int i = 2; i <= Math.sqrt(n); i++) {
+ double d = Math.log(n) / Math.log(i);
+ if(Math.abs(d - Math.round(d)) < EPSILON)
+ return false;
+ }
+ return true;
+ }
+
+ public static ArrayList nAchilles(int n){
+ ArrayList achilles = new ArrayList();
+ int i = 1;
+ do{
+ i++;
+ if(isAchilles(i))
+ achilles.add(new Integer(i));
+ }while(achilles.size() < n);
+ return achilles;
+ }
+
+ public static void main(String[] args){
+ ArrayList achilles = Achilles.nAchilles(20);
+ for(int i = 0; i < achilles.size() - 1; i++){
+ System.out.print(achilles.get(i) + ", ");
+ }
+ System.out.println(achilles.get(achilles.size() - 1));
+ }
+} \ No newline at end of file