Skip to main content
deleted 71 characters in body
Source Link
Simon Forsberg
  • 59.8k
  • 9
  • 160
  • 312

As your original implementation doesn't produce the correct results for 21, 42, 69, 81, 84, 87, and 93 (that's just from 0 to 100 !), I wanted to provide a more "common" way to handle the question of divisibility by three. This is what many humans do when they are faced with a number with a lot of digits and want to determine if it's divisible by three or not:

A number is divisible by 3 if the sum of all its digits is divisible by three

The main benefit of this approach is that it's easier to understand for the human mind. This can be turned into a nice little recursive method:

public static boolean isDivisibleByThree(int n) {
    int sum = 0;
    int abs = Math.abs(n);
    if (abs < 10) {
        return abs == 3 || abs == 6 || abs == 9 || abs == 0;
    }
    while (n != 0) {
        sum += n % 10;
        n = n / 10;
    }
    return isDivisibleByThree(sum);
}

Admittedly, this is not as fast as your current approach (according to my benchmarking, it's about twice as slow), but it does produce the correct results. (Even for the extreme value of Integer.MIN_VALUE).

Although in the end, nothing is as readable for a programmer as i % 3 == 0. Unless you do C/C++, in which case I believe that'd be (i % 3) == 0

As your original implementation doesn't produce the correct results for 21, 42, 69, 81, 84, 87, and 93 (that's just from 0 to 100 !), I wanted to provide a more "common" way to handle the question of divisibility by three. This is what many humans do when they are faced with a number with a lot of digits and want to determine if it's divisible by three or not:

A number is divisible by 3 if the sum of all its digits is divisible by three

The main benefit of this approach is that it's easier to understand for the human mind. This can be turned into a nice little recursive method:

public static boolean isDivisibleByThree(int n) {
    int sum = 0;
    int abs = Math.abs(n);
    if (abs < 10) {
        return abs == 3 || abs == 6 || abs == 9 || abs == 0;
    }
    while (n != 0) {
        sum += n % 10;
        n = n / 10;
    }
    return isDivisibleByThree(sum);
}

Admittedly, this is not as fast as your current approach (according to my benchmarking, it's about twice as slow), but it does produce the correct results. (Even for the extreme value of Integer.MIN_VALUE).

Although in the end, nothing is as readable for a programmer as i % 3 == 0. Unless you do C/C++, in which case I believe that'd be (i % 3) == 0

As your original implementation doesn't produce the correct results for 21, 42, 69, 81, 84, 87, and 93 (that's just from 0 to 100 !), I wanted to provide a more "common" way to handle the question of divisibility by three. This is what many humans do when they are faced with a number with a lot of digits and want to determine if it's divisible by three or not:

A number is divisible by 3 if the sum of all its digits is divisible by three

The main benefit of this approach is that it's easier to understand for the human mind. This can be turned into a nice little recursive method:

public static boolean isDivisibleByThree(int n) {
    int sum = 0;
    int abs = Math.abs(n);
    if (abs < 10) {
        return abs == 3 || abs == 6 || abs == 9 || abs == 0;
    }
    while (n != 0) {
        sum += n % 10;
        n = n / 10;
    }
    return isDivisibleByThree(sum);
}

Admittedly, this is not as fast as your current approach (according to my benchmarking, it's about twice as slow), but it does produce the correct results. (Even for the extreme value of Integer.MIN_VALUE).

Although in the end, nothing is as readable for a programmer as i % 3 == 0

it's --> its
Source Link
Carl Manaster
  • 1.3k
  • 10
  • 15

As your original implementation doesn't produce the correct results for 21, 42, 69, 81, 84, 87, and 93 (that's just from 0 to 100 !), I wanted to provide a more "common" way to handle the question of divisibility by three. This is what many humans do when they are faced with a number with a lot of digits and want to determine if it's divisible by three or not:

A number is divisible by 3 if the sum of all it'sits digits is divisible by three

The main benefit of this approach is that it's easier to understand for the human mind. This can be turned into a nice little recursive method:

public static boolean isDivisibleByThree(int n) {
    int sum = 0;
    int abs = Math.abs(n);
    if (abs < 10) {
        return abs == 3 || abs == 6 || abs == 9 || abs == 0;
    }
    while (n != 0) {
        sum += n % 10;
        n = n / 10;
    }
    return isDivisibleByThree(sum);
}

Admittedly, this is not as fast as your current approach (according to my benchmarking, it's about twice as slow), but it does produce the correct results. (Even for the extreme value of Integer.MIN_VALUE).

Although in the end, nothing is as readable for a programmer as i % 3 == 0. Unless you do C/C++, in which case I believe that'd be (i % 3) == 0

As your original implementation doesn't produce the correct results for 21, 42, 69, 81, 84, 87, and 93 (that's just from 0 to 100 !), I wanted to provide a more "common" way to handle the question of divisibility by three. This is what many humans do when they are faced with a number with a lot of digits and want to determine if it's divisible by three or not:

A number is divisible by 3 if the sum of all it's digits is divisible by three

The main benefit of this approach is that it's easier to understand for the human mind. This can be turned into a nice little recursive method:

public static boolean isDivisibleByThree(int n) {
    int sum = 0;
    int abs = Math.abs(n);
    if (abs < 10) {
        return abs == 3 || abs == 6 || abs == 9 || abs == 0;
    }
    while (n != 0) {
        sum += n % 10;
        n = n / 10;
    }
    return isDivisibleByThree(sum);
}

Admittedly, this is not as fast as your current approach (according to my benchmarking, it's about twice as slow), but it does produce the correct results. (Even for the extreme value of Integer.MIN_VALUE).

Although in the end, nothing is as readable for a programmer as i % 3 == 0. Unless you do C/C++, in which case I believe that'd be (i % 3) == 0

As your original implementation doesn't produce the correct results for 21, 42, 69, 81, 84, 87, and 93 (that's just from 0 to 100 !), I wanted to provide a more "common" way to handle the question of divisibility by three. This is what many humans do when they are faced with a number with a lot of digits and want to determine if it's divisible by three or not:

A number is divisible by 3 if the sum of all its digits is divisible by three

The main benefit of this approach is that it's easier to understand for the human mind. This can be turned into a nice little recursive method:

public static boolean isDivisibleByThree(int n) {
    int sum = 0;
    int abs = Math.abs(n);
    if (abs < 10) {
        return abs == 3 || abs == 6 || abs == 9 || abs == 0;
    }
    while (n != 0) {
        sum += n % 10;
        n = n / 10;
    }
    return isDivisibleByThree(sum);
}

Admittedly, this is not as fast as your current approach (according to my benchmarking, it's about twice as slow), but it does produce the correct results. (Even for the extreme value of Integer.MIN_VALUE).

Although in the end, nothing is as readable for a programmer as i % 3 == 0. Unless you do C/C++, in which case I believe that'd be (i % 3) == 0

Source Link
Simon Forsberg
  • 59.8k
  • 9
  • 160
  • 312

As your original implementation doesn't produce the correct results for 21, 42, 69, 81, 84, 87, and 93 (that's just from 0 to 100 !), I wanted to provide a more "common" way to handle the question of divisibility by three. This is what many humans do when they are faced with a number with a lot of digits and want to determine if it's divisible by three or not:

A number is divisible by 3 if the sum of all it's digits is divisible by three

The main benefit of this approach is that it's easier to understand for the human mind. This can be turned into a nice little recursive method:

public static boolean isDivisibleByThree(int n) {
    int sum = 0;
    int abs = Math.abs(n);
    if (abs < 10) {
        return abs == 3 || abs == 6 || abs == 9 || abs == 0;
    }
    while (n != 0) {
        sum += n % 10;
        n = n / 10;
    }
    return isDivisibleByThree(sum);
}

Admittedly, this is not as fast as your current approach (according to my benchmarking, it's about twice as slow), but it does produce the correct results. (Even for the extreme value of Integer.MIN_VALUE).

Although in the end, nothing is as readable for a programmer as i % 3 == 0. Unless you do C/C++, in which case I believe that'd be (i % 3) == 0