# 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

# J, 15 bytes

0 indicates truthy, nonzero indicates falsy.

|~[:+/2#"."0@":


Explanation

        "."0@":  convert to list of digits
[:+/2#         sum 2 copies of the list ([: forces monadic phrase)
|~               residue of sum divided by argument?

• Very clever way to avoid parens or multiple @ or [:! Jul 3, 2017 at 20:05
• I debated posting this as my own answer, but it's not really different enough. |~2*1#.,.&.": for 13 bytes.
– cole
Nov 29, 2017 at 4:14
• I get a 'domain error' for this on my J Qt IDE. (|~[:+/2#"."0@": 112) Then for cole's code I get (|~2*1#.,.&.": 112)=0. :/ Possibly something wrong on my end. Aug 24, 2019 at 11:41

# 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

($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. • 38 bytes by changing the replace to -replace'','+0', 80 would become +08+00+08+00+0 May 8, 2023 at 2:14 # Haskell, 35 34 bytes 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: echo 80 | php -nR 'for(;~$d=$argn[$i++];)$t+=2*$d;echo$argn%$t;'


# Explanation

Iterates over the digits to compute the total, then outputs the modulo like most answers.

# Java (OpenJDK 8), 55 53 bytes

a->{int i=0,x=a;for(;x>0;x/=10)i+=x%10*2;return a%i;}


Try it online!

A return value of 0 means truthy, anything else means falsy.

Since my comment in Okx's answer made no ripple, I deleted it and posted it as this answer, golfed even a bit more.

Further golfing thanks to @KrzysztofCichocki and @Laikoni who rightfully showed me I needn't answer a truthy/falsy value, but any value as long as I describe the result.

• You can remove the <1 part at the end, so the result will be 0 for true and >0 for false, which is accebtable, this will result in additional -2 bytes, so you answer coiuld be like 53 bytes. Jul 5, 2017 at 7:50
• @KrzysztofCichoki No I can't: this is Java. The only truthy value is true. Jul 5, 2017 at 7:56
• @OlivierGrégoire While this true if nothing else is specified, this challenge specifically states 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.. Jul 5, 2017 at 8:13
• @KrzysztofCichocki and Laikoni Sorry I misread that part, I just fixed it! Thank you both :) Also, sorry for rejecting the edit which was actually appropriate in this case. Jul 5, 2017 at 10:32

# Mini-Flak, 296 292 bytes

({}((()[()]))){(({}[((((()()()){}){}){}){}])({}(((({})){}){}{}){})[({})])(({}({})))({}(({}[{}]))[{}])({}[({})](({}{})(({}({})))({}(({}[{}]))[{}])({}[({})]))[{}])(({}({})))({}(({}[{}]))[{}])({}[({})])}{}({}(((({}){})))[{}]){({}((({}[()]))([{(({})[{}])()}{}]()){{}{}(({}))(()[()])}{})[{}][()])}


Try it online!

The TIO link have more comments from me, so it is partially easier to read.

Truthy/Falsey: Truthy (divisible) if the second number is equal to the third number, falsy otherwise. So both the truthy and falsy set are infinite, but I suppose that should be allowed. +10 byte if that is not.

Note: Leading/trailing newlines/whitespaces are not allowed in input.

# C# (.NET Core), 51 bytes

n=>{int l=n,k=0;for(;l>0;l/=10)k+=l%10;return n%k;}


Try it online!

0 for truthy, anything else for falsey.

# Perl 5, 21 + 1 (-p) = 22 bytes

$_%=eval s/./+2*$&/rg


Try it online!

• -1 byte: $_%=eval s//+2*/rg.0 Feb 6, 2019 at 22:25 # 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): Jump to Label DONE 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 # Desmos, 62 bytes f(n)=0^{mod(n,2mod(floor(n/10^{[0...floor(logn)]}),10).total)}  Try It Online! Try It Online! - Prettified # Knight, 39 bytes ;=n!=p=sE P;Ws;=n+*2%s 10n=s/s 10O!%p n  Try it online! Test Suite # Arturo, 21 bytes $->n->n%2*∑digits n


Try it

Returns 0 for true or any other number for false.

# Thunno 2, 3 bytes

SḌḊ


#### Explanation

SḌḊ   # implicit input
S     # sum the digits
Ḍ    # double this number
Ḋ   # input is divisible by this?
# implicit output


# Lua, 47 bytes

n=...a=0n:gsub('.',load'a=2*...+a')print(n%a<1)


Try it online!

# 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!