From 630477c0cee52347c6871778624a740a2adaad7f Mon Sep 17 00:00:00 2001 From: Mcbencrafter <92606492+Mcbencrafter@users.noreply.github.com> Date: Sat, 11 Jan 2025 12:05:49 +0100 Subject: [PATCH 1/9] capataliezed titles of date/time snippets --- snippets/java/date-time/date-time-formatting-american.md | 2 +- snippets/java/date-time/date-time-formatting-european.md | 2 +- .../java/date-time/duration-formatting-hours-minutes-seconds.md | 2 +- snippets/java/date-time/duration-formatting-minutes-seconds.md | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/snippets/java/date-time/date-time-formatting-american.md b/snippets/java/date-time/date-time-formatting-american.md index c2bf6f93..569d5457 100644 --- a/snippets/java/date-time/date-time-formatting-american.md +++ b/snippets/java/date-time/date-time-formatting-american.md @@ -1,5 +1,5 @@ --- -title: Date time formatting american +title: Date Time Formatting American description: Formats a timestamp to a human-readable date-time string in the format "MM/dd/yyyy hh:mm:ss a" author: Mcbencrafter tags: date,time,date-time,formatting,american diff --git a/snippets/java/date-time/date-time-formatting-european.md b/snippets/java/date-time/date-time-formatting-european.md index 7c78eba2..5c966b6e 100644 --- a/snippets/java/date-time/date-time-formatting-european.md +++ b/snippets/java/date-time/date-time-formatting-european.md @@ -1,5 +1,5 @@ --- -title: Date time formatting european +title: Date Time Formatting European description: Formats a timestamp to a human-readable date-time string in the format "dd.MM.yyyy HH:mm:ss" author: Mcbencrafter tags: date,time,date-time,formatting,european diff --git a/snippets/java/date-time/duration-formatting-hours-minutes-seconds.md b/snippets/java/date-time/duration-formatting-hours-minutes-seconds.md index 718fd39b..76660b9f 100644 --- a/snippets/java/date-time/duration-formatting-hours-minutes-seconds.md +++ b/snippets/java/date-time/duration-formatting-hours-minutes-seconds.md @@ -1,5 +1,5 @@ --- -title: Duration formatting hours minutes seconds +title: Duration Formatting Hours Minutes Seconds description: Converts a given time duration to a human-readable string in the format "hh:mm(:ss)" author: Mcbencrafter tags: time,formatting,hours,minutes,seconds diff --git a/snippets/java/date-time/duration-formatting-minutes-seconds.md b/snippets/java/date-time/duration-formatting-minutes-seconds.md index 49107f58..19175d19 100644 --- a/snippets/java/date-time/duration-formatting-minutes-seconds.md +++ b/snippets/java/date-time/duration-formatting-minutes-seconds.md @@ -1,5 +1,5 @@ --- -title: Duration formatting minutes seconds +title: Duration Formatting Minutes Seconds description: Converts a given time duration to a human-readable string in the format "mm:ss" author: Mcbencrafter tags: time,formatting,minutes,seconds From 4cabb19e5e702b98dfab9ff429e0fbce1e99975f Mon Sep 17 00:00:00 2001 From: Mcbencrafter <92606492+Mcbencrafter@users.noreply.github.com> Date: Sat, 11 Jan 2025 12:07:00 +0100 Subject: [PATCH 2/9] added bit manipulation snippets --- .../java/bit-manipulation/bit-counting.md | 23 +++++++++++++++++++ .../java/bit-manipulation/is-power-of-two.md | 16 +++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 snippets/java/bit-manipulation/bit-counting.md create mode 100644 snippets/java/bit-manipulation/is-power-of-two.md diff --git a/snippets/java/bit-manipulation/bit-counting.md b/snippets/java/bit-manipulation/bit-counting.md new file mode 100644 index 00000000..66f9313a --- /dev/null +++ b/snippets/java/bit-manipulation/bit-counting.md @@ -0,0 +1,23 @@ +--- +title: Bit Counting +description: Counts the bits in an integer +author: Mcbencrafter +tags: math,number,bits,bit-counting +--- + +```java +public static int countBits(int number) { + int bits = 0; + + while (number > 0) { + bits += number & 1; + number >>= 1; + } + + return bits; +} + +// Usage: +int number = 5; +System.out.println(countBits(5)); // 2 (101) +``` \ No newline at end of file diff --git a/snippets/java/bit-manipulation/is-power-of-two.md b/snippets/java/bit-manipulation/is-power-of-two.md new file mode 100644 index 00000000..667cc201 --- /dev/null +++ b/snippets/java/bit-manipulation/is-power-of-two.md @@ -0,0 +1,16 @@ +--- +title: Is Power Of Two +description: Checks if a number is a power of two +author: Mcbencrafter +tags: math,number,bit,power-of-two +--- + +```java +public static boolean isPowerOfTwo(int number) { + return (number > 0) && ((number & (number - 1)) == 0); +} + +// Usage: +int number = 16; +System.out.println(isPowerOfTwo(5)); // true (2^4) +``` \ No newline at end of file From 0cace22fdc41fb123b37ab2261c22053775455c5 Mon Sep 17 00:00:00 2001 From: Mcbencrafter <92606492+Mcbencrafter@users.noreply.github.com> Date: Sat, 11 Jan 2025 12:07:28 +0100 Subject: [PATCH 3/9] added gcd and lcm snippets --- snippets/java/math/greatest-common-divisor.md | 23 ++++++++++++++++ snippets/java/math/least-common-multiple.md | 26 +++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 snippets/java/math/greatest-common-divisor.md create mode 100644 snippets/java/math/least-common-multiple.md diff --git a/snippets/java/math/greatest-common-divisor.md b/snippets/java/math/greatest-common-divisor.md new file mode 100644 index 00000000..dc3acdb7 --- /dev/null +++ b/snippets/java/math/greatest-common-divisor.md @@ -0,0 +1,23 @@ +--- +title: Greatest Common Divisor +description: Calculates the greatest common divisor (gcd) of two numbers +author: Mcbencrafter +tags: math,number,greatest-common-devisor,gcd,euclidean-algorithm +--- + +```java +public static int gcd(int number1, int number2) { + while (number2 != 0) { + int remainder = number2; + number2 = number1 % number2; + number1 = remainder; + } + + return number1; +} + +// Usage: +int a = 16; +int b = 12; +System.out.println(gcd(a, b)); // 4 +``` \ No newline at end of file diff --git a/snippets/java/math/least-common-multiple.md b/snippets/java/math/least-common-multiple.md new file mode 100644 index 00000000..4180be1e --- /dev/null +++ b/snippets/java/math/least-common-multiple.md @@ -0,0 +1,26 @@ +--- +title: Least Common Multiple +description: Calculates the least common multiple (lcm) of two numbers +author: Mcbencrafter +tags: math,number,least-common-multiple,lcm,euclidean-algorithm +--- + +```java +public static int lcm(int number1, int number2) { + int gcdNumber1 = number1; + int gcdNumber2 = number2; + + while (gcdNumber2 != 0) { + int remainder = gcdNumber2; + gcdNumber2 = gcdNumber1 % gcdNumber2; + gcdNumber1 = remainder; + } + + return (number1 / gcdNumber1) * number2; +} + +// Usage: +int a = 16; +int b = 12; +System.out.println(lcm(a, b)); // 48 +``` \ No newline at end of file From 393ae33081ec245fa32ca9156ffa45e03272624b Mon Sep 17 00:00:00 2001 From: Mcbencrafter <92606492+Mcbencrafter@users.noreply.github.com> Date: Sat, 11 Jan 2025 12:07:57 +0100 Subject: [PATCH 4/9] added checksum calculation snippet --- snippets/java/math/checksum.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 snippets/java/math/checksum.md diff --git a/snippets/java/math/checksum.md b/snippets/java/math/checksum.md new file mode 100644 index 00000000..d48184a1 --- /dev/null +++ b/snippets/java/math/checksum.md @@ -0,0 +1,24 @@ +--- +title: Checksum +description: Calculates the checksum of an int +author: Mcbencrafter +tags: math,number,checksum +--- + +```java +public static int checksum(int number) { + number = Math.abs(number); + int sum = 0; + + while (number != 0) { + sum += number % 10; + number /= 10; + } + + return sum; +} + +// Usage: +int number = 12345; +System.out.println(checksum(number)); // 15 = 1+2+3+4+5 +``` \ No newline at end of file From ce14126baa20bc90aa522c0f6a0b5a5770d249d4 Mon Sep 17 00:00:00 2001 From: Mcbencrafter <92606492+Mcbencrafter@users.noreply.github.com> Date: Sat, 11 Jan 2025 12:08:24 +0100 Subject: [PATCH 5/9] added factorial calculation snippet --- snippets/java/math/factorial.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 snippets/java/math/factorial.md diff --git a/snippets/java/math/factorial.md b/snippets/java/math/factorial.md new file mode 100644 index 00000000..6192d08a --- /dev/null +++ b/snippets/java/math/factorial.md @@ -0,0 +1,22 @@ +--- +title: Factorial +description: Computes the factorial of a given number +author: Mcbencrafter +tags: math,number,factorial +--- + +```java +public static BigInteger factorial(int number) { + BigInteger result = BigInteger.ONE; + + for (int currentNumber = 1; currentNumber <= number; currentNumber++) { + result = result.multiply(BigInteger.valueOf(currentNumber)); + } + + return result; +} + +// Usage: +int number = 6; +System.out.println(factorial(number)); // 720 = 6*5*4*3*2 +``` From ee95621227068b04cc48b9fa667fab01fafce47f Mon Sep 17 00:00:00 2001 From: Mcbencrafter <92606492+Mcbencrafter@users.noreply.github.com> Date: Sat, 11 Jan 2025 12:09:03 +0100 Subject: [PATCH 6/9] added snippet for checking if number is a prime --- snippets/java/math/prime-check.md | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 snippets/java/math/prime-check.md diff --git a/snippets/java/math/prime-check.md b/snippets/java/math/prime-check.md new file mode 100644 index 00000000..47b9440a --- /dev/null +++ b/snippets/java/math/prime-check.md @@ -0,0 +1,31 @@ +--- +title: Prime Check +description: Checks if a number is a prime +author: Mcbencrafter +tags: math,number,prime +--- + +```java +public static boolean isPrime(int number) { + if (number <= 1) + return false; + + if (number <= 3) + return true; + + boolean prime = true; + for (int divisor = 3; divisor < number; divisor++) { + if (number % divisor != 0) + continue; + + prime = false; + break; + } + + return prime; +} + +// Usage: +int number = 31; +System.out.println(isPrime(number)); // true +``` \ No newline at end of file From 5b0cc81f5280b013db445dbd61178ca0b8e02c73 Mon Sep 17 00:00:00 2001 From: Mcbencrafter <92606492+Mcbencrafter@users.noreply.github.com> Date: Sat, 11 Jan 2025 12:09:44 +0100 Subject: [PATCH 7/9] added snippet for calculating nth fibonacci number --- snippets/java/math/fibonacci.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 snippets/java/math/fibonacci.md diff --git a/snippets/java/math/fibonacci.md b/snippets/java/math/fibonacci.md new file mode 100644 index 00000000..c2fb507c --- /dev/null +++ b/snippets/java/math/fibonacci.md @@ -0,0 +1,19 @@ +--- +title: Fibonacci +description: Calculates the nth fibonacci number +author: Mcbencrafter +tags: math,number,fibonacci +--- + +```java +public static int fibonacci(int number) { + if (number <= 1) + return number; + + return fibonacci(number - 1) + fibonacci(number - 2); +} + +// Usage: +int number = 5; +System.out.println(fibonacci(number)) // 3 (0, 1, 1, 2, 3) +``` \ No newline at end of file From 8333f132cb2ce205f9e1f9ead76bba0e404cc46e Mon Sep 17 00:00:00 2001 From: Mcbencrafter <92606492+Mcbencrafter@users.noreply.github.com> Date: Sun, 12 Jan 2025 12:36:25 +0100 Subject: [PATCH 8/9] made bit counting description more descriptive --- snippets/java/bit-manipulation/bit-counting.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/java/bit-manipulation/bit-counting.md b/snippets/java/bit-manipulation/bit-counting.md index 66f9313a..534e5f7b 100644 --- a/snippets/java/bit-manipulation/bit-counting.md +++ b/snippets/java/bit-manipulation/bit-counting.md @@ -1,6 +1,6 @@ --- title: Bit Counting -description: Counts the bits in an integer +description: Counts the set bits in the binary representation of an integer author: Mcbencrafter tags: math,number,bits,bit-counting --- From f5ff9b760243a65f7e6db577950483afd5081a7a Mon Sep 17 00:00:00 2001 From: Mcbencrafter <92606492+Mcbencrafter@users.noreply.github.com> Date: Sun, 12 Jan 2025 16:46:23 +0100 Subject: [PATCH 9/9] added missing import --- snippets/java/math/factorial.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/snippets/java/math/factorial.md b/snippets/java/math/factorial.md index 6192d08a..1b3ff2ed 100644 --- a/snippets/java/math/factorial.md +++ b/snippets/java/math/factorial.md @@ -6,6 +6,8 @@ tags: math,number,factorial --- ```java +import java.math.BigInteger; + public static BigInteger factorial(int number) { BigInteger result = BigInteger.ONE;