diff --git a/algebra/sum_of_first_n_primes/sum_of_first_n_primes.java b/algebra/sum_of_first_n_primes/sum_of_first_n_primes.java new file mode 100644 index 000000000..ccacaa9ca --- /dev/null +++ b/algebra/sum_of_first_n_primes/sum_of_first_n_primes.java @@ -0,0 +1,40 @@ +// Returns the sum of the fist N prime numbers as inputted by user. +// You can convert it into a method as well by + +import java.util.Scanner; +import java.util.stream.IntStream; + +public class sum_of_first_n_primes { + + public static void main(String[] args) { + Scanner scan = new Scanner(System.in); + int n = Integer.parseInt(scan.nextLine()); + int counter = 0; + int primeDividend = 2; + + int[] numList = new int[n]; + while (counter != n) { + + boolean prime = true; + + for (int i = 2; i <= Math.sqrt(primeDividend); i++) { + + if (primeDividend % i == 0) { + prime = false; + break; + } + + } + + if (prime) { + numList[counter] = primeDividend; + counter++; + } + primeDividend++; + } + + System.out.println("Sum of the first " + n + " primes " + IntStream.of(numList).sum()); + + } + +} diff --git a/discrete/ackermann_peter/java/ackermann_peter.java b/discrete/ackermann_peter/java/ackermann_peter.java new file mode 100644 index 000000000..9c594c6b1 --- /dev/null +++ b/discrete/ackermann_peter/java/ackermann_peter.java @@ -0,0 +1,23 @@ +// A java implementation of the Ackermann function using explicit recursion. + + +public class ackermann_peter { + + private static double M; + private static double N; + + public static void main(String[] args) { + if (args.length != 0) + M = Double.parseDouble(args[0]); + if ((args.length > 1)) + N = Double.parseDouble(args[1]); + System.out.println(ackermanCalculator(M, N)); + } + + public static double ackermanCalculator(double m, double n){ + if (m == 0) return n +1; + if (m == 0) return ackermanCalculator(m -1, 1); + return ackermanCalculator(m-1, ackermanCalculator(m, n-1)); + } + +} diff --git a/physics/acceleration_formula/java/AccelerationCalculator.java b/physics/acceleration_formula/java/AccelerationCalculator.java index 6d65ac57c..cfdef576e 100644 --- a/physics/acceleration_formula/java/AccelerationCalculator.java +++ b/physics/acceleration_formula/java/AccelerationCalculator.java @@ -10,7 +10,21 @@ public class AccelerationCalculator { * @param t - Time taken in seconds * @return acceleration in meter/second^2 */ - public static double calculateFor(double u, double v, double t) { + public static double calculateAcceleration(double u, double v, double t) { return (v - u) / t; } + + /** + * + * @param u - Initial Velocity in meter/second + * @param v - Final Velocity in meter/second + * @param s - Displacement in meters + * @return Acceleration in meter/second ^ 2 + */ + + public static double calculateAcceleration(double u, double v, float s) { + return ((Math.pow(u, 2) - Math.pow(v, 2)) / (2 * s)); // Derived from the u^2 = v^2 + 2ad formula. + } + + } \ No newline at end of file