42
\$\begingroup\$

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)
\$\endgroup\$
11
  • \$\begingroup\$ Good challenge, welcome to PPCG! \$\endgroup\$
    – Mayube
    Jul 3, 2017 at 13:59
  • \$\begingroup\$ @Mayube Thanks, it is my second challenge, but the first one got closed :P \$\endgroup\$
    – user70974
    Jul 3, 2017 at 14:00
  • \$\begingroup\$ Are we allowed to take digits as a list of Integers? \$\endgroup\$
    – Henry
    Jul 3, 2017 at 14:32
  • 5
    \$\begingroup\$ @Henry No, that would be way too trivial \$\endgroup\$
    – user70974
    Jul 3, 2017 at 14:34
  • 1
    \$\begingroup\$ 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"). \$\endgroup\$ Jul 4, 2017 at 2:52

78 Answers 78

1
\$\begingroup\$

Ohm, 5 bytes

D}Σd¥

Try it online!

\$\endgroup\$
1
\$\begingroup\$

tcl, 45

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

demo

\$\endgroup\$
1
  • 1
    \$\begingroup\$ You can replace 0== with 1>. \$\endgroup\$
    – Mr. Xcoder
    Jul 3, 2017 at 19:25
1
\$\begingroup\$

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!

\$\endgroup\$
2
  • \$\begingroup\$ Same byte count if you go pointfree: mod<*>(2*).sum.map(read.pure).show \$\endgroup\$
    – nimi
    Jul 4, 2017 at 15:34
  • \$\begingroup\$ Looks good, I added it in my submission. \$\endgroup\$
    – bartavelle
    Jul 4, 2017 at 15:46
1
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
4
  • \$\begingroup\$ 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. \$\endgroup\$ Jul 5, 2017 at 7:50
  • \$\begingroup\$ @KrzysztofCichoki No I can't: this is Java. The only truthy value is true. \$\endgroup\$ Jul 5, 2017 at 7:56
  • 1
    \$\begingroup\$ @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.. \$\endgroup\$
    – Laikoni
    Jul 5, 2017 at 8:13
  • \$\begingroup\$ @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. \$\endgroup\$ Jul 5, 2017 at 10:32
1
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

Java (OpenJDK 8), 40 bytes

i->i%(i+"").chars().map(k->k*2-96).sum()

Try it online!

Returns 0 as truthy value and a value greater than 0 as falsy value.

\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

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

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

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ -1 byte: $_%=eval s//+2*/rg.0 \$\endgroup\$
    – Grimmy
    Feb 6, 2019 at 22:25
1
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

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
\$\endgroup\$
1
\$\begingroup\$

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.
\$\endgroup\$
1
\$\begingroup\$

Befunge-93, 34 bytes

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

Try it online!

Prints 0 for truthy, a different integer for falsey.

\$\endgroup\$
1
\$\begingroup\$

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
\$\endgroup\$
1
\$\begingroup\$

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
\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

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
\$\endgroup\$
1
\$\begingroup\$

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)
\$\endgroup\$
1
\$\begingroup\$

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
\$\endgroup\$
1
\$\begingroup\$

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
\$\endgroup\$
1
\$\begingroup\$

Vyxal, 3 bytes

∑dḊ

Try it Online!

\$\endgroup\$
1
\$\begingroup\$

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`
}
\$\endgroup\$
1
0
\$\begingroup\$

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
\$\endgroup\$
1
  • \$\begingroup\$ @TheIOSCoder Added explanation. \$\endgroup\$ Jul 3, 2017 at 13:50
0
\$\begingroup\$

Pari/GP, 24 bytes

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

Try it online!

\$\endgroup\$
0
\$\begingroup\$

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

\$\endgroup\$
0
\$\begingroup\$

PowerShell, 39 bytes

($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.

\$\endgroup\$
0
\$\begingroup\$

Bash + coreutils + bc, 46 bytes

yes $1%\(0`echo $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.

\$\endgroup\$
0
\$\begingroup\$

Perl 5, 27 bytes

$_%(2*eval join'+',split//)

Returns 0 for Truthy and non-zero for Falsy

\$\endgroup\$
1
  • \$\begingroup\$ 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 :) \$\endgroup\$
    – Dada
    Jul 4, 2017 at 9:21
0
\$\begingroup\$

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.

Try it online! Or verify all test cases.

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
\$\endgroup\$
2
  • \$\begingroup\$ You can replace s:~ with Ab (convert to base 10) \$\endgroup\$ Jul 4, 2017 at 6:36
  • \$\begingroup\$ @Challenger5 Thanks! Edited \$\endgroup\$
    – Luis Mendo
    Jul 4, 2017 at 8:26
0
\$\begingroup\$

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)

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy