-
-
Notifications
You must be signed in to change notification settings - Fork 130
Added math and bit manipulation snippets for java #230
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
Merged
Merged
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
630477c
capataliezed titles of date/time snippets
Mcbencrafter 4cabb19
added bit manipulation snippets
Mcbencrafter 0cace22
added gcd and lcm snippets
Mcbencrafter 393ae33
added checksum calculation snippet
Mcbencrafter ce14126
added factorial calculation snippet
Mcbencrafter ee95621
added snippet for checking if number is a prime
Mcbencrafter 5b0cc81
added snippet for calculating nth fibonacci number
Mcbencrafter 8333f13
made bit counting description more descriptive
Mcbencrafter f5ff9b7
added missing import
Mcbencrafter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
snippets/java/date-time/duration-formatting-hours-minutes-seconds.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.