# Am I divisible by double the sum of my digits?

Given a positive integer as input, your task is to output a truthy value if the number is divisible by the double of the sum of its digits, and a falsy value otherwise (OEIS A134516). In other words:

(sum_of_digits)*2 | number

• Instead of truthy / falsy values for the true and false cases, you may instead specify any finite set of values for the true/false case, and their complement the other values. For a simple example, you may use 0 for the true case and all other numbers for the false case (or vice versa, if you like).

• Standard input and output rules apply. Default Loopholes also apply.

• You can take input as an integer or as the string representation of that integer.

• This is , hence the shortest code in bytes wins!

• I am new to PPCG, so I would like you to post an explanation if it's possible.

# Test Cases

Input - Output - (Reason)

80  - Truthy - (16 divides 80)
100 - Truthy - (2 divides 100)
60  - Truthy - (12 divides 60)
18 - Truthy - (18 divides 18)
12 - Truthy - (6 divides 12)

4 - Falsy - (8 does not divide 4)
8 - Falsy - (16 does not divide 8)
16  - Falsy  - (14 does not divide 16)
21 - Falsy - (6 does not divide 21)
78  - Falsy  - (30 does not divide 78)
110 - Falsy - (4 does not dide 110)
111 - Falsy - (6 does not divide 111)
390 - Falsy  - (24 does not divide 390)

• Good challenge, welcome to PPCG! Jul 3, 2017 at 13:59
• @Mayube Thanks, it is my second challenge, but the first one got closed :P
– user70974
Jul 3, 2017 at 14:00
• Are we allowed to take digits as a list of Integers? Jul 3, 2017 at 14:32
• @Henry No, that would be way too trivial
– user70974
Jul 3, 2017 at 14:34
• Indeed, the two sentences of "Instead of truthy / falsy values for the true and false cases, you may instead specify any finite set of values for the true case, and their complement for the falsy ones. For a simple example, you may use 0 for the true case and all other numbers for the false case (or vice versa, if you like)" seem to contradict each other (in particular, the "finite" and the "or vice versa"). Jul 4, 2017 at 2:52

# Ohm, 5 bytes

D}Σd¥


Try it online!

# tcl, 45

puts [expr 1>$n%(2*([join [split$n ""] +]))]


# demo

• You can replace 0== with 1>. Jul 3, 2017 at 19:25

f x=mod x$2*sum[read[c]|c<-show x]  Try it online! Returns '0' in the true case, the remainder otherwise. # Haskell, pointfree edition by nimi, 34 bytes mod<*>(2*).sum.map(read.pure).show  Try it online! • Same byte count if you go pointfree: mod<*>(2*).sum.map(read.pure).show – nimi Jul 4, 2017 at 15:34 • Looks good, I added it in my submission. Jul 4, 2017 at 15:46 # PHP, 44 bytes for(;~$d=$argn[$i++];)$t+=2*$d;echo$argn%$t;


Run like this:

# Ly, 10 bytes

nsS&+2*lf%


Try it online!

Ly is lucky enough to have a splitting built-in.

Returns 0 for truthy and a number greater than 0 for falsy.

# Pyth - 12 Bytes

%sZ*2ssMscZ1


Explanation:

%       - implicitly print modulus of
s      - parse int
Z     - input
*      - two times
s    - sum of
M   - map
s  - parse int to
c  - substrings
Z - of input
1 - of length 1


# Brachylog, 7 bytes

f.&ẹj+∈


Try it online!

The predicate succeeds if the input is divisible by twice the sum of its digits, and fails otherwise. Run as a program, success prints true. and failure prints false..

This started off as a minor edit to Fatalize's answer, golfing a byte off by replacing +×₂ with j+, but as I was testing it I noticed that one or another of the updates he's made to the language in the last year and a half seems to have broken his solution on the case of 18, with I apparently being unable to assume the value of 1, so I ended up having to restructure it somewhat (fortunately without changing the byte count).

f.         The output variable is a list of the input's factors.
&        And,
ẹ       the digits of the input,
j      all duplicated,
+     sum up to
∈    a member of the output variable.


# Befunge-93, 34 bytes

p&::v>0g2*%.@
+19:_^#:/+91p00+g00%


Try it online!

Prints 0 for truthy, a different integer for falsey.

# Runic Enchantments, 11 bytes

i:'+A2*%0=@


Try it online!

### Explanation

i:             Read input, duplicate it
'+A          Sum its digits
2*        Multiply by 2
%       Modulo input with digit sum
0=     Compare to 0
@    Print and terminate


# Forth (gforth), 58 bytes

: f 0 over begin 10 /mod >r + r> ?dup 0= until 2* mod 0= ;


Try it online!

### Code Explanation

: f           \ start a new word definition
0 over      \ create an accumulator and copy the input above it on the stack
begin       \ start an indefinite loop
10 /mod   \ divide by 10 and get quotient and remainder
>r + r>   \ add remainder to accumulator
?dup      \ duplicate the quotient if it's greater than 0
0=        \ check if quotient is 0
until       \ end the indefinite loop if it is
2* mod      \ multiply result by 2, then modulo with input number
0=          \ check if result is 0 (input is divisible by result)
;             \ end word definition


## Keg+Reg, 11 bytes

Boring algorithm.

¿:;9%1+2*%


Finds the digital root of the top of the stack and then finds the remainder.

0 is a truthy value, any value that is not 0 is a falsy value. (Including ASCII characters)

## Explanation (Syntactically invalid)

¿#         Integer input
:#        Duplicate
;9%1+#   Digital Root Formula (1+(n-1)mod 9)
2*%#Multiply by 2 and find the remainder


I believe everyone here used repetition/reduction but not an existing formula.

# Vyxal, 3 bytes

∑dœ


Try it Online!

Compiles as:

stack.push(summate(stack.pop()))
stack.push(stack.pop() * 2)
lhs, rhs = stack.pop(2); stack.push(int((rhs % lhs) == 0))


## Explained

∑dœ
∑   # Push the sum of the integers of the input
d  # Double that sum
œ # And push ((sum(input) * 2) % input) == 0


# MathGolf, 3 bytes

Σ∞÷


Try it online.

Explanation:

Σ    # Get the digit-sum of the (implicit) input-integer
∞   # Double it
÷  # Check if the (implicit) input-integer is divisible by this value
# (after which the entire stack is output implicitly as result)


# Whitespace, 103 bytes

[S S S N
_Push_0][S N
S _Duplicate_0][S N
S _Duplicate_0][T   N
T   T   _Read_STDIN_as_integer][T   T   T   _Retrieve_input][N
S S N
_Create_Label_LOOP][S N
S _Duplicate][N
T   S S N
_If_0_Jump_to_Label_DONE][S N
S _Duplicate][S S S T   S T S N
_Push_10][T S T T   _Modulo][S T    S S T   S N
_Copy_0-based_2nd][T    S S S _Add][S N
T   _Swap_top_two][S S S T  S T S N
_Push_10][T S T S _Integer_divide][N
S N
N
_Jump_to_Label_LOOP][N
S S S N
_Create_Label_DONE][T   T   T   _Retrieve_input][S N
T   _Swap_top_two][S S S T  S N
_Push_2][T  S S N
_Multiply]{T    S T T   _Modulo][T  N
S T _Print_as_integer]


Letters S (space), T (tab), and N (new-line) added as highlighting only.
[..._some_action] added as explanation only.

Outputs 0 as truthy or a positive integer as falsey.

Try it online (with raw spaces, tabs and new-lines only).

To output two distinct values (0 for truthy; 1 for falsey) it would be 128 bytes instead: try it online.

Explanation in pseudo-code:

Integer sum = 0
Integer input = STDIN as integer
Integer n = input
Start LOOP:
If(n == 0):
Integer t = n modulo-10
sum = sum + t
n = n integer-divided by 10
Go to next iteration of LOOP

DONE:
sum = sum * 2
Integer result = input % sum
Print result as integer to STDOUT


# Factor + math.text.utils math.unicode, 37 bytes

[ dup 1 digit-groups Σ 2 * mod 0 = ]


Try it online!

## Explanation

                ! 12
dup             ! 12 12
1 digit-groups  ! 12 { 2 1 }
Σ               ! 12 3
2 *             ! 12 6
mod             ! 0
0 =             ! t


# Vyxal, 3 bytes

∑dḊ


Try it Online!

# C++, 121 bytes

#include<iostream>
main(){std::string n;int m=0;std::cin>>n;for(char i:n){m+=i-48;};std::cout<<((stoi(n)%(m*2)==0)?0:1);}


Try it online!

# Explanation

#include<iostream>
main(){
std::string n; int m = 0; // Initialize n (input) and m (sum)
std::cin >> n; // Get input
for(char i:n) // Loop through input to calculate the sum of digits
{
m+=i-48;
};
std::cout<<((stoi(n)%(m*2)==0)?0:1); // Output 0 if double the sum of digits is divisible by the number, else return 1
}

• 109 bytes Mar 7, 2022 at 9:30

# Jelly, 4 bytes

DSḤḍ


Try it online!

Explanation:

DSḤḍ Takes an integer as input.
D    Get digits (convert to base 10)
S   Sum
Ḥ  Unhalve (double)
ḍ Check if y (the input itself) is divisible by x

• @TheIOSCoder Added explanation. Jul 3, 2017 at 13:50

# Pari/GP, 24 bytes

n->!(n%(2*sumdigits(n)))


Try it online!

# jq, 37 characters

.%(tostring/""|map(tonumber)|add*2)<1


Sample run:

bash-4.4$jq '.%(tostring/""|map(tonumber)|add*2)<1' <<< '60' true bash-4.4$ jq '.%(tostring/""|map(tonumber)|add*2)<1' <<< '390'
false


Try on jp‣play

($a="$args")%($a*2-replace'\B','+'|iex)  0 as Truthy output, any other number as Falsey. PS D:\> D:\t.ps1 80 0 PS D:\> D:\t.ps1 81 9  It takes the args array as a string, does string multiplication to double all the digits, regex-replaces non-word-boundaries to make '8+0+8+0', eval's that to get the sum, and calculates a remainder of original/sum. # Bash + coreutils + bc, 46 bytes yes$1%$$0echo 1|sed 's/\(.$$/+\1*2/g'\)|bc


Input via command line. 0 is truthy.

First I tried to loop through the characters of the input string, but bash loops are to big. I ended up using sed to replace each character with an addition and multiplication, which then goes to bc. I saved a byte using yes instead of echo.

# Perl 5, 27 bytes

$_%(2*eval join'+',split//)  Returns 0 for Truthy and non-zero for Falsy • Welcome on the site. Answers need to be either full programs or functions, while your answer is only a snippet. To make it a full program, you could add -p flag (cost 1 byte), and start with $_%=(2*...). Also, join'+',split// can be simplified to join'+',@F is you use -F  flag, or even better, s/./+\$&/gr :)
Jul 4, 2017 at 9:21

# CJam, 11 10 bytes

1 byte saved thanks to @Challenger5

{_Ab:+2*%}


Anonymous block that takes the input from the stack and replaces it by the output, which is 0 if divisible or a positive integer otherwise.

### Explanation

{        }   e# Block
_           e# Duplicate
Ab         e# Convert to base 10. Gives an array of digits
:+       e# Fold addition over array
2*     e# Multiply by 2
%    e# Modulus

• You can replace s:~ with Ab (convert to base 10) Jul 4, 2017 at 6:36
• @Challenger5 Thanks! Edited Jul 4, 2017 at 8:26

## LOGO, 24 bytes

[modulo ? 2*reduce "+ ?]


Try it online!
Only FMSLogo and its older version (MSWLogo) support ? in template-list; and only FMSLogo supports apply for infix operator +.

That is a template-list (equivalent of lambda function in other languages). Return 0 for truthy and other values for falsy.

Usage:

pr invoke [modulo ? 2*reduce "+ ?] 100


(invoke calls the function, pr prints the result)

Explanation:

There is only one points need explaining: reduce. That is similar to fold in some other languages: reduce "f [1 2 3 4] calculate (f (f (f 1 2) 3) 4). In this case, reduce "+ ? calculate total of elements of digits in ?.

Numbers in LOGO is represented by word, so reduce, a library function can receive a word as its second parameter (while apply can't)