13
\$\begingroup\$

Challenge

  • Given a non-negative integer, find the sum of its digits.

Rules

  • Your program must take a non-negative integer as input.
  • Your program should output the sum of the digits of the input integer.
  • Your program should be able to handle inputs up to 10^100.

Examples

  • If the input is 123, the output should be 6 (1 + 2 + 3).
  • If the input is 888, the output should be 24 (8 + 8 + 8).
  • If the input is 1024, the output should be 7 (1 + 0 + 2 + 4).
  • If the input is 3141592653589793238462643383279502884197169399375105820974944592, the output should be 315.

Format

  • Your program should take input from standard input (stdin) and output the result to standard output (stdout).
  • Your program should be runnable in a Unix-like environment.

Test cases

Input:

123

Output:

6

Input:

888

Output:

24

Input:

1024

Output:

7

Input:

3141592653589793238462643383279502884197169399375105820974944592

Output:

315

Scoring

  • This is a code-golf challenge, so the shortest code (in bytes) wins.
\$\endgroup\$
7
  • 4
    \$\begingroup\$ I have a feeling this is a duplicate... \$\endgroup\$
    – Seggan
    Commented Feb 24, 2023 at 23:04
  • 3
    \$\begingroup\$ this is OEIS A007953 \$\endgroup\$
    – c--
    Commented Feb 24, 2023 at 23:24
  • \$\begingroup\$ Can I take it as an array with 1 element in it? \$\endgroup\$ Commented Feb 25, 2023 at 0:05
  • 2
    \$\begingroup\$ Congrats on finding a non duplicate challenge which can have an answer of 0 bytes \$\endgroup\$
    – Surb
    Commented Feb 26, 2023 at 20:40
  • 3
    \$\begingroup\$ @Surb 1 2 3 4 5 6 7 ... there's plenty more \$\endgroup\$
    – naffetS
    Commented Mar 9, 2023 at 1:33

72 Answers 72

1 2
3
1
\$\begingroup\$

I saw this and just had to give it a go with Java. I mean, I'm not going to win, but it's in good fun, right? Using loops, assuming that we can have multiple inputs, including 0:

class D{public static void main(String[]a){int s=0;for(String t:a)for(char c:t.toCharArray())s+=c-48;System.out.println(s);}}

That gives 125 bytes, but if we assume some assumptions, we can surely get it lower. If we assume one and only input, we don't have to loop, so we can shorten it down further. But if we also stream, we get:

class D{public static void main(String[]a){System.out.println(a[0].chars().map(i->i-48).sum());}}

Which clocks in at 97 bytes.

That's not too bad for Java.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Welcome to Code Golf, and nice first answer! Be sure to check out Tips for golfing in Java \$\endgroup\$ Commented Mar 3, 2023 at 2:47
  • 3
    \$\begingroup\$ I think for Java submissions is is common to put the boilerplate into the header and use lambdas. Here is a 31 byte port of your solution using this: Try it online! (note: I don't know any Java, just based this off of other answers I've seen on this site) \$\endgroup\$ Commented Mar 3, 2023 at 3:04
  • \$\begingroup\$ Yeah, but it said it should be runnable from a command line. I just wanted to make usre it went with the rules. Your suggestion does shave off a lot of scaffolding for sure. Thanks! :) \$\endgroup\$ Commented Mar 6, 2023 at 11:46
1
\$\begingroup\$

Google Sheets, 44 bytes

=QUERY(,"select "&REGEXREPLACE(A1,"\B","+"))
\$\endgroup\$
1
\$\begingroup\$

vemf, 6 bytes

ª-:48+

Input is a number literal.

try it online, uses :314159 as example

Explanation

ª      ' represent as a codepage string
 -:48  ' subtract 48 from each codepoint
       ' (so the codepoints are all now 0-9)
     + ' sum list
\$\endgroup\$
1
\$\begingroup\$

Java 8 (OpenJDK 8), 31 bytes

Very classic answer in Java

s->s.chars().map(e->e-48).sum()

Try it online!

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

Python, 42 Bytes

lambda x:sum(int(y) for y in list(str(x)))

Try this code online

\$\endgroup\$
2
  • \$\begingroup\$ -7 bytes \$\endgroup\$
    – emirps
    Commented May 15, 2023 at 16:49
  • \$\begingroup\$ lambda x:sum(map(int,str(x))) \$\endgroup\$
    – naffetS
    Commented May 15, 2023 at 23:46
1
\$\begingroup\$

C# (Visual C# Interactive Compiler), 18 bytes

s=>s.Sum(a=>a-48);

Try it online!

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

Python 3, 33 (35!) 30 bytes

-5 thanks to c--. No strings

f=lambda n:n and n%10+f(n//10)

Try it online!

\$\endgroup\$
3
  • 1
    \$\begingroup\$ 28 bytes \$\endgroup\$
    – c--
    Commented May 18, 2023 at 0:01
  • 1
    \$\begingroup\$ Since the lambda is recursive, it has to be named, so your answer is 35 bytes, or 30 bytes with my earlier suggestion. \$\endgroup\$
    – c--
    Commented Jun 4, 2023 at 15:58
  • \$\begingroup\$ I agree and have thought about this: it can not be legal to call something that is named for free. \$\endgroup\$
    – Hunaphu
    Commented Jun 4, 2023 at 23:19
1
\$\begingroup\$

Bash & coreutils, 21 bytes

fold -1|paste -sd+|bc

Try it online!

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

Java, 27 bytes

for(s=0;n>0;s+=n%10,n/=10);

Input as Integer (n)

\$\endgroup\$
2
  • \$\begingroup\$ Welcome to Code Golf, and nice first answer! \$\endgroup\$
    – The Thonnu
    Commented Jul 27, 2023 at 6:46
  • \$\begingroup\$ Do note that input can't be in predefined variables - it needs to be read from stdin/argv (if using a full program) or taken from function/lambda arguments. \$\endgroup\$
    – lyxal
    Commented Jul 27, 2023 at 12:43
1
\$\begingroup\$

Swift, 51 bytes

let s={String($0+0).reduce(0){Int("\($1)")!+$0}}
\$\endgroup\$
1
  • \$\begingroup\$ You can remove the space between i: and Int. Also, you might want to use an online testing website like Try It Online so other people can verify your code is correct. \$\endgroup\$
    – The Thonnu
    Commented Jul 24, 2023 at 2:04
0
\$\begingroup\$

Thunno 2 S, 0 bytes

Attempt This Online!

No bytes, that's right.

Thunno 2, 1 byte

S

Attempt This Online!

Alternatively, have a built-in.

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

Perl 5 -MList::Util=sum -pF, 8 bytes

$_=sum@F

Try it online!

\$\endgroup\$
1 2
3

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.