From 17f8bd804196099583d1897e84068a61b361f659 Mon Sep 17 00:00:00 2001 From: IreneAchola Date: Wed, 12 Sep 2018 17:54:36 +0300 Subject: [PATCH] Challenge 1, 2 and 5 --- ATBackendChallenge.iml | 11 ++ .../ATBackendChallenge/Calculations.class | Bin 0 -> 3446 bytes out/production/ATBackendChallenge/Main.class | Bin 0 -> 2336 bytes .../ATBackendChallenge/Person.class | Bin 0 -> 838 bytes src/Calculations.java | 102 ++++++++++++++++++ src/Main.java | 52 +++++++++ src/Person.java | 28 +++++ 7 files changed, 193 insertions(+) create mode 100644 ATBackendChallenge.iml create mode 100644 out/production/ATBackendChallenge/Calculations.class create mode 100644 out/production/ATBackendChallenge/Main.class create mode 100644 out/production/ATBackendChallenge/Person.class create mode 100644 src/Calculations.java create mode 100644 src/Main.java create mode 100644 src/Person.java 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 0000000000000000000000000000000000000000..703bb9a7db8f0bf9e98bab42f0bd25acbbb47153 GIT binary patch literal 3446 zcmb7G`*&2;75?tbB=g`B$N(`xLIQ|PX7WP7R)~mXLd7P*HVA@k^=5Jj7w+6U+_?h= z@zMHjeYEyrvDUWsK_9jv>LkiqUH!T3FE0CY*XqB)D(bh-y_p9D*OHKP&)NH&y}!M` zz0b)%KmF+M0Jh`96t?2wIE)0QQ;49D#4u(OFy(tJfm!(;PoWc55+`sn0Xu~v9Fe#( zl;Sv*!b&_WgO@;AzP=0<*?3xpGjTj3d$}iqxfCs)P2o|T6WL=TdpybE36VY7LRj)I zBJ%Ib@V!M*R8~)=@HC!D;aNN#Mh9AiCMHya-7}@A&4d>Fv^xI%|6F;;y8BrZ#UKu^h|ats$D}JES2t>du%NR*YMY zIaw)Ao8CcV+NLB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kHhXDOJy8BVK9zEI$}Kr>#2-oc0?)v;EY7il5;v)?aNr4 ze=X8!5);@8MW)$=Q}k&Wnto5=4X*mu1{n|D zzvPn=W0UAan{E0Bi|pEjDGE0I|IZcJ86}mZpe;o7(6ttz_zoTWFW9=6zb9sbbz|YoX8E k*e=CKRILZnd3Xuy-!W~`OxtsgtiO&dJ$p5vJG$fDKTGg~ssI20 literal 0 HcmV?d00001 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; + } +}