From 58eb2ec581fe03afdbf27d480cbe6a4bcc2e364f Mon Sep 17 00:00:00 2001 From: shellhub Date: Fri, 11 Oct 2019 16:17:38 +0800 Subject: [PATCH 01/14] PalindromeNumber --- Maths/PalindromeNumber.java | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 Maths/PalindromeNumber.java 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; + } +} From 58b9f0bb0cf4827eec13ca2129f8936497edc7e3 Mon Sep 17 00:00:00 2001 From: shellhub Date: Fri, 11 Oct 2019 16:44:33 +0800 Subject: [PATCH 02/14] perfect number --- Maths/PerfectNumber.java | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 Maths/PerfectNumber.java 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; + } +} From 51f36068a1cbf868d57e528ea6c447602343cb51 Mon Sep 17 00:00:00 2001 From: salonilakhotia Date: Sat, 12 Oct 2019 10:14:25 +0530 Subject: [PATCH 03/14] Result print --- DynamicProgramming/RodCutting.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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); } } From f5752234b45bcce2ce713d2d213c1fb59013b2fb Mon Sep 17 00:00:00 2001 From: salonilakhotia Date: Sat, 12 Oct 2019 10:25:40 +0530 Subject: [PATCH 04/14] Size --- DynamicProgramming/Fibonacci.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/DynamicProgramming/Fibonacci.java b/DynamicProgramming/Fibonacci.java index 35af98add..d1a619f71 100644 --- a/DynamicProgramming/Fibonacci.java +++ b/DynamicProgramming/Fibonacci.java @@ -16,11 +16,11 @@ public class Fibonacci { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); - int n = Integer.parseInt(br.readLine()); + int size = Integer.parseInt(br.readLine()); // Methods all returning [0, 1, 1, 2, 3, 5, ...] for n = [0, 1, 2, 3, 4, 5, ...] - System.out.println(fibMemo(n)); - System.out.println(fibBotUp(n)); + System.out.println(fibMemo(size)); + System.out.println(fibBotUp(size)); } /** From 8ae6b4e64b587081dc4c14ddba2203d54c444ccf Mon Sep 17 00:00:00 2001 From: shellhub Date: Sat, 12 Oct 2019 20:48:38 +0800 Subject: [PATCH 05/14] update BalancedBrackets --- DataStructures/Stacks/BalancedBrackets.java | 102 ++++++++++---------- 1 file changed, 49 insertions(+), 53 deletions(-) diff --git a/DataStructures/Stacks/BalancedBrackets.java b/DataStructures/Stacks/BalancedBrackets.java index 8063c5105..ea342219c 100644 --- a/DataStructures/Stacks/BalancedBrackets.java +++ b/DataStructures/Stacks/BalancedBrackets.java @@ -1,10 +1,8 @@ -package data_structures.Stacks; +package DataStructures.Stacks; -import java.util.Scanner; import java.util.Stack; /** - * * The nested brackets problem is a problem that determines if a sequence of * brackets are properly nested. A sequence of brackets s is considered properly * nested if any of the following conditions are true: - s is empty - s has the @@ -15,71 +13,69 @@ import java.util.Stack; * returns true if S is nested and false otherwise. * * @author akshay sharma - * @date: 2017-10-17 * @author khalil2535 - * + * @author shellhub */ class BalancedBrackets { /** + * Check if {@code leftBracket} and {@code rightBracket} is paired or not * - * @param s - * @return + * @param leftBracket left bracket + * @param rightBracket right bracket + * @return {@code true} if {@code leftBracket} and {@code rightBracket} is paired, + * otherwise {@code false} */ - static boolean is_balanced(String s) { - Stack bracketsStack = new Stack<>(); - char[] text = s.toCharArray(); - for (char x : text) { - switch (x) { - case '{': - case '<': - case '(': - case '[': - bracketsStack.push(x); - break; - case '}': - if (!bracketsStack.empty() && bracketsStack.pop() == '{') { - break; - } else { - return false; - } - case '>': - if (!bracketsStack.empty() && bracketsStack.pop() == '<') { - break; - } else { - return false; - } - case ')': - if (!bracketsStack.empty() && bracketsStack.pop() == '(') { - break; - } else { - return false; - } - case ']': - if (!bracketsStack.empty() && bracketsStack.pop() == '[') { - break; - } else { - return false; - } + public static boolean isPaired(char leftBracket, char rightBracket) { + char[][] pairedBrackets = { + {'(', ')'}, + {'[', ']'}, + {'{', '}'}, + {'<', '>'} + }; + for (char[] pairedBracket : pairedBrackets) { + if (pairedBracket[0] == leftBracket && pairedBracket[1] == rightBracket) { + return true; } } - return bracketsStack.empty(); + return false; } /** + * Check if {@code brackets} is balanced * - * @param args - * @TODO remove main method and Test using JUnit or other methodology + * @param brackets the brackets + * @return {@code true} if {@code brackets} is balanced, otherwise {@code false} */ - public static void main(String args[]) { - try (Scanner in = new Scanner(System.in)) { - System.out.println("Enter sequence of brackets: "); - String s = in.nextLine(); - if (is_balanced(s)) { - System.out.println(s + " is balanced"); - } else { - System.out.println(s + " ain't balanced"); + public static boolean isBalanced(String brackets) { + if (brackets == null) { + throw new IllegalArgumentException("brackets is null"); + } + Stack bracketsStack = new Stack<>(); + for (char bracket : brackets.toCharArray()) { + switch (bracket) { + case '(': + case '[': + case '{': + bracketsStack.push(bracket); + break; + case ')': + case ']': + case '}': + if (!(!bracketsStack.isEmpty() && isPaired(bracketsStack.pop(), bracket))) { + return false; + } + break; + default: /* other character is invalid */ + return false; } } + return bracketsStack.isEmpty(); + } + + + public static void main(String[] args) { + assert isBalanced("[()]{}{[()()]()}"); + assert !isBalanced("[(])"); } } From 14d67ffdf45745b69a05dd6cc356038e3df1745e Mon Sep 17 00:00:00 2001 From: shellhub Date: Tue, 15 Oct 2019 22:18:12 +0800 Subject: [PATCH 06/14] clear list --- DataStructures/Lists/SinglyLinkedList.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/DataStructures/Lists/SinglyLinkedList.java b/DataStructures/Lists/SinglyLinkedList.java index 8747aebb6..1b442e523 100644 --- a/DataStructures/Lists/SinglyLinkedList.java +++ b/DataStructures/Lists/SinglyLinkedList.java @@ -106,6 +106,25 @@ public class SinglyLinkedList { } } + /** + * clear all nodes in list + */ + public void clear() { + if (size == 0) { + return; + } + Node prev = head.next; + Node cur = prev.next; + while (cur != null) { + prev = null; // clear to let GC do its work + prev = cur; + cur = cur.next; + } + prev = null; + head.next = null; + size = 0; + } + /** * Checks if the list is empty * From a5997e8d7b8aa7a39802bcea0e783540a7ac747a Mon Sep 17 00:00:00 2001 From: shellhub Date: Wed, 16 Oct 2019 22:27:07 +0800 Subject: [PATCH 07/14] update SinglyLinkedList --- DataStructures/Lists/SinglyLinkedList.java | 90 ++++++++++++---------- 1 file changed, 51 insertions(+), 39 deletions(-) diff --git a/DataStructures/Lists/SinglyLinkedList.java b/DataStructures/Lists/SinglyLinkedList.java index 1b442e523..a478d7a5f 100644 --- a/DataStructures/Lists/SinglyLinkedList.java +++ b/DataStructures/Lists/SinglyLinkedList.java @@ -54,20 +54,16 @@ public class SinglyLinkedList { * @param data data to be stored in a new node * @param position position at which a new node is to be inserted */ - public void insertNth(int data, int position) { - if (position < 0 || position > getSize()) { - throw new IndexOutOfBoundsException("position less than zero or position more than the count of list"); - } else { - Node cur = head; - Node node = new Node(data); - for (int i = 0; i < position; ++i) { - cur = cur.next; - } - node.next = cur.next; - cur.next = node; - size++; + checkBounds(position, 0, size); + Node cur = head; + for (int i = 0; i < position; ++i) { + cur = cur.next; } + Node node = new Node(data); + node.next = cur.next; + cur.next = node; + size++; } /** @@ -90,19 +86,28 @@ public class SinglyLinkedList { * This method deletes an element at Nth position */ public void deleteNth(int position) { - if (position < 0 || position > size - 1) { - throw new IndexOutOfBoundsException("position less than zero or position more than the count of list"); - } else { - Node cur = head; - for (int i = 0; i < position; ++i) { - cur = cur.next; - } + checkBounds(position, 0, size - 1); + Node cur = head; + for (int i = 0; i < position; ++i) { + cur = cur.next; + } - Node destroy = cur.next; - cur.next = cur.next.next; - destroy = null; // clear to let GC do its work + Node destroy = cur.next; + cur.next = cur.next.next; + destroy = null; // clear to let GC do its work - size--; + size--; + } + + /** + * @param position to check position + * @param low low index + * @param high high index + * @throws IndexOutOfBoundsException if {@code position} not in range {@code low} to {@code high} + */ + public void checkBounds(int position, int low, int high) { + if (position < low || position > high) { + throw new IndexOutOfBoundsException(position + ""); } } @@ -134,18 +139,6 @@ public class SinglyLinkedList { return size == 0; } - /** - * Prints contents of the list - */ - public void display() { - Node current = head.next; - while (current != null) { - System.out.print(current.value + " "); - current = current.next; - } - System.out.println(); - } - /** * Returns the size of the linked list */ @@ -153,6 +146,20 @@ public class SinglyLinkedList { return size; } + @Override + public String toString() { + if (size == 0) { + return ""; + } + StringBuilder builder = new StringBuilder(); + Node cur = head.next; + while (cur != null) { + builder.append(cur.value).append("->"); + cur = cur.next; + } + return builder.replace(builder.length() - 2, builder.length(), "").toString(); + } + /** * Main method * @@ -167,19 +174,24 @@ public class SinglyLinkedList { myList.insertHead(7); myList.insertHead(10); - myList.display(); // 10 -> 7 -> 5 + System.out.println(myList);; // 10 -> 7 -> 5 myList.deleteHead(); - myList.display(); // 7 -> 5 + System.out.println(myList);; // 7 -> 5 myList.insertNth(11, 2); - myList.display(); // 7 -> 5 -> 11 + System.out.println(myList);; // 7 -> 5 -> 11 myList.deleteNth(1); - myList.display(); // 7-> 11 + System.out.println(myList);; // 7-> 11 + + myList.clear(); + assert myList.isEmpty(); + + System.out.println(myList); // "" } } From bb9e082aa9118460a501525d80206bcb09e04dbb Mon Sep 17 00:00:00 2001 From: shellhub Date: Fri, 18 Oct 2019 17:39:32 +0800 Subject: [PATCH 08/14] update StackOfLinkedList --- DataStructures/Stacks/StackOfLinkedList.java | 118 ++++++++++++------- 1 file changed, 74 insertions(+), 44 deletions(-) diff --git a/DataStructures/Stacks/StackOfLinkedList.java b/DataStructures/Stacks/StackOfLinkedList.java index 093442112..5a2b8fa08 100644 --- a/DataStructures/Stacks/StackOfLinkedList.java +++ b/DataStructures/Stacks/StackOfLinkedList.java @@ -17,12 +17,12 @@ class StackOfLinkedList { stack.push(4); stack.push(5); - stack.printStack(); + System.out.println(stack); System.out.println("Size of stack currently is: " + stack.getSize()); - stack.pop(); - stack.pop(); + assert stack.pop() == 5; + assert stack.pop() == 4; System.out.println("Top element of stack currently is: " + stack.peek()); } @@ -48,65 +48,95 @@ class Node { class LinkedListStack { - Node head = null; + /** + * Top of stack + */ + Node head; - public void push(int x) { - Node n = new Node(x); - if (head == null) { - head = n; - } else { - Node temp = head; - n.next = temp; - head = n; - } + /** + * Size of stack + */ + private int size; + + /** + * Init properties + */ + public LinkedListStack() { + head = null; + size = 0; } - public void pop() { - if (head == null) { - System.out.println("Empty stack. Nothing to pop"); - } + /** + * Add element at top + * + * @param x to be added + * @return true if add successfully + */ + public boolean push(int x) { + Node newNode = new Node(x); + newNode.next = head; + head = newNode; + size++; + return true; + } - Node temp = head; + /** + * Pop element at top of stack + * + * @return element at top of stack + * @throws NoSuchElementException if stack is empty + */ + public int pop() { + if (size == 0) { + throw new NoSuchElementException("Empty stack. Nothing to pop"); + } + Node destroy = head; head = head.next; - System.out.println("Popped element is: " + temp.data); + int retValue = destroy.data; + destroy = null; // clear to let GC do it's work + size--; + return retValue; } + /** + * Peek element at top of stack + * + * @return element at top of stack + * @throws NoSuchElementException if stack is empty + */ public int peek() { - if (head == null) { - return -1; + if (size == 0) { + throw new NoSuchElementException("Empty stack. Nothing to pop"); } return head.data; } - public void printStack() { - Node temp = head; - System.out.println("Stack is printed as below: "); - while (temp != null) { - if (temp.next == null) { - System.out.print(temp.data); - } else { - System.out.print(temp.data + " -> "); - } - temp = temp.next; + @Override + public String toString() { + Node cur = head; + StringBuilder builder = new StringBuilder(); + while (cur != null) { + builder.append(cur.data).append("->"); + cur = cur.next; } - System.out.println(); + return builder.replace(builder.length() - 2, builder.length(), "").toString(); } + /** + * Check if stack is empty + * + * @return true if stack is empty, otherwise false + */ public boolean isEmpty() { - return head == null; + return size == 0; } + /** + * Return size of stack + * + * @return size of stack + */ public int getSize() { - if (head == null) - return 0; - else { - int size = 1; - Node temp = head; - while (temp.next != null) { - temp = temp.next; - size++; - } - return size; - } + return size; } } From ffb3195b38132c7e87c6e53339b396af06d3dbbc Mon Sep 17 00:00:00 2001 From: shellhub Date: Sat, 19 Oct 2019 22:51:59 +0800 Subject: [PATCH 09/14] DecimalToAnyUsingStack --- .../Stacks/DecimalToAnyUsingStack.java | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 DataStructures/Stacks/DecimalToAnyUsingStack.java diff --git a/DataStructures/Stacks/DecimalToAnyUsingStack.java b/DataStructures/Stacks/DecimalToAnyUsingStack.java new file mode 100644 index 000000000..6e129c32c --- /dev/null +++ b/DataStructures/Stacks/DecimalToAnyUsingStack.java @@ -0,0 +1,44 @@ +package DataStructures.Stacks; + +import java.util.Stack; + +public class DecimalToAnyUsingStack { + public static void main(String[] args) { + assert convert(0, 2).equals("0"); + assert convert(30, 2).equals("11110"); + assert convert(30, 8).equals("36"); + assert convert(30, 10).equals("30"); + assert convert(30, 16).equals("1E"); + } + + /** + * Convert decimal number to another radix + * + * @param number the number to be converted + * @param radix the radix + * @return another radix + * @throws ArithmeticException if number or radius is invalid + */ + private static String convert(int number, int radix) { + if (radix < 2 || radix > 16) { + throw new ArithmeticException( + String.format("Invalid input -> number:%d,radius:%d", number, radix)); + } + char[] tables = { + '0', '1', '2', '3', '4', + '5', '6', '7', '8', '9', + 'A', 'B', 'C', 'D', 'E', 'F' + }; + Stack bits = new Stack<>(); + do { + bits.push(tables[number % radix]); + number = number / radix; + } while (number != 0); + + StringBuilder result = new StringBuilder(); + while (!bits.isEmpty()) { + result.append(bits.pop()); + } + return result.toString(); + } +} From f1ad7a1bbfe1be870d1b8e2d6eaf28297369543a Mon Sep 17 00:00:00 2001 From: shellhub Date: Mon, 21 Oct 2019 21:57:10 +0800 Subject: [PATCH 10/14] parseInteger --- Maths/ParseInteger.java | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 Maths/ParseInteger.java 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; + } +} From 56e887213bffa683cd89501c6726c6135b09b2cd Mon Sep 17 00:00:00 2001 From: shellhub Date: Wed, 23 Oct 2019 21:42:04 +0800 Subject: [PATCH 11/14] add pow --- Maths/Pow.java | 26 ++++++++++++++++++++++++++ Maths/PowRecursion.java | 26 ++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 Maths/Pow.java create mode 100644 Maths/PowRecursion.java 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..673b8fdee --- /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) { + int result = 1; + for (int i = 1; i <= b; i++) { + result *= a; + } + return result; + } +} From 6252b4eff8d0ff4e73db2f40a753ef75fb70756a Mon Sep 17 00:00:00 2001 From: UntouchedOdin0 Date: Wed, 23 Oct 2019 21:22:14 +0100 Subject: [PATCH 12/14] Update FindMin.java Re-word the comments --- Maths/FindMin.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 From 3a8ab829a8ac2171f492ecf05a3b4ccd05423cfb Mon Sep 17 00:00:00 2001 From: UntouchedOdin0 Date: Wed, 23 Oct 2019 21:30:19 +0100 Subject: [PATCH 13/14] Update CountWords.java Cleaning the code --- Others/CountWords.java | 2 -- 1 file changed, 2 deletions(-) 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; - } - } From d11b7347b650c225e31b1d46ec0f8800a8ce1fd9 Mon Sep 17 00:00:00 2001 From: shellhub Date: Thu, 24 Oct 2019 11:54:37 +0800 Subject: [PATCH 14/14] using recursion --- Maths/PowRecursion.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Maths/PowRecursion.java b/Maths/PowRecursion.java index 673b8fdee..243548708 100644 --- a/Maths/PowRecursion.java +++ b/Maths/PowRecursion.java @@ -17,10 +17,10 @@ public class PowRecursion { * @return the value {@code a}{@code b}. */ public static long pow(int a, int b) { - int result = 1; - for (int i = 1; i <= b; i++) { - result *= a; + if (b == 0) { + return 1; + } else { + return a * pow(a, b - 1); } - return result; } }