map = new HashMap<>();
+
public static void main(String[] args) {
// Methods all returning [0, 1, 1, 2, 3, 5, ...] for n = [0, 1, 2, 3, 4, 5, ...]
diff --git a/DynamicProgramming/RodCutting.java b/DynamicProgramming/RodCutting.java
index d57c69ce9..0913dfd80 100644
--- a/DynamicProgramming/RodCutting.java
+++ b/DynamicProgramming/RodCutting.java
@@ -26,7 +26,8 @@ public class RodCutting {
public static void main(String args[]) {
int[] arr = new int[]{2, 5, 13, 19, 20};
int size = arr.length;
+ int result = cutRod(arr,size);
System.out.println("Maximum Obtainable Value is " +
- cutRod(arr, size));
+ result);
}
}
diff --git a/Maths/FindMin.java b/Maths/FindMin.java
index 2a13fe4b5..9097c0179 100644
--- a/Maths/FindMin.java
+++ b/Maths/FindMin.java
@@ -9,7 +9,7 @@ public class FindMin {
}
/**
- * find min of array
+ * Find the minimum number of an array of numbers.
*
* @param array the array contains element
* @return min value
diff --git a/Maths/PalindromeNumber.java b/Maths/PalindromeNumber.java
new file mode 100644
index 000000000..2916c753e
--- /dev/null
+++ b/Maths/PalindromeNumber.java
@@ -0,0 +1,30 @@
+package Maths;
+
+public class PalindromeNumber {
+ public static void main(String[] args) {
+
+ assert isPalindrome(12321);
+ assert !isPalindrome(1234);
+ assert isPalindrome(1);
+ }
+
+ /**
+ * Check if {@code n} is palindrome number or not
+ *
+ * @param number the number
+ * @return {@code true} if {@code n} is palindrome number, otherwise {@code false}
+ */
+ public static boolean isPalindrome(int number) {
+ if (number < 0) {
+ throw new IllegalArgumentException(number + "");
+ }
+ int numberCopy = number;
+ int reverseNumber = 0;
+ while (numberCopy != 0) {
+ int remainder = numberCopy % 10;
+ reverseNumber = reverseNumber * 10 + remainder;
+ numberCopy /= 10;
+ }
+ return number == reverseNumber;
+ }
+}
diff --git a/Maths/ParseInteger.java b/Maths/ParseInteger.java
new file mode 100644
index 000000000..91177bb49
--- /dev/null
+++ b/Maths/ParseInteger.java
@@ -0,0 +1,33 @@
+package Maths;
+
+public class ParseInteger {
+ public static void main(String[] args) {
+ assert parseInt("123") == Integer.parseInt("123");
+ assert parseInt("-123") == Integer.parseInt("-123");
+ assert parseInt("0123") == Integer.parseInt("0123");
+ assert parseInt("+123") == Integer.parseInt("+123");
+ }
+
+ /**
+ * Parse a string to integer
+ *
+ * @param s the string
+ * @return the integer value represented by the argument in decimal.
+ * @throws NumberFormatException if the {@code string} does not contain a parsable integer.
+ */
+ public static int parseInt(String s) {
+ if (s == null) {
+ throw new NumberFormatException("null");
+ }
+ boolean isNegative = s.charAt(0) == '-';
+ boolean isPositive = s.charAt(0) == '+';
+ int number = 0;
+ for (int i = isNegative ? 1 : isPositive ? 1 : 0, length = s.length(); i < length; ++i) {
+ if (!Character.isDigit(s.charAt(i))) {
+ throw new NumberFormatException("s=" + s);
+ }
+ number = number * 10 + s.charAt(i) - '0';
+ }
+ return isNegative ? -number : number;
+ }
+}
diff --git a/Maths/PerfectNumber.java b/Maths/PerfectNumber.java
new file mode 100644
index 000000000..ceaf0b2bc
--- /dev/null
+++ b/Maths/PerfectNumber.java
@@ -0,0 +1,33 @@
+package Maths;
+
+/**
+ * In number theory, a perfect number is a positive integer that is equal to the sum of
+ * its positive divisors, excluding the number itself. For instance, 6 has divisors 1, 2 and 3
+ * (excluding itself), and 1 + 2 + 3 = 6, so 6 is a perfect number.
+ *
+ * link:https://en.wikipedia.org/wiki/Perfect_number
+ *
+ */
+public class PerfectNumber {
+ public static void main(String[] args) {
+ assert isPerfectNumber(6); /* 1 + 2 + 3 == 6 */
+ assert !isPerfectNumber(8); /* 1 + 2 + 4 != 8 */
+ assert isPerfectNumber(28); /* 1 + 2 + 4 + 7 + 14 == 28 */
+ }
+
+ /**
+ * Check if {@code number} is perfect number or not
+ *
+ * @param number the number
+ * @return {@code true} if {@code number} is perfect number, otherwise false
+ */
+ public static boolean isPerfectNumber(int number) {
+ int sum = 0; /* sum of its positive divisors */
+ for (int i = 1; i < number; ++i) {
+ if (number % i == 0) {
+ sum += i;
+ }
+ }
+ return sum == number;
+ }
+}
diff --git a/Maths/Pow.java b/Maths/Pow.java
new file mode 100644
index 000000000..605e01d98
--- /dev/null
+++ b/Maths/Pow.java
@@ -0,0 +1,26 @@
+package maths;
+
+public class Pow {
+ public static void main(String[] args) {
+ assert pow(2, 0) == Math.pow(2, 0);
+ assert pow(0, 2) == Math.pow(0, 2);
+ assert pow(2, 10) == Math.pow(2, 10);
+ assert pow(10, 2) == Math.pow(10, 2);
+ }
+
+ /**
+ * Returns the value of the first argument raised to the power of the
+ * second argument
+ *
+ * @param a the base.
+ * @param b the exponent.
+ * @return the value {@code a}{@code b}.
+ */
+ public static long pow(int a, int b) {
+ long result = 1;
+ for (int i = 1; i <= b; i++) {
+ result *= a;
+ }
+ return result;
+ }
+}
diff --git a/Maths/PowRecursion.java b/Maths/PowRecursion.java
new file mode 100644
index 000000000..243548708
--- /dev/null
+++ b/Maths/PowRecursion.java
@@ -0,0 +1,26 @@
+package Maths;
+
+public class PowRecursion {
+ public static void main(String[] args) {
+ assert pow(2, 0) == Math.pow(2, 0);
+ assert pow(0, 2) == Math.pow(0, 2);
+ assert pow(2, 10) == Math.pow(2, 10);
+ assert pow(10, 2) == Math.pow(10, 2);
+ }
+
+ /**
+ * Returns the value of the first argument raised to the power of the
+ * second argument
+ *
+ * @param a the base.
+ * @param b the exponent.
+ * @return the value {@code a}{@code b}.
+ */
+ public static long pow(int a, int b) {
+ if (b == 0) {
+ return 1;
+ } else {
+ return a * pow(a, b - 1);
+ }
+ }
+}
diff --git a/Others/CountWords.java b/Others/CountWords.java
index 5dc637559..f1caf9a87 100644
--- a/Others/CountWords.java
+++ b/Others/CountWords.java
@@ -44,7 +44,5 @@ public class CountWords {
}
s = sb.toString();
return s.trim().split("[\\s]+").length;
-
}
-
}