Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added math and bit manipulation snippets for java #230

Merged
merged 9 commits into from
Jan 13, 2025
23 changes: 23 additions & 0 deletions snippets/java/bit-manipulation/bit-counting.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
title: Bit Counting
description: Counts the set bits in the binary representation of 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)
```
16 changes: 16 additions & 0 deletions snippets/java/bit-manipulation/is-power-of-two.md
Original file line number Diff line number Diff line change
@@ -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)
```
2 changes: 1 addition & 1 deletion snippets/java/date-time/date-time-formatting-american.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion snippets/java/date-time/date-time-formatting-european.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
24 changes: 24 additions & 0 deletions snippets/java/math/checksum.md
Original file line number Diff line number Diff line change
@@ -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
```
24 changes: 24 additions & 0 deletions snippets/java/math/factorial.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
title: Factorial
description: Computes the factorial of a given number
author: Mcbencrafter
tags: math,number,factorial
---

```java
import java.math.BigInteger;

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
```
19 changes: 19 additions & 0 deletions snippets/java/math/fibonacci.md
Original file line number Diff line number Diff line change
@@ -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)
```
23 changes: 23 additions & 0 deletions snippets/java/math/greatest-common-divisor.md
Original file line number Diff line number Diff line change
@@ -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
```
26 changes: 26 additions & 0 deletions snippets/java/math/least-common-multiple.md
Original file line number Diff line number Diff line change
@@ -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
```
31 changes: 31 additions & 0 deletions snippets/java/math/prime-check.md
Original file line number Diff line number Diff line change
@@ -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
```
Loading