mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-25 05:22:39 +08:00
style: enable NeedBraces
in checkstyle (#5227)
* enable style NeedBraces * style: enable NeedBraces in checkstyle --------- Co-authored-by: Samuel Facchinello <samuel.facchinello@piksel.com>
This commit is contained in:

committed by
GitHub

parent
51fcc66345
commit
87b17e0571
@ -34,7 +34,9 @@ public final class AliquotSum {
|
||||
* @return aliquot sum of given {@code number}
|
||||
*/
|
||||
public static int getAliquotSum(int n) {
|
||||
if (n <= 0) return -1;
|
||||
if (n <= 0) {
|
||||
return -1;
|
||||
}
|
||||
int sum = 1;
|
||||
double root = Math.sqrt(n);
|
||||
/*
|
||||
@ -53,7 +55,9 @@ public final class AliquotSum {
|
||||
}
|
||||
// if n is a perfect square then its root was added twice in above loop, so subtracting root
|
||||
// from sum
|
||||
if (root == (int) root) sum -= root;
|
||||
if (root == (int) root) {
|
||||
sum -= root;
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
}
|
||||
|
@ -22,7 +22,9 @@ public final class AutomorphicNumber {
|
||||
* {@code false}
|
||||
*/
|
||||
public static boolean isAutomorphic(long n) {
|
||||
if (n < 0) return false;
|
||||
if (n < 0) {
|
||||
return false;
|
||||
}
|
||||
long square = n * n; // Calculating square of the number
|
||||
long t = n;
|
||||
long numberOfdigits = 0;
|
||||
@ -42,7 +44,9 @@ public final class AutomorphicNumber {
|
||||
* {@code false}
|
||||
*/
|
||||
public static boolean isAutomorphic2(long n) {
|
||||
if (n < 0) return false;
|
||||
if (n < 0) {
|
||||
return false;
|
||||
}
|
||||
long square = n * n; // Calculating square of the number
|
||||
return String.valueOf(square).endsWith(String.valueOf(n));
|
||||
}
|
||||
@ -56,7 +60,9 @@ public final class AutomorphicNumber {
|
||||
*/
|
||||
public static boolean isAutomorphic3(String s) {
|
||||
BigInteger n = new BigInteger(s);
|
||||
if (n.signum() == -1) return false; // if number is negative, return false
|
||||
if (n.signum() == -1) {
|
||||
return false; // if number is negative, return false
|
||||
}
|
||||
BigInteger square = n.multiply(n); // Calculating square of the number
|
||||
return String.valueOf(square).endsWith(String.valueOf(n));
|
||||
}
|
||||
|
@ -14,7 +14,9 @@ public final class HarshadNumber {
|
||||
* {@code false}
|
||||
*/
|
||||
public static boolean isHarshad(long n) {
|
||||
if (n <= 0) return false;
|
||||
if (n <= 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
long t = n;
|
||||
long sumOfDigits = 0;
|
||||
@ -35,7 +37,9 @@ public final class HarshadNumber {
|
||||
*/
|
||||
public static boolean isHarshad(String s) {
|
||||
final Long n = Long.valueOf(s);
|
||||
if (n <= 0) return false;
|
||||
if (n <= 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
int sumOfDigits = 0;
|
||||
for (char ch : s.toCharArray()) {
|
||||
|
@ -16,11 +16,15 @@ public final class KaprekarNumbers {
|
||||
// Provides a list of kaprekarNumber in a range
|
||||
public static List<Long> kaprekarNumberInRange(long start, long end) throws Exception {
|
||||
long n = end - start;
|
||||
if (n < 0) throw new Exception("Invalid range");
|
||||
if (n < 0) {
|
||||
throw new Exception("Invalid range");
|
||||
}
|
||||
ArrayList<Long> list = new ArrayList<>();
|
||||
|
||||
for (long i = start; i <= end; i++) {
|
||||
if (isKaprekarNumber(i)) list.add(i);
|
||||
if (isKaprekarNumber(i)) {
|
||||
list.add(i);
|
||||
}
|
||||
}
|
||||
|
||||
return list;
|
||||
|
@ -20,7 +20,9 @@ public final class MillerRabinPrimalityCheck {
|
||||
*/
|
||||
|
||||
public static boolean millerRabin(long n, int k) { // returns true if n is probably prime, else returns false.
|
||||
if (n < 4) return n == 2 || n == 3;
|
||||
if (n < 4) {
|
||||
return n == 2 || n == 3;
|
||||
}
|
||||
|
||||
int s = 0;
|
||||
long d = n - 1;
|
||||
@ -31,13 +33,17 @@ public final class MillerRabinPrimalityCheck {
|
||||
Random rnd = new Random();
|
||||
for (int i = 0; i < k; i++) {
|
||||
long a = 2 + rnd.nextLong(n) % (n - 3);
|
||||
if (checkComposite(n, a, d, s)) return false;
|
||||
if (checkComposite(n, a, d, s)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean deterministicMillerRabin(long n) { // returns true if n is prime, else returns false.
|
||||
if (n < 2) return false;
|
||||
if (n < 2) {
|
||||
return false;
|
||||
}
|
||||
|
||||
int r = 0;
|
||||
long d = n - 1;
|
||||
@ -47,8 +53,12 @@ public final class MillerRabinPrimalityCheck {
|
||||
}
|
||||
|
||||
for (int a : new int[] {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37}) {
|
||||
if (n == a) return true;
|
||||
if (checkComposite(n, a, d, r)) return false;
|
||||
if (n == a) {
|
||||
return true;
|
||||
}
|
||||
if (checkComposite(n, a, d, r)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@ -66,10 +76,14 @@ public final class MillerRabinPrimalityCheck {
|
||||
*/
|
||||
private static boolean checkComposite(long n, long a, long d, int s) {
|
||||
long x = powerModP(a, d, n);
|
||||
if (x == 1 || x == n - 1) return false;
|
||||
if (x == 1 || x == n - 1) {
|
||||
return false;
|
||||
}
|
||||
for (int r = 1; r < s; r++) {
|
||||
x = powerModP(x, 2, n);
|
||||
if (x == n - 1) return false;
|
||||
if (x == n - 1) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@ -79,11 +93,14 @@ public final class MillerRabinPrimalityCheck {
|
||||
|
||||
x = x % p; // Update x if it is more than or equal to p
|
||||
|
||||
if (x == 0) return 0; // In case x is divisible by p;
|
||||
|
||||
if (x == 0) {
|
||||
return 0; // In case x is divisible by p;
|
||||
}
|
||||
while (y > 0) {
|
||||
// If y is odd, multiply x with result
|
||||
if ((y & 1) == 1) res = multiplyModP(res, x, p);
|
||||
if ((y & 1) == 1) {
|
||||
res = multiplyModP(res, x, p);
|
||||
}
|
||||
|
||||
// y must be even now
|
||||
y = y >> 1; // y = y/2
|
||||
|
@ -52,10 +52,11 @@ public final class PascalTriangle {
|
||||
*/
|
||||
for (int i = 0; i <= line; i++) {
|
||||
// First and last values in every row are 1
|
||||
if (line == i || i == 0) arr[line][i] = 1;
|
||||
// The rest elements are sum of values just above and left of above
|
||||
else
|
||||
if (line == i || i == 0) {
|
||||
arr[line][i] = 1;
|
||||
} else {
|
||||
arr[line][i] = arr[line - 1][i - 1] + arr[line - 1][i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -19,7 +19,9 @@ public final class PerfectNumber {
|
||||
* @return {@code true} if {@code number} is perfect number, otherwise false
|
||||
*/
|
||||
public static boolean isPerfectNumber(int number) {
|
||||
if (number <= 0) return false;
|
||||
if (number <= 0) {
|
||||
return false;
|
||||
}
|
||||
int sum = 0;
|
||||
/* sum of its positive divisors */
|
||||
for (int i = 1; i < number; ++i) {
|
||||
@ -37,7 +39,9 @@ public final class PerfectNumber {
|
||||
* @return {@code true} if {@code number} is perfect number, otherwise false
|
||||
*/
|
||||
public static boolean isPerfectNumber2(int n) {
|
||||
if (n <= 0) return false;
|
||||
if (n <= 0) {
|
||||
return false;
|
||||
}
|
||||
int sum = 1;
|
||||
double root = Math.sqrt(n);
|
||||
|
||||
@ -58,7 +62,9 @@ public final class PerfectNumber {
|
||||
|
||||
// if n is a perfect square then its root was added twice in above loop, so subtracting root
|
||||
// from sum
|
||||
if (root == (int) root) sum -= root;
|
||||
if (root == (int) root) {
|
||||
sum -= root;
|
||||
}
|
||||
|
||||
return sum == n;
|
||||
}
|
||||
|
@ -12,7 +12,9 @@ public class SumWithoutArithmeticOperators {
|
||||
*/
|
||||
|
||||
public int getSum(int a, int b) {
|
||||
if (b == 0) return a;
|
||||
if (b == 0) {
|
||||
return a;
|
||||
}
|
||||
int sum = a ^ b;
|
||||
int carry = (a & b) << 1;
|
||||
return getSum(sum, carry);
|
||||
|
Reference in New Issue
Block a user