Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a class that returns the sum of a specific number of primes. #522

Closed
wants to merge 3 commits into from
Closed
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
40 changes: 40 additions & 0 deletions algebra/sum_of_first_n_primes/sum_of_first_n_primes.java
Original file line number Diff line number Diff line change
@@ -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());

}

}
23 changes: 23 additions & 0 deletions discrete/ackermann_peter/java/ackermann_peter.java
Original file line number Diff line number Diff line change
@@ -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));
}

}
16 changes: 15 additions & 1 deletion physics/acceleration_formula/java/AccelerationCalculator.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
}


}