Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions ATBackendChallenge.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
Binary file not shown.
Binary file added out/production/ATBackendChallenge/Main.class
Binary file not shown.
Binary file added out/production/ATBackendChallenge/Person.class
Binary file not shown.
102 changes: 102 additions & 0 deletions src/Calculations.java
Original file line number Diff line number Diff line change
@@ -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<BigInteger> sourceList)
{
if ((sourceList.isEmpty()))
{
System.out.println("List is empty!");
}
else
{
for(int i = 0; i<sourceList.size(); i++)
{
BigInteger x = sourceList.get(i);
System.out.print(x +" ");
}
}
System.out.println();
}
//A function that takes an int n and returns a list of all prime numbers between 0 and n
public ArrayList<BigInteger> primeNumbersToN(BigInteger n)
{
ArrayList<BigInteger> 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<BigInteger> nFactorials(BigInteger n)
{
ArrayList<BigInteger> 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;
}

}
52 changes: 52 additions & 0 deletions src/Main.java
Original file line number Diff line number Diff line change
@@ -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<BigInteger> listOfPrimes = calcs.primeNumbersToN(n);
calcs.displayArraylist(listOfPrimes);
}

public static void listFactorials(BigInteger f)
{
ArrayList<BigInteger> listOfFactorials = calcs.nFactorials(f);
calcs.displayArraylist(listOfFactorials);
}

}
28 changes: 28 additions & 0 deletions src/Person.java
Original file line number Diff line number Diff line change
@@ -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;
}
}