32
\$\begingroup\$

To check whether a decimal number is divisible by 7:

Erase the last digit. Multiply it by 2 and subtract from what is left. If the result is divisible by 7, the original number is divisible by 7.

(also described e.g. here)

This rule is good for manual divisibility check. For example:

Is 2016 divisible by 7?

Subtract 6*2 from 201; we get 189. Is this divisible by 7? To check it, let's apply the rule again.

Subtract 9*2 from 18; we get 0. Therefore, 2016 is divisible by 7.

In this challenge, you should apply this rule until the divisibility status is obvious, that is, the number is not greater than 70 (however, see below for details). Make a function or a full program.

Input: a positive integer; your code should support inputs up to 32767 (supporting arbitrary-precision integers is a bonus; see below).

Output: an integer (possibly negative), not greater than 70, that is a result of applying the divisibility-by-7 rule zero or more times.

Test cases:

Input                   Output      Alternative output

1                       1
10                      10          1
100                     10          1
13                      13          -5
42                      42          0
2016                    0
9                       9
99                      -9
9999                    -3
12345                   3
32767                   28          -14

---------- Values below are only relevant for the bonus

700168844221            70          7
36893488147419103232    32          -1
231584178474632390847141970017375815706539969331281128078915168015826259279872    8

Where two possible outputs are specified, either result is correct: the second one corresponds to applying the rule one more time. It's forbidden to apply the rule on a single-digit number: if you erase the digit, nothing (not 0) is left.


Bonus: If your algorithm

where n is the number of decimal digits:

Subtract 50% from your code's byte count.

Real bonus:

In addition, if your algorithm reads the input in normal direction, starting from the most significant digit, subtract 50% once again - your score is 25% of your byte count (it seems possible, but I'm not absolutely sure).

\$\endgroup\$
19
  • 1
    \$\begingroup\$ @DenkerAffe Returning the input as-is is acceptable. I updated the test-case of input=10 to reflect this; that was the idea from the beginning. \$\endgroup\$
    – anatolyg
    Commented Feb 14, 2016 at 20:40
  • 4
    \$\begingroup\$ I wouldn't want to use that rule on 1000000000000000000001. \$\endgroup\$
    – Neil
    Commented Feb 14, 2016 at 20:55
  • 1
    \$\begingroup\$ But what if your language has long longs or some equivalent type built in? \$\endgroup\$ Commented Feb 14, 2016 at 22:00
  • 1
    \$\begingroup\$ What I was saying was that, in some implementations, it's a 128-bit integer, which is more than big enough for that last test case. \$\endgroup\$ Commented Feb 14, 2016 at 22:18
  • 7
    \$\begingroup\$ -1. Not all languages support arbitrary precision. \$\endgroup\$
    – March Ho
    Commented Feb 15, 2016 at 3:54

31 Answers 31

1
2
0
\$\begingroup\$

JavaScript (Node.js), 91 - 50% = 46 bytes

n=BigInt(process.argv[2])
f=x=>(x/10n|0n)-2n*(x%10n)
while(n>70n){n=f(n)}
console.log(n+'')

Ran the code in VS Code with command line input, and tested all the provided test cases. Added support for long integers, so claiming 50% bonus.

\$\endgroup\$
2
  • \$\begingroup\$ You don't have to define f as a function of x, you can just do those operations directly to n. Also, any while loop can be reduced to a for loop which is a little shorter. for(n=BigInt(process.argv[2]);n>70n;n=(n/10n|0n)-x%10n*2n);console.log(n) \$\endgroup\$ Commented Jul 28 at 0:29
  • \$\begingroup\$ If you want you could instead write this as a function taking n as input and returning it: f=n=>n>70n?f(n/10n-n%10n*2n):n \$\endgroup\$ Commented Jul 28 at 0:32
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.