diff --git a/ATBackendChallenge.iml b/ATBackendChallenge.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/ATBackendChallenge.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/out/production/ATBackendChallenge/Calculations.class b/out/production/ATBackendChallenge/Calculations.class new file mode 100644 index 0000000..703bb9a Binary files /dev/null and b/out/production/ATBackendChallenge/Calculations.class differ diff --git a/out/production/ATBackendChallenge/Main.class b/out/production/ATBackendChallenge/Main.class new file mode 100644 index 0000000..aad50a8 Binary files /dev/null and b/out/production/ATBackendChallenge/Main.class differ diff --git a/out/production/ATBackendChallenge/Person.class b/out/production/ATBackendChallenge/Person.class new file mode 100644 index 0000000..368c443 Binary files /dev/null and b/out/production/ATBackendChallenge/Person.class differ diff --git a/src/Calculations.java b/src/Calculations.java new file mode 100644 index 0000000..4875160 --- /dev/null +++ b/src/Calculations.java @@ -0,0 +1,102 @@ + +import java.math.BigDecimal; +import java.math.BigInteger; +import java.util.ArrayList; + +public class Calculations { + + + private BigInteger oneBigint = new BigInteger("1"); + private BigInteger zeroBigint = new BigInteger("0"); + private BigInteger twoBigint = new BigInteger("2"); + public Calculations() { + } + + //A method to display Arraylists + public void displayArraylist(ArrayList sourceList) + { + if ((sourceList.isEmpty())) + { + System.out.println("List is empty!"); + } + else + { + for(int i = 0; i primeNumbersToN(BigInteger n) + { + ArrayList primeNums = new ArrayList<>(); + if(n.compareTo(new BigInteger("2"))>0) + { + for (BigInteger i = twoBigint; i.compareTo(n)<0;i=i.add(oneBigint)) + { + if(isPrime(i)) + { + primeNums.add(i); + } + } + } + return primeNums; + } + private Boolean isPrime(BigInteger number) + { + if ((number.compareTo(oneBigint.add(BigInteger.ONE)))<0) + { + return false; + } + else if (number.equals(BigInteger.valueOf(2)) || number.equals(BigInteger.valueOf(3))) + { + return true; + } + else + { + BigInteger rootNumber = BigDecimal.valueOf(Math.sqrt(number.doubleValue())).toBigInteger(); + for (BigInteger i = twoBigint; i.compareTo(rootNumber.add(oneBigint))<0;i=i.add(oneBigint)) + { + if((number.remainder(i)).equals(zeroBigint)) + { + return false; + } + } + return true; + } + } + + //A function that takes an Int n and returns a List of all the factorials of the numbers between 0 and n inclusive + + + public ArrayList nFactorials(BigInteger n) + { + ArrayList numberFactorials = new ArrayList<>(); + if(n.compareTo(zeroBigint)>0) + { + for(BigInteger i = oneBigint; i.compareTo(n)<0;i=i.add(oneBigint)) + { + BigInteger nFact = nFactorial(i); + numberFactorials.add(nFact); + } + } + return numberFactorials; + } + private BigInteger nFactorial(BigInteger number) + { + BigInteger multiplier = new BigInteger("1"); + return computeFactorial(number,multiplier); + } + private BigInteger computeFactorial(BigInteger start, BigInteger result) + { + if(start.compareTo(zeroBigint)>0) + { + return computeFactorial(start.subtract(oneBigint), result.multiply(start)); + } + return result; + } + +} diff --git a/src/Main.java b/src/Main.java new file mode 100644 index 0000000..55deb38 --- /dev/null +++ b/src/Main.java @@ -0,0 +1,52 @@ +import java.math.BigInteger; +import java.util.ArrayList; +import java.util.Scanner; + +public class Main { + + public static Calculations calcs; + + public static void main(String[] args) + { + calcs = new Calculations(); + Scanner scanner = new Scanner(System.in); + + String yes; + do{ + evaluateCalculations(); + System.out.println("Enter 'y' to repeat operation: "); + yes = scanner.next(); + }while (yes.equals("y") || yes.equals("Y")); + } + + public static void evaluateCalculations() + { + Scanner scanner = new Scanner(System.in); + + BigInteger nPrimes, nFactorials; + do { + System.out.println("Enter a positive integer, n, to evaluate Primes between 0 and n: "); + nPrimes = scanner.nextBigInteger(); + }while (nPrimes.compareTo(BigInteger.ONE)<0); + listPrimes(nPrimes); + do { + System.out.println("Enter a positive integer to evaluate Factorials of integers between 0 and n: "); + nFactorials = scanner.nextBigInteger(); + }while (nFactorials.compareTo(BigInteger.ONE) < 0); + listFactorials(nFactorials); + + } + + public static void listPrimes(BigInteger n) + { + ArrayList listOfPrimes = calcs.primeNumbersToN(n); + calcs.displayArraylist(listOfPrimes); + } + + public static void listFactorials(BigInteger f) + { + ArrayList listOfFactorials = calcs.nFactorials(f); + calcs.displayArraylist(listOfFactorials); + } + +} diff --git a/src/Person.java b/src/Person.java new file mode 100644 index 0000000..7c96f05 --- /dev/null +++ b/src/Person.java @@ -0,0 +1,28 @@ +public class Person { + private String name; + private int Age; + + + public Person() { + } + + public int getAge() { + return Age; + } + + public void setAge(int age) { + if (age<=0) + { + throw new IllegalArgumentException("The value of age should be positive."); + } + Age = age; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +}