Skip to content

Commit f32a63e

Browse files
Merge pull request TheAlgorithms#1416 from shellhub/dev
Added more algorithms to maths
2 parents 53d5cf0 + ada4b6c commit f32a63e

8 files changed

Lines changed: 217 additions & 21 deletions

File tree

DIRECTORY.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,21 +118,27 @@
118118
* [AmicableNumber](https://github.com/TheAlgorithms/Java/blob/master/Maths/AmicableNumber.java)
119119
* [Area](https://github.com/TheAlgorithms/Java/blob/master/Maths/Area.java)
120120
* [Average](https://github.com/TheAlgorithms/Java/blob/master/Maths/Average.java)
121+
* [Ceil](https://github.com/TheAlgorithms/Java/blob/master/Maths/Ceil.java)
122+
* [Combinations](https://github.com/TheAlgorithms/Java/blob/master/Maths/Combinations.java)
121123
* [Factorial](https://github.com/TheAlgorithms/Java/blob/master/Maths/Factorial.java)
122124
* [FactorialRecursion](https://github.com/TheAlgorithms/Java/blob/master/Maths/FactorialRecursion.java)
123125
* [FibonacciNumber](https://github.com/TheAlgorithms/Java/blob/master/Maths/FibonacciNumber.java)
124126
* [FindMax](https://github.com/TheAlgorithms/Java/blob/master/Maths/FindMax.java)
125127
* [FindMaxRecursion](https://github.com/TheAlgorithms/Java/blob/master/Maths/FindMaxRecursion.java)
126128
* [FindMin](https://github.com/TheAlgorithms/Java/blob/master/Maths/FindMin.java)
127129
* [FindMinRecursion](https://github.com/TheAlgorithms/Java/blob/master/Maths/FindMinRecursion.java)
130+
* [Floor](https://github.com/TheAlgorithms/Java/blob/master/Maths/Floor.java)
128131
* [GCD](https://github.com/TheAlgorithms/Java/blob/master/Maths/GCD.java)
129132
* [GCDRecursion](https://github.com/TheAlgorithms/Java/blob/master/Maths/GCDRecursion.java)
133+
* [LucasSeries](https://github.com/TheAlgorithms/Java/blob/master/Maths/LucasSeries.java)
130134
* [MaxValue](https://github.com/TheAlgorithms/Java/blob/master/Maths/MaxValue.java)
131135
* [MinValue](https://github.com/TheAlgorithms/Java/blob/master/Maths/MinValue.java)
132136
* [NumberOfDigits](https://github.com/TheAlgorithms/Java/blob/master/Maths/NumberOfDigits.java)
133137
* [PalindromeNumber](https://github.com/TheAlgorithms/Java/blob/master/Maths/PalindromeNumber.java)
134138
* [ParseInteger](https://github.com/TheAlgorithms/Java/blob/master/Maths/ParseInteger.java)
139+
* [PerfectCube](https://github.com/TheAlgorithms/Java/blob/master/Maths/PerfectCube.java)
135140
* [PerfectNumber](https://github.com/TheAlgorithms/Java/blob/master/Maths/PerfectNumber.java)
141+
* [PerfectSquare](https://github.com/TheAlgorithms/Java/blob/master/Maths/PerfectSquare.java)
136142
* [Pow](https://github.com/TheAlgorithms/Java/blob/master/Maths/Pow.java)
137143
* [PowRecursion](https://github.com/TheAlgorithms/Java/blob/master/Maths/PowRecursion.java)
138144
* [PrimeCheck](https://github.com/TheAlgorithms/Java/blob/master/Maths/PrimeCheck.java)

Maths/Ceil.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package Maths;
2+
3+
public class Ceil {
4+
public static void main(String[] args) {
5+
assert ceil(10) == Math.ceil(10);
6+
assert ceil(-10) == Math.ceil(-10);
7+
assert ceil(10.0) == Math.ceil(10.0);
8+
assert ceil(-10.0) == Math.ceil(-10.0);
9+
assert ceil(10.1) == Math.ceil(10.1);
10+
assert ceil(-10.1) == Math.ceil(-10.1);
11+
assert ceil(0) == Math.ceil(0);
12+
assert ceil(-0) == Math.ceil(-0);
13+
assert ceil(0.0) == Math.ceil(0.0);
14+
assert ceil(-0.0) == Math.ceil(-0.0);
15+
}
16+
17+
/**
18+
* Returns the smallest (closest to negative infinity)
19+
*
20+
* @param number the number
21+
* @return the smallest (closest to negative infinity) of given {@code number}
22+
*/
23+
public static double ceil(double number) {
24+
if (number - (int) number == 0) {
25+
return number;
26+
} else if (number - (int) number > 0) {
27+
return (int) (number + 1);
28+
} else {
29+
return (int) number;
30+
}
31+
}
32+
}

Maths/Combinations.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package Maths;
2+
3+
/**
4+
* @see <a href="https://en.wikipedia.org/wiki/Combination">Combination</a>
5+
*/
6+
public class Combinations {
7+
public static void main(String[] args) {
8+
assert combinations(1, 1) == 1;
9+
assert combinations(10, 5) == 252;
10+
assert combinations(6, 3) == 20;
11+
assert combinations(20, 5) == 15504;
12+
}
13+
14+
/**
15+
* Calculate of factorial
16+
*
17+
* @param n the number
18+
* @return factorial of given number
19+
*/
20+
public static long factorial(int n) {
21+
if (n < 0) {
22+
throw new IllegalArgumentException("number is negative");
23+
}
24+
return n == 0 || n == 1 ? 1 : n * factorial(n - 1);
25+
}
26+
27+
/**
28+
* Calculate combinations
29+
*
30+
* @param n first number
31+
* @param k second number
32+
* @return combinations of given {@code n} and {@code k}
33+
*/
34+
public static long combinations(int n, int k) {
35+
return factorial(n) / (factorial(k) * factorial(n - k));
36+
}
37+
}

Maths/Factorial.java

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,27 @@
11
package Maths;
2-
import java.util.*; //for importing scanner
32

43
public class Factorial {
5-
public static void main(String[] args) { //main method
6-
int n = 1;
7-
Scanner sc= new Scanner(System.in);
8-
System.out.println("Enter Number");
9-
n=sc.nextInt();
10-
System.out.println(n + "! = " + factorial(n));
11-
}
124

13-
//Factorial = n! = n1 * (n-1) * (n-2)*...1
5+
/* Driver Code */
6+
public static void main(String[] args) {
7+
assert factorial(0) == 1;
8+
assert factorial(1) == 1;
9+
assert factorial(5) == 120;
10+
assert factorial(10) == 3628800;
11+
}
1412

1513
/**
16-
* Calculate factorial N
14+
* Calculate factorial N using iteration
1715
*
1816
* @param n the number
1917
* @return the factorial of {@code n}
2018
*/
2119
public static long factorial(int n) {
22-
// Using recursion
23-
try {
24-
if (n == 0) {
25-
return 1; // if n = 0, return factorial of n;
26-
}else {
27-
return n*factorial(n-1); // While N is greater than 0, call the function again, passing n-1 (Principle of factoring);
28-
}
29-
}catch (ArithmeticException e) {
30-
System.out.println("Dont work with less than 0");
31-
}
32-
return n;
20+
if (n < 0) {
21+
throw new IllegalArgumentException("number is negative");
22+
}
23+
long factorial = 1;
24+
for (int i = 1; i <= n; factorial *= i, ++i) ;
25+
return factorial;
3326
}
3427
}

Maths/Floor.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package Maths;
2+
3+
public class Floor {
4+
public static void main(String[] args) {
5+
assert floor(10) == Math.floor(10);
6+
assert floor(-10) == Math.floor(-10);
7+
assert floor(10.0) == Math.floor(10.0);
8+
assert floor(-10.0) == Math.floor(-10.0);
9+
assert floor(10.1) == Math.floor(10.1);
10+
assert floor(-10.1) == Math.floor(-10.1);
11+
assert floor(0) == Math.floor(0);
12+
assert floor(-0) == Math.floor(-0);
13+
assert floor(0.0) == Math.floor(0.0);
14+
assert floor(-0.0) == Math.floor(-0.0);
15+
}
16+
17+
/**
18+
* Returns the largest (closest to positive infinity)
19+
*
20+
* @param number the number
21+
* @return the largest (closest to positive infinity) of given {@code number}
22+
*/
23+
public static double floor(double number) {
24+
if (number - (int) number == 0) {
25+
return number;
26+
} else if (number - (int) number > 0) {
27+
return (int) number;
28+
} else {
29+
return (int) number - 1;
30+
}
31+
}
32+
}

Maths/LucasSeries.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package Maths;
2+
3+
/**
4+
* https://en.wikipedia.org/wiki/Lucas_number
5+
*/
6+
public class LucasSeries {
7+
public static void main(String[] args) {
8+
assert lucasSeries(1) == 2 && lucasSeriesIteration(1) == 2;
9+
assert lucasSeries(2) == 1 && lucasSeriesIteration(2) == 1;
10+
assert lucasSeries(3) == 3 && lucasSeriesIteration(3) == 3;
11+
assert lucasSeries(4) == 4 && lucasSeriesIteration(4) == 4;
12+
assert lucasSeries(5) == 7 && lucasSeriesIteration(5) == 7;
13+
assert lucasSeries(6) == 11 && lucasSeriesIteration(6) == 11;
14+
assert lucasSeries(11) == 123 && lucasSeriesIteration(11) == 123;
15+
16+
}
17+
18+
/**
19+
* Calculate nth number of lucas series(2, 1, 3, 4, 7, 11, 18, 29, 47, 76, 123, ....) using recursion
20+
*
21+
* @param n nth
22+
* @return nth number of lucas series
23+
*/
24+
public static int lucasSeries(int n) {
25+
return n == 1 ? 2 : n == 2 ? 1 : lucasSeries(n - 1) + lucasSeries(n - 2);
26+
}
27+
28+
/**
29+
* Calculate nth number of lucas series(2, 1, 3, 4, 7, 11, 18, 29, 47, 76, 123, ....) using iteration
30+
*
31+
* @param n nth
32+
* @return nth number of lucas series
33+
*/
34+
public static int lucasSeriesIteration(int n) {
35+
int previous = 2;
36+
int current = 1;
37+
for (int i = 1; i < n; i++) {
38+
int next = previous + current;
39+
previous = current;
40+
current = next;
41+
}
42+
return previous;
43+
}
44+
}

Maths/PerfectCube.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package Maths;
2+
3+
/**
4+
* https://en.wikipedia.org/wiki/Cube_(algebra)
5+
*/
6+
public class PerfectCube {
7+
public static void main(String[] args) {
8+
assert !isPerfectCube(-1);
9+
assert isPerfectCube(0);
10+
assert isPerfectCube(1);
11+
assert !isPerfectCube(4);
12+
assert isPerfectCube(8);
13+
assert isPerfectCube(27);
14+
15+
}
16+
17+
/**
18+
* Check if a number is perfect cube or not
19+
*
20+
* @param number number to check
21+
* @return {@code true} if {@code number} is perfect cube, otherwise {@code false}
22+
*/
23+
public static boolean isPerfectCube(int number) {
24+
int a = (int) Math.pow(number, 1.0 / 3);
25+
return a * a * a == number;
26+
}
27+
}

Maths/PerfectSquare.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package Maths;
2+
3+
/**
4+
* https://en.wikipedia.org/wiki/Perfect_square
5+
*/
6+
public class PerfectSquare {
7+
public static void main(String[] args) {
8+
assert !isPerfectSquare(-1);
9+
assert !isPerfectSquare(3);
10+
assert !isPerfectSquare(5);
11+
assert isPerfectSquare(9);
12+
assert isPerfectSquare(100);
13+
}
14+
15+
/**
16+
* Check if a number is perfect square number
17+
*
18+
* @param number the number to be checked
19+
* @return <tt>true</tt> if {@code number} is perfect square, otherwise <tt>false</tt>
20+
*/
21+
public static boolean isPerfectSquare(int number) {
22+
int sqrt = (int) Math.sqrt(number);
23+
return sqrt * sqrt == number;
24+
}
25+
}

0 commit comments

Comments
 (0)