File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 11package Maths ;
2- import java .util .*; //for importing scanner
32
43public 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}
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments