21
\$\begingroup\$

Input:

An integer

Output:

Sum of the input itself + the length of the input + each individual digit of the input.

nr + nr-length + {sum of digits} = output

Examples:

Input: 99
Output: 99 (nr) + 2 (nr-length) + (9 + 9) (digits) → 119

Input: 123
Output: 123 + 3 + (1 + 2 + 3)132

Challenge rules:

  • The input can also contain negative input, which are resolved special. The -/minus-sign is also +1 for the length, and is part of the first digit.
    For example:

    Input: -123
    Output: -123 + 4 + (-1 + 2 + 3)-115

  • You can assume that the input nor output will ever be outside the range of an (32-bit) integer.

General rules:

  • This is , so shortest answer in bytes wins.
    Don't let code-golf languages discourage you from posting answers with non-codegolfing languages. Try to come up with an as short as possible answer for 'any' programming language.
  • Standard rules apply for your answer, so you are allowed to use STDIN/STDOUT, functions/method with the proper parameters and return-type, full programs. Your call.
  • Default Loopholes are forbidden.
  • If possible, please add a link with a test for your code.
  • Also, please add an explanation if necessary.

Test cases:

87901 → 87931
123 → 132
99 → 119
5 → 11
1 → 3
0 → 1
-3 → -4
-123 → -115
-900 → -905
-87901 → -87886

Semi-related: Count Sum of all Digits

\$\endgroup\$
  • \$\begingroup\$ I think that with the negative numbers, for example -123 the sum chain should be (-1 + 1 + 2 + 3) instead of (-1 + 2 + 3), right? \$\endgroup\$ – Yytsi Jun 30 '16 at 16:36
  • \$\begingroup\$ @TuukkaX Nope, it should be -1 + 2 + 3. For this challenge I choose to merge the -/minus-sign to the first digit as one negative digit to make it a bit more interesting. \$\endgroup\$ – Kevin Cruijssen Jun 30 '16 at 17:36

33 Answers 33

1
\$\begingroup\$

Add++, 15 bytes

L,bU"+"jvAbLAvs

Try it online!

How it works

L,		; Create a lambda function. Takes a string argument
		; Example argument: 	['123']
	bU	; Unpack;	STACK = ['1' '2' '3']
	"+"j	; Join with +;	STACK = ['1+2+3']
	v	; Evaluate;	STACK = [6]
	AbL	; Arg length;	STACK = [6 3]
	Av	; Int argument;	STACK = [6 3 123]
	s	; Sum;		STACK = [132]
\$\endgroup\$
0
\$\begingroup\$

C# 77 bytes

Check it here

Golfed:

int r(int n){return n+(n+"").Length+(n+"").Select(c=>int.Parse(c+"")).Sum();}

Ungolfed:

 public int r(int n)
 {
     var t = n + (n+"").Length;
     t += (n + "").Select(c => int.(c + "")).Sum();
     return t;
 }
\$\endgroup\$
  • \$\begingroup\$ Hi, welcome to PPCG! Although it is quite a bit shorter than both other C# answers so far, it fails to comply to the -/minus rule: -123 -> -123 + 4 + (-1 + 2 + 3) -> -115. Also, one small tip for your current code, you can remove the spaces between c=>int. Again welcome, and enjoy your stay. :) \$\endgroup\$ – Kevin Cruijssen Sep 20 '16 at 12:18
  • 2
    \$\begingroup\$ Sure this complies with ”you are allowed to use STDIN/STDOUT, functions/method with the proper parameters, full programs”? Looks like a snippet using a variable appearing from nowhere. \$\endgroup\$ – manatwork Sep 20 '16 at 12:39
  • \$\begingroup\$ As it stands, this is invalid and needs to be fixed or deleted. \$\endgroup\$ – Rɪᴋᴇʀ Sep 20 '16 at 13:59
  • \$\begingroup\$ I tried to fix my answer, is it still invalid? \$\endgroup\$ – Quintonn Sep 20 '16 at 19:33
-1
\$\begingroup\$

Pyt, 7 bytes

ĐąŁ⇹ĐŚƩ

Try it online!

Shortest as of now!

Đ         duplicate
 ąŁ       convert to array of digits, pop, and push length
   ⇹Đ     swap top two values and duplicate
     ŚƩ   sum of digits and sum everything on stack
\$\endgroup\$
  • 2
    \$\begingroup\$ Hi. This seems to fail for all negative test cases. \$\endgroup\$ – Kevin Cruijssen Feb 18 '18 at 11:05

Your Answer

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

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