tags:

views:

277

answers:

5

From Java Malik textbook- determine if an number is divisible by 11..

Code Solution provided:

import java.util.*;

public class Divby11
{
    static Scanner console = new Scanner(System.in);

    public static void main (String[] args)
    {
        int num, temp, sum;
        char sign;

        System.out.print("Enter a positive integer: ");
        num = console.nextInt();
        System.out.println();

        temp = num;

        sum = 0;
        sign = '+';

        do
        {
            switch (sign)
            {
            case '+' :
                sum = sum + num % 10;
                sign = '-';
                break;

            case '-' :
                sum = sum - num % 10;
                sign = '+';
            }

            num = num / 10;       //remove the last digit
        }
        while (num > 0);

        if (sum % 11 == 0)
            System.out.println(temp + " is divisible by 11");
        else
            System.out.println(temp + " is not divisible by 11");
    }

Why go through all the effort above and just say...

  if (sum % 11 == 0)
            System.out.println(temp + " is divisible by 11");
        else
            System.out.println(temp + " is not divisible by 11");

Can any of you experts see why the author would do it this way (long way)?

A: 

This code example isn't actually dividing by eleven. If you see, it's alternating between adding and subtracting each digit, then checks at the very end if the result is divisible by 11.

For example, look at the following number and how this algorithm works with it:

Start with sum=0, sign='+', num=517
First iteration: sum=7, sign='-', num=51
Second iteration: sum=6, sign='+', num=5
Final iteration: sum=11, sign='-', num=0

The final result is divisible by eleven.

EDIT: The algorithm does indeed look to be implementing the divisibility rule for 11 as dfa mentions in his answer.

Welbog
517 = 11*47.
Michael Myers
dfa is right; this is actually a correct (although unnecessary) algorithm for determining divisibility by 11.
Michael Myers
@iwanttoprogram: I appreciate you selecting my answer as the correct one, but dfa's answer is superior to mine.
Welbog
I selected yours because I already knew what dfa stated how the algorithm worked- you and mmyers- imho clarified it...
iwanttoprogram
@iwanttoprogram: Fair enough.
Welbog
+7  A: 

for the Divisibility Rule of 11:

  • form the alternating sum of the digits
  • if this sum is divisible for 11 then the number is divisible for 11

Examples

  • 68090 = 0 - 9 + 0 - 8 + 6 = -11 => TRUE
  • 493827 = 7 - 2 + 8 - 3 + 9 - 4 = 15 = 4 => FALSE
dfa
Probably faster than dividing by 11 for very large numbers?
Ravi Wallau
Not faster by a long shot.
Bill K
If you read a big number as a String and then process its characters with this Divisibility Rule of 11 then it might be faster
Gábor Hargitai
A: 

I suspect it is simulating the manual test that digits in the odd positions and the digits in the even positions differ by a factor of 11. In practice using %11 would be the way to go.

EDIT: If the example were truly trying to avoid doing % 11, it should send the sum through again until it is 0.

Kathy Van Stone
If it were shooting for efficiency, it wouldn't be doing all those % 10's. Just sayin'.
Michael Myers
The system for dividing by 11 exists for us humans who can extract digits more easily than divide by 11. For computers in the binary world there is little difference. This is an exercise with artificial restraints, not practical ones.
Kathy Van Stone
A: 

You will have to provide more context from the book as to what the author was trying to demonstrate. This code example does not check to see if the number entered is divisible by 11. What it does is it adds every other digit, subtracts every other digit and then checks THAT number to see if it's divisible by 10.

EX Entered number is 4938 It takes the 8 adds it to sum Divides by ten giving 493 Takes the 3 subtracts it from sum: sum = 5 Divides by ten giving 49 Takes 9 and adds it to sum: sum = 14 Divides by ten giving 4 Takes 4 subtracts it from sum: sum = 10

THEN it checks if this is divisible by 11.

Ok, I know why now. He/she's trying to teach you something besides computing about numbers

amischiefr
Coincidentally, 4938 % 11 = 10. Or is it a coincidence?
Michael Myers
Here is what the question states:"Write a program that prompts the user to enter a positive integer and then uses this criterion (from what you mention) to determine whether the number is divisible by 11."
iwanttoprogram
A: 

It an example to show how to implement that particular check. Using your example would not demonstrate the same code methodologies.

Bill K
That is reasonable- I could use the same argument for divisibility by 7 or any other number
iwanttoprogram
Although it is using the divisible by 11 trick which is a little more complicated than the divisible by 3 trick, but yeah, any random algorithm that shows off functionality is good in a text book.
Bill K