Compter la somme de tous les chiffres


38

Ce défi consiste à écrire un programme ou un script qui compte la somme de tous les chiffres compris entre 1 et un nombre donné.

Entrée, un entier positif. La sortie, la somme des chiffres de ce nombre et tous les plus petits.

Exemples:

Input: 5 
Integer Sequence: 1, 2, 3, 4, 5
Sum of Digits: 1 + 2 + 3 +4 + 5 = 15

Input: 12
Integer Sequence: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 
Sum of Digits: 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 1 + 0 + 1 + 1 + 1 + 2 = 51

Pour être clair, il s’agit de compter la somme des chiffres et non des entiers. Pour les entrées à un chiffre, ce sera la même chose. Cependant, les intrants supérieurs à 10 auront des réponses différentes. Ce serait une réponse incorrecte :

Input: 12
Output: 78

Un autre exemple, pour montrer la différence:

Input: 10

Integer Sequence: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Sum of Integers (INCORRECT RESPONSE): 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 = 55

Digit Sequence: 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 0
Sum of Digits (CORRECT RESPONSE): 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 1 + 0 = 46

Un cas de test plus grand (réponse correcte):

Input: 1000000
Output: 27000001

Règles et directives:

  • Le code soumis doit être un programme complet ou un script, pas seulement une fonction. Si le code nécessite des inclus, des importations, etc., ils doivent être inclus dans le code envoyé.
  • Le numéro doit être entré par l'utilisateur - non codé en dur. Les entrées peuvent être reçues sous forme d'argument de ligne de commande, fichier, stdin ou tout autre moyen permettant à votre langue de saisir les entrées de l'utilisateur.
  • Le code doit être capable de gérer correctement les entrées au moins jusqu’à (2^64)-1.
  • Le code ne doit générer que la somme.
  • Les programmes et scripts soumis doivent être faciles à utiliser et ne pas gaspiller en ressources informatiques (par exemple: ils ne doivent pas déclarer des tableaux incroyablement grands contenant tous les caractères). Il n'y a pas de bonus strict ni de pénalité pour cela, mais soyez de bons programmeurs.

Notation:

Le mécanisme de notation principal est basé sur la longueur du code. Les scores les plus bas sont meilleurs. Les bonus et pénalités suivants s'appliquent également:

  • -25 Bonus si votre code peut gérer tous les nombres positifs, par exemple:1234567891234567891234564789087414984894900000000
  • -50 Bonus si votre code peut gérer des expressions simples, par exemple 55*96-12. Pour pouvoir bénéficier de ce bonus, le code doit gérer les + - / *opérateurs (addition, soustraction, division, multiplication) et appliquer l'ordre des opérations. La division est la division entière régulière.
    • L'exemple donné ( 55*96-12) est évalué à 5268. Votre code devrait renvoyer la même chose pour l'une ou l'autre de ces entrées - la réponse correcte est 81393.
  • -10 Bonus si votre code est éligible au bonus -50 et peut gérer l' ^opérateur (exposant).
  • -100 Bonus si votre code se qualifie pour le bonus -50 et n'utilise pas evalou similaire pour gérer les expressions.
  • Pénalité de +300 si votre code repose sur des ressources Web.

2
Et que devrait 55*96-12revenir?
ProgramFOX

1
55 * 96-12 = 5268, devrait avoir la même sortie que celle entrée 5268
ST3 le

3
Les bonus peuvent être un peu gros, semble devenir une compétition sur le plus gros score négatif :)
Joachim Isaksson

7
@ ST3 S'il est pratiquement impossible de gagner sans les bonus, il est presque préférable de simplement en faire le nécessaire ou de valoir moins.
Cruncher

3
-1 parce que ce défi utilise l'incitatif de notation "bonus" obsolète (et affreux).
mbomb007

Réponses:


9

Perl 6: 108 - (25 + 50 + 100) + 0 = -67 points

Solution golfée (dernière ligne basée sur l'excellente solution de xfix ):

$!=get;for '*',&[*],'/',&[/],'+',&[+],'-',&[-] ->$s,&f{$!~~s:g[(\d+)$s(\d+){}]=f |@()}
say [+] (1..$!)».comb

Solution sans jeu:

my $expression = get;
for '*', &[*],
    '/', &[/],
    '+', &[+],
    '-', &[-]
-> $sym, &infix {
    $expression ~~ s:g[(\d+) $sym (\d+) {}] = infix($0, $1)
}
say [+] (1..$expression)».comb

L'étape d'évaluation fonctionne par itération sur chaque symbole *, /, +, -, trouvant quand qui se trouve entre deux nombres entiers, par ce que l' utilisation de la fonction qui représente le symbole.

Plus en détail: il prend chaque symbole (par exemple +) et la fonction infixe qu’il est censé représenter (par exemple, &[+]qui est le raccourci pour &infix:<+>et la même fonction que Perl 6 appelle lors de l’exécution 1 + 2) et effectue une substitution globale ( s:g[…] = …qui ressemble à Perl 5). s/…/…/ge), qui fait correspondre deux entiers séparés par le symbole ( (\d+) $sym (\d+)) et les substitue au résultat de la fonction infixe correspondante appelée avec ces entiers ( infix($0, $1)).

Enfin, cette expression évaluée est alimentée say [+] (1..$expression)».comb, ce que xfix explique très bien dans sa solution .

Désolé d'être si tard à la fête

EDIT: Suppression du support pour les exposants; de toute façon, c’était exactement 10 caractères et l’associativité n’était pas correcte.


C'est bien. J'aime la façon dont vous avez créé un analyseur syntaxique très simple - j'ai essayé, mais je n'ai pas réussi à faire quelque chose d'aussi court que cela. Au lieu de cela, my $gvous voudrez peut-être utiliser quelque chose de prédéclaré (je pense que cela $!pourrait fonctionner, mais je n'ai pas testé).
Konrad Borowski

@ xfix, je ne sais pas comment cela aiderait le golf. Il y a un moyen de vraiment jouer au golf, mais cela nécessite la syntaxe "infixe: [$ var]" pas encore totalement fonctionnelle: my$g=get;for <* / + -> {$g~~s:g[(\d+)$^s(\d+){}]=infix:[$^s] |@()};say [+] (1..$g)».combCela réduirait le score à 88 caractères ou -97 points
Mouq

Ohh, le $! aiderait à se débarrasser du 'mon'! Merci @xfix
Mouq

14

Mathematica 30- (10 + 50) = -30

Raccourci de 4 caractères grâce à ybeltukov.

Range@nrenvoie les nombres de 1 à n.

Integerdigits@n décompose chacun de ces nombres en ses chiffres.

Total[n,2]résume les chiffres. Le 2 doit permettre d’additionner différents niveaux, c’est-à-dire des listes de listes.

IntegerDigits@Range@#~Total~2&

Essai

IntegerDigits@Range@#~Total~2&[12]

51

IntegerDigits@Range@#~Total~2 &[1000000]

27000001


Expressions

IntegerDigits@Range@#~Total~2 &[55*96 - 12]

55*96 - 12

81393
5268

IntegerDigits@Range@#~Total~2 &[5268]

81393


IntegerDigits@Range@#~Total~2 &[55*96^2 - 12]
55*96^2 - 12

12396621
506868

IntegerDigits@Range@#~Total~2 &[506868]

12396621


Vous devriez ajouter des informations sur les arguments valides pour obtenir tous les points Brownie: D
Yves Klett

1
Je ne sais pas si je considérerais que ne pas utiliser eval
Cruncher le

3
re: Eval dans Mathematica. C'est un langage symbolique dans lequel le serveur essaie toujours de résoudre Math automatiquement comme ça. Vous devez ajouter du code supplémentaire (maintenez enfoncé []) pour l'empêcher de le faire.
Michael Stern

1
Tr@Flattenpeut être réduite à Total[...,2]: IntegerDigits@Range@#~Total~2&.
Ybeltukov

1
Don't you handle arbitrarily large int and deserve another -25?
aka.nice

12

C: 150 138 - (100+50) = -12

a,b,c;main(d){for(scanf("%d ",&a);~scanf("%c%d ",&d,&b);a=d^43?d%5?d%2?a/b:a*b:a-b:a+b);for(;a;)for(b=a--;b;b/=10)c+=b%10;printf("%d",c);}

Very shamefully stealing @Fors answer from here to do the expression evaluation: https://codegolf.stackexchange.com/a/11423/13877

Sample usage:

./a.exe <<< "5 + 7"
51

Note: the expression implementation assumes no operator precedence and consumes values as it receives them; ex, 1+2*3 = 9 rather than the typical 7.


1
This doesn't deal with operator precedence, but the question doesn't specify whether standard operator precedence should apply... ping @ST3, this should probably be clarified. Anyway, it should probably be mentioned in the answer.
FireFly

@FireFly I modified the answer to reflect this fact.
Josh

@Josh - please provide answer for 2^64 - 5
SergeyS

10

sed, 411 283 - 25 = 258

I can't be bothered to golf it more right now. :-) Not recommended for use with even remotely big integers, but technically it could deal with arbitrarily large integers (you'll likely run out of RAM pretty quickly though, since I (more-or-less have to) encode the number in unary).

s/$/x0123456789/
:l
/9$/H
:b
s/(.)(y*x\1)/y\2/
/(.)y*x\1/b b
s/(.)([xy].*)(.)\1/\3\2\3\1/
:c
s/y(.*(.))/\2\1/
/y/b c
/0$/b f
/^0*x.*9$/!b l
x
s/x[^\n]*\n//g
:d
s/(.)(.*x.*(.)\1)/z\3\2/
/[^z0]x/b d
s/0|x.*|\n//g
H;x
s/./0/g
s/$/x9876543210/
x
:e
x
b l
:f
x
s/.//
/./b e
x
s/^0+|x.*//g

Sample use

(Input lines indented for easier reading.)

  5
15
  12
51
  33
183

8

python, 55-(50+25+10) = -30

In-efficient yet shorter and also able to handle expressions.

EDIT: Thanks Wolframh and legoStormtroopr for the tricks :D

s,t=0,input()
while t:s+=sum(map(int,`t`));t-=1
print s

python, 149-(25+50+10) = 64

My first version

def d(n):
 if n/10==0:return n*(n+1)/2
 c,t=0,n
 while t/10:c,t=c+1,t/10
 p=10**c;m=n%p
 return d(m)+t*(m+1)+p*t*(t-1)/2+p*c*t*45/10
print d(input())

input:

1234567891234567891234564789087414984894900000000

output:

265889343871444899381999757086453238874482500000214

I get an overflow error when I try running your xrange solution on 1234567891234567891234564789087414984894900000000
Josh

1
@Josh got rid of xrange :D
Wasi

2
Some hints: You can replace eval(raw_input()) by input(). The while loop could be while t:s+=sum(map(int,t));t-=1.
Reinstate Monica

2
You can shorten this by just using input() instead of eval(raw_input()), as input already evals the expression! This means you can get the -10 binus for the power symbol and the -100 bonus for not using eval!!!

@LegoStormtroopr the rules say eval and similar, so I think the -100 wouldn't count
SztupY

8

Python - 108 chars minus 85 bonuses, 23 strokes, handles very very very large inputs

Most of these solutions seem to be looping over all ints less than the input and adding up all their digit sums. This works, but I feel it's inelegant, and would question whether they're truly eligible for the 25 point bonus, since I don't think they'd be able to handle the input 1234567891234567891234564789087414984894900000000 within our lifetimes. Indeed, on an input of n digits, these solutions take O(10^n) time. I chose instead to throw some maths at this problem.

#Returns the sum of all digits in all x-digit numbers
def f(x):
    return x*(10**(x-1))*45

#Returns the sum of all numbers up to x
def g(x):
    return x*(x+1)/2

#Solves the problem quickly
def magic(x):
    digits = [int(y) for y in list(str(x))]
    digits.reverse()
    total = 0

    for (sig, val) in enumerate(digits):
        total += (10**sig)*g(val-1) + val*f(sig) + val + (val*10**sig)*sum(digits[sig+1:])
    return int(total)

The set of all x digit numbers is isomorphic to the set {0,1,2,3,4,5,6,7,8,9}^x. For a fixed (n,sig) there are x different values for sig, 10^x-1 points with the sigth index set to n, and the sum of all digits 0-9 is 45. This is all handled by f.

g is something we're probably all familiar with

magic takes all the digits in the input number, and iterates over them from least to most significant. It's easiest to track this with an example input, say 1,234,567.

To deal with the range 1,234,567-1,234,560, we must add up all digits from 1 to 7, and add on 7 times the sum of the other digits, to deal with all numbers greater than 1,234,560. We now need to deal with the remainder.

To deal with the range 1,234,560-1,234,500, we add on the 6 (val), and drop the upper limit to 1,234,559. In making the remainder of the drop, we'll see every single-digit number 6 times (val*f(sig)). We'll see all the numbers from 0 to 5 exactly 10 times each ((10**sig)*g(val-1)). We'll see all the other digits in this number exactly 60 times ((val*10**sig)*sum(digits[sig+1:])). We have now dealt with all numbers strictly greater than 1,234,500. The same logic will apply inductively across all significances.

Golfing this, with thanks to WolframH, reduces this solution to

d=map(int,str(input()))
print sum(v*(10**s*((v-1)/2+sum(d[:~s]))-~s*9*10**s/2)for s,v in enumerate(d[::-1]))

And the sum of the digit sums of all integers up to 1234567891234567891234564789087414984894900000000 is 265889343871444927857379407666265810009829069029376

The largest number I've managed to throw at the golfed version is 10^300, at which point the floats start overflowing and numeric instability starts to cause problems. With a quick square-and-multiply exponentiation function, this problem would vanish.

And LaTeX support would be really useful...


Nice. I tried to attack this problem with maths a while ago, but got stuck. I'll have to go over this carefully later and have to think about how it works.
FireFly

Nice answer! It is similar into the way I counted, that would be if input is 1000000 :)
ST3

1
+1 for using maths. However, I get 2.65889343871e+50, which is a floating point approximation of the real solution. Apparently you printed int(t) instead of t as in the code you gave. That is wrong; the real solution is 265889343871444899381999757086453238874482500000214. Just avoid using floats, i.e. replace **(x-1) by the shorter **x/10.
Reinstate Monica

1
Golfing this a little bit more. It's clear that the only global needed is d (because it's used twice). Eliminating the others (and using some tricks) one arrives at d=map(int,str(input()))\nprint sum(v*(10**s*((v-1)/2+sum(d[:~s]))-~s*9*10**s/2)for s,v in enumerate(d[::-1])) (108 characters). Runs fine on inputs of any size (like int("1"*1000)).
Reinstate Monica

1
@ymbritt 10**-1 is 0.1, and from there on everything gets turned into floats. 1/10 is 0 (integer division), and everything can stay ints.
Reinstate Monica

8

TI-BASIC, 137 - (50 + 10 + 100) = -23

Input A:Disp cumSum(randIntNoRep(1,A))→L₁:"?:For(A,1,dim(L₁:Ans+sub("ABCDEFGHIJKLMNOPQRSTUVWXYZ",L₁(A),1:End:Disp sub(Ans,2,length(Ans)-1

Input handles numbers up to 1E100 and automatically evaluates. Can handle expressions.

Although it is an insanely large array, I'm not wasting computer resources (this is run from a calculator).


1
best answer for this question I think. using a calculator language to write a code golf answer for adding numbers together. so cool!
Malachi

1
@Malachi As I always say, when code golf = math, it's time to pull out the calculator.
Timtech

2
My version that allowed numbers up to 9E99 wasn't good enough apparently, so I don't think you can count that bonus. Also, I'm pretty sure you'll have to count the input as "with eval", per Carraher's Mathematica answer.
FireFly

1
Agree with FireFly, bonus of not using eval shouldn't be taken.
ST3

3
How is a calculator not a computer?
David Conrad


6

C, 77 74

n,v,i;main(){scanf("%d",&n);for(;i||(i=n--);i/=10)v+=i%10;printf("%d",v);}

C, 150 124 - 25 = 99

Here is an alternative version that should technically be eligible for the 25 bonus for "any" positive integer, but it's impractically slow since the algorithm is linear-time in its input. Regardless, it was fun to write. Manually subtracts a number read in as ASCII characters. This version is 150 characters. (Now with horrible, argument-thrashing, loopful code!)

n,v;main(int n,char**a){char*p;do{for(p=a[1];*p>47;p++)v+=*p-48;for(;*--p==48;)*p=57;
p[0]--;}while(p>=a[1]);printf("%d",v);}

C, 229 224 - (50 + 100) = 74

Expression-handling variation. Implements operator precedence according to typical rules: / * - +. Limited to 97 tokens = 48 terms.

#define F(X,Y)for(q=n+1;q+1!=p;)*q-X?q+=2:(q[-1]Y##=q[1],memmove(q,q+2,(p-q)*4))
n[99],*p,*q,v,i;main(){for(p=n;~scanf("%d%c",p,p+1);)p+=2;F('/',/);F('*',*);
F('-',-);F('+',+);for(;i||(i=n[0]--);i/=10)v+=i%10;printf("%d",v);}

All positive integers mean, that it should handle even longer then 99 digits numbers.
ST3

@Firefly cool algorithm to work on numbers larger than the builtin numerics!
Josh

5

GolfScript 18 - 50 = -32

~),{`+}*' '*~]{+}*

Explanation: Suppose input is "12":

~), # turn input into integer, increment, and then turn into an array of all numbers less than or equal to input.  

Stack is [0,1,2,3,...,12].

{`+}* # fold string concatenation across the array

Stack is "01234...9101112".

' '* # join a space between all characters

Stack is "0 1 2 ... 1 0 1 1 1 2".

~] # evaluate the stack into an array.  No `[` is necessary since the stack is otherwise empty.

Stack is [0,1,2,...,9,1,0,1,1,1,2].

{+}* # fold addition across the new array

Stack is 51, as desired.

The input here could be any valid GolfScript expression, which can include exponents. For example:

echo "5 5 + 2 * 8 -" | ruby golfscript.rb h.gs
-> 51

Since 2(5 + 5) - 8 = 12. I think this should qualify for the bonus, but maybe it was expected to be only if in normal form, not the reverse Polish notation of GolfScript.


Does it support ^ as well?
SztupY

It supports exponentiation in GolfScript syntax, which is ?
Ben Reich

You do not get bonus 10, because program must support ^, not ? or pow and etc.
ST3

@ST3 As you wish!
Ben Reich

4

Ruby, 37 - 50 = -13

Double eval, all the way across the sky! As with the other Ruby solutions, I think this should theoretically be able to work with arbitrarily large numbers, but execution time would be... dire.

p eval [*1..eval(gets)].join.chars*?+

Older version (49 - 50 score)

p"#{[*1..eval(gets)]}".chars.map(&:to_i).inject:+

Assuming the 10 character bonus actually requires the character for exponentiation to be a caret, the shortest way I could think to add that is:

.gsub ?^,'**'

Which costs more characters than the bonus would give.


You can remove a few chars: p"#{[*1..eval(gets)]}".chars.map(&:to_i).inject :+
SztupY

@SztupY good call, thanks! I don't use & nearly enough in golf. In fact, you don't need the space between inject and :+ either.
Paul Prestidge

4

Perl 6 (28 - 75 + 0 = -47 bytes)

say [+] (1..get.eval)».comb

It can handle all positive numbers (however, big ones will take a long while, because currently Perl 6 implementations are slow, but Perl 6 supports big integers natively). It uses eval, in order to implement a simple calculator (five character penalty for fifty characters is worth it). It's slow just because current implementations are slow, but in theory, it should be fast enough (when Perl 6 implementations improve, that is). Also, surprisingly, I win with the Mathematica (for now).

» in this code is actually not needed, but I put it here for performance reasons (otherwise, program would allocate entire string. The reason why it's here is that Perl 6 doesn't have infinite strings, but it does have infinite lists.

Anyway, you may ask how this code even works. Well, I'm going to pass it part by part.

  • get.eval

    This gets one line (get function), and evaluates it (eval method).

  • 1..get.eval

    After that, Perl 6 prepares a range object, from 1 to evaluated value. This is a range, so nothing huge is allocated.

  • ».comb

    .comb method splits string onto characters (unless called with an argument). For example, 'cat'.comb returns 'c', 'a', 't'. » maps the list elements, so .comb is ran on its every item - not only on the list itself (for example, (4, 9)».sqrt gives 2, 3). This also doesn't allocate more than needed, because Perl 6 has infinite lists (like Haskell, for example).

    » character actually not needed, as .comb can be used directly on the list, but this involves implicit string coercion (and Perl 6 doesn't have infinite strings, so this would waste memory). For example, 1, 2, 3 list after conversion to the string returns 1 2 3. For Perl 6, a space is a perfectly fine number meaning 0, so the code would work, even with such conversion. However, it would abuse computing resources.

  • [+]

    This is a reduce operator. Basically, between [], you can put an operator to use, in this case +. The list after reduce operator is reduced, so [+] 1, 2, 3 is 1 + 2 + 3, which is 6. Perl 6 uses separate operators for numbers and strings, so it won't be considered to be concatenation.

  • say

    Finally, say outputs the result. After all, you want to see the final result, don't you?


Hmmm... [+] 1,2,3,4,5,6,7,8,9,10 is 1+2+3+4+5+6+7+8+9+10, am I right?
ST3

@ST3: Yes. Reduce operator can be used in many interesting ways in Perl 6. For example, > can be chained, so 3 > 2 > 1 is true. The same property applies to reduce operators, so [>] 3, 2, 1 is still true, as it means 3 > 2 > 1 - [>] can be used to determine if numbers are in descending order.
Konrad Borowski

couldn't you use get.Int instead of eval ? Does it need math expressions ?
Ven

@user1737909: "-50 Bonus if your code can handle simple expressions". Also, Perl 6 doesn't need casting by design (aside of few rare edge cases, like sort without comparison method argument).
Konrad Borowski

4

Perl 31 - No bonuses

map{s/./$%+=$&/ge}0..<>;print$%

Sample output:

perl -e 'map{s/./$%+=$&/ge}0..<>;print$%'
1000000
27000001

Perl 5 with -p, 50 - 28 bytes: -22

map$\+=$_,/./g for 1..eval}{

Try it online!


3

J, 22

([:+/[:"."0[:":>:@:i.)

Explanation

Evaluation proceeds from right to left.

i. n -> 0 1 2...n-1

>: n -> n+1

": numbers -> 'numbers'

"."0 -> (on each scalar item) apply ". -> '123' -> 1 2 3

+/ -> sum

Downvoter needs to explain their objections to this answer. I've just tried it and, while it doesn't earn any bonuses, it works just fine as far as I can see.
Gareth

Actually, having looked at the top answer, this one also seems to earn the expressions and power operator bonuses for a score of 22-60 = -38.
Gareth

This +/,10#.inv>:i. would be shorter. But it still a function and not a complete program as OP asked.
swish

@Gareth Bonuses don't apply to this answer, because you would just write expressions inside code and not as input.
swish

1
@swish That's what I thought at first, but the Mathematica answer seems to work much like this.
Gareth

3

R, 64 - (50 + 10) = 4

sum(utf8ToInt(paste(0:eval(parse(t=scan(,""))),collapse=""))-48)

When this is run, the user is asked for input.


Old version (cannot handle expressions): 46 characters:

sum(utf8ToInt(paste(0:scan(),collapse=""))-48)

It occurs to me that codegolf is wildly biased towards languages with single-symbol functions. This solution would be considerably shorter if we predefined u<-function(x) utf8ToInt(x) and so on.
Carl Witthoft

@CarlWitthoft This is true. But the predefinition also counts for the number of characters. By the way: It's sufficient to have u <- utf8ToInt without function. This can be helpful for code golf if the function is used multiple times.
Sven Hohenstein

so if I create a Rcheatcodegolf package, is it legal to use the predefined functions in that package? :-)
Carl Witthoft

@CarlWitthoft Yes, packages can be used. Of course, the package should not be written for the task. But if it includes short names for functions only, it's ok.
Sven Hohenstein

3

Batch - (181 - 50) - 131

Just for a bit of fun.

@set/av=%1
@setLocal enableDelayedExpansion&for /L %%a in (1,1,%v%)do @set a=%%a&powershell "&{'%%a'.length-1}">f&set/pb=<f&for /L %%c in (0,1,!b!)do @set/as+=!a:~%%c,1!
@echo !s!

I'll make it a bit more readable:

@set /a v=%1
setLocal enableDelayedExpansion
for /L %%a in (1,1,%v%) do (
    @set a=%%a
    powershell "&{'%%a'.length-1}">f
    set /p b=<f
    for /L %%c in (0,1,!b!) do @set /a s+=!a:~%%c,1!
)
@echo !s!

Old method uses for loop to get output of powershell command, as opposed to writing to and reading from a file:

@set /a v=%1
@setLocal enableDelayedExpansion&for /L %%a in (1,1,%v%)do @set a=%%a&for /F usebackq %%b in (`powershell "&{'%%a'.length-1}"`)do @for /L %%c in (0,1,%%b)do @set /a s+=!a:~%%c,1!
@echo !s!

Set the input to a variable - v - using /a to accept arithmetic expressions.
Unfortunately enabling delayed expansion was necessary.
Use a for loop to count from 1 to the inputted value - v.
In order to handle numbers greater than 9, I had to use powershell to get the length of the string then use another for loop to split that string up, and add it to the sum - s.
You could change the name of powershell.exe to p.exe under C:\WINDOWS\System32\WindowsPowerShell\v1.0\ then call it with just p "&{'%%a'.length-1}, saving 9 bytes. But that's not really in the spirit of it.

H:\>sumof.bat 12
51
H:\>sumOf.bat (55*96-12)
81393

Left that second one running while I took my lunch break.

I can't really test it with numbers that are too much larger than this due to how slow it is. However it should work for fairly large numbers. 2147483647 is the largest number it will take (maximum 32 bit integer) before giving the following error -

H:\>sumOf.bat 2147483648
Invalid number.  Numbers are limited to 32-bits of precision.

This of course disqualifies me from the challenge.


1
Nice solution! There are a few ways to golf this down. 1. You can get rid of the temporary variable v and use %1 directly. 2. You can subtract 1 within your PowerShell script rather than the lengthy @set /a b=%%b-1 which saves you a bunch. With those changes, I have it down to 211 from the original 240. :-)
Mark

Oops, I see now why you kept your temp variable (for the bonus points). The PowerShell tip still stands, though...
Mark

Well spotted, Thanks. Will change that now.
unclemeat

Batch won't work. It's limited to (2^31)-1 (signed 32-bit integer). The challenge requires handling of inputs up to (2^64)-1 (unsigned 64-bit integer, but the output for that value would overflow it). This is where PowerShell has a distinct advantage - its [decimal] type allows for values up to (2^96)-1.
Iszi

1
I will give Batch some good credit for defaulting to integer division, though. That's something PowerShell is missing entirely.
Iszi

3

Dyalog APL, 9 – 160* = -151

+/⍎¨∊⍕¨⍳⎕

Try it online!

get evaluated input
 e.g. "7+5" gives 12

indices 1 ... n
[1,2,3,4,5,6,7,8,9,10,12]

⍕¨ format each number into string
["1","2","3","4","5","6","7","8","9","10","11","12"]

enlist (flatten)
"123456789101112"

⍎¨ execute each character (yields list of single digit numbers numbers)
[1,2,3,4,5,6,7,8,9,1,0,1,1,1,2]

+/ sum  51


* Scoring

-50 bonus as it even accepts expressions as input. The expression must be valid APL, which is acceptable according to OP.

-10 bonus because because it also handles the ^ (* in APL).

-100 bonus because expression input is handled without explicit usage of eval (i.e. in APL).


Are you sure the -100 bonus is added here? Because it states "-100 Bonus if your code qualifies for the -50 bonus and does not use eval or similar to handle expressions." Since ⍎¨ seems to execute each character one by one, it's kinda the same as an eval (except it executes the characters one by one instead of all at the same time like eval does).
Kevin Cruijssen

@KevinCruijssen Yes, as it does not use eval or similar to handle expressions. ⍎¨ is only used to convert digits to integers, not to handle expressions.
Adám

Ah wait, I looked at your explanation incorrectly. But isn't the kinda an input+eval builtin then, or is eval always done implicitly when expressions are input?
Kevin Cruijssen

1
@KevinCruijssen always takes an expression as input, evaluates it, and returns its result. So to input a string, you'd have to put quotes around it. The fact that a related built-in () returns the input as raw text shouldn't matter (especially since the symbols indicate that is the primary input method, and is a special variant), as otherwise getting the bonus would require implementing a math evaluator — an entirely different task than the main one. I don't like bonuses, and the -100 one is just silly or had APL in mind, but imho, it does seem an exact fit for the bonus.
Adám

If is indeed the normal way of getting input and automatically handles expressions, I indeed see it fit into the bonus as well, so +1 from me. Bonuses are silly these days anyway, but nice way of utilizing them to minimize your score.
Kevin Cruijssen

2

C# (161)

using C=System.Console;using System.Linq;class X{static void Main(){C.WriteLine(Enumerable.Range(1,int.Parse(C.ReadLine())).SelectMany(i=>i+"").Sum(c=>c-48));}}

Pretty

using C = System.Console;
using System.Linq;

class X
{
    static void Main()
    {
        C.WriteLine(
            Enumerable.Range(1, int.Parse(C.ReadLine()))
                .SelectMany(i => i + "")
                .Sum(c => c - 48)
            );
    }
}

2

Python3+Bash (78 - 185 = -107)

python3 -c"print(sum(sum(map(int,str(x+1)))for x in range(int(${1//^/**}))))"
  • can handle all positive number
  • can handle expressions with + - / * operation
  • can handle ^ (power) operator.
  • can handle expressions, without eval or similar¹

If the result of expression is not integer, it will be truncated first. If the result of the expression is negative, the result is undefined.

Use it like:

bash golf.sh "12 + (42 / 3 + 3^4)"

1: unless you count invoking Python from Bash as such, but I don't think it is the case. If you think that it actually is, then the adjusted score is -7.


I would say that if you didn't write an expression evaluator, then you're using something equivalent to eval. But I'm not the OP, so good luck!
Tobia

Agree with @Tobia, no bonus for expression evaluator.
ST3

2

Java, 254

class T
{
    public static void main(String[] a)
    {
        long target = 10, count = 0;
        String[] digits = new String[50];
        for (long i = 1; i <= target; i++)
        {
            digits = String.valueOf(i).split("(?!^)");
            for (int j = 0; j < digits.length; j++)
                if (digits.length > j)
                    count += Integer.parseInt(digits[j]);
        }
        System.out.println(count);
    }
}

Handles expressions. Give whatever expression you desire in target. Handles until the length long can handle. If you clean up taking off all spaces into one line, and no statement to print, it counts to 254 chars (considering the long long words based Java programming).

PS: This is a complete program, not just logic. Words count given for the program, not just the logic.


2

Java (JDK8), 272

My first challenge I'm in, suggestions are welcome =)

import java.util.*;import java.util.stream.*;class C{public static void main(String[]a){System.out.print(Arrays.asList(IntStream.range(1,new Integer(a[0])).mapToObj(s->s+"").collect(Collectors.joining()).split("")).stream().map(Integer::valueOf).reduce(0,Integer::sum));}}

Indented:

import java.util.*;
import java.util.stream.*;

class C {

   public static void main(String[] a) {
     System.out.print(Arrays.asList(IntStream.range(1,new Integer(a[0]))
            .mapToObj(s->s+"")
            .collect(Collectors.joining())
            .split(""))
            .stream()
            .map(Integer::valueOf)
            .reduce(0,Integer::sum));
  }
}

+1 as everyone who does golf code challenge in java deserves it, but seems like Stream API does not give you advantage while you are golfing. Ill bet if you rewrite your solution and you will use loops instead streams it will be shorter.
user902383

2

CJam, 9 - 25 = -16

CJam is a few months younger than this challenge, so this is not eligible for the green checkmark. Furthermore, this isn't beating Perl in the first place. ;) I quite liked the approach though, so I wanted to post it anyway.

l~),s:~:+

Test it here.

The idea is to create a range from 0 to N. This range is then converted to a string, which just concatenates the integers back to back. For N = 12, we'd get

"0123456789101112"

Then each character is converted to a integer with :~ (yielding an array of integers), and then summed up with :+. CJam can deal with arbitrarily big integers.


2

Python 3 + astor, 1017 1007 bytes - (25 + 50 + 100) = Score: 842 834

saved 10 bytes by removing ts and changing p

edit: I am unable to test the ridiculously long integer (1234567891234567891234564789087414984894900000000) [hangs my computer] but from my knowledge, Python 3 supports arbritrarily long integers.

This implementation uses abuses AST. I wouldn't consider abusing AST as "eval or similar".

from ast import*
from astor import*
nt,bo,m,d,a,s,n,p,ty=NodeTransformer,BinOp,Mult,Div,Add,Sub,Num,map,type
class M(nt):
    def visit_BinOp(t,z):
        if ty(z.left)==bo and ty(z.right)==bo:return bo(t.visit_BinOp(z.left),z.op,t.visit_BinOp(z.right))
        if ty(z.left)==bo:return bo(t.visit_BinOp(z.left),z.op,z.right)
        if ty(z.right)==bo:return bo(z.left,z.op,t.visit_BinOp(z.right))
        if ty(z.op)==m:return n(z.left.n*z.right.n)
        if ty(z.op)==d:return n(z.left.n/z.right.n);return z
class A(nt):
    def visit_BinOp(t,z):
        if ty(z.left)==bo and ty(z.right)==bo:return bo(t.visit_BinOp(z.left),z.op,t.visit_BinOp(z.right))
        if ty(z.left)==bo:return bo(t.visit_BinOp(z.left),z.op,z.right)
        if ty(z.right)==bo:return bo(z.left,z.op,t.visit_BinOp(z.right))
        if ty(z.op)==a:return n(z.left.n+z.right.n)
        if ty(z.op)==s:return n(z.left.n-z.right.n);return z
class S(nt):
    def visit_Num(t,z):return n(sum(p(int,list("".join(p(str,range(1,z.n+1)))))))
print(to_source(S().visit(A().visit(M().visit(parse(input()))))))

Too lazy to write ungolfed, so I'll give you an explanation of the classes:

M(NodeTransformer|nt) - converts multiplication and division into their results.
A(NodeTransformer|nt) - converts addition and subtraction into their results.
S(NodeTransformer|nt) - converts numbers into their sum of digits via the Pythonic (naïve) way.

The last line just executes these classes in the appropriate order on the input, to preserve order of operations, and prevent unwanted behavior.

Example usage ($ or > means user input) and by the way, the actual program takes input only once:

$ python3 summer.py
> 5
15
> 10
46
> 12
51
> 1000000
27000001
> 55*96-12
81393

This is amazing, but yet horrifying. Not sure if it's allowed (to knowingly use a long solution), but 10/10 from me.
Rɪᴋᴇʀ

@EᴀsᴛᴇʀʟʏIʀᴋ Why isn't it allowed to knowingly use a long solution? I see no problem. At least I'll beat solutions with 842+ score ;)

They are supposed to be competetive answers, meaning show effort. Also, DELETE THAT COMMENT. SE LIMIT FOR AGE IS 13!!! You should probably wait until you are legally allowed to be on. Due to COPPA (google it), you need to be 13 to use the internet like this.
Rɪᴋᴇʀ

@EᴀsᴛᴇʀʟʏIʀᴋ Now I'm curious, who was that user?
cat

1
@cat An arabic name I couldn't pronounce? Probably nuked account.
Rɪᴋᴇʀ

1

C# (108)

int c(int n){return string.Join("",Enumerable.Range(1,n).Select(i=>i+"")).ToArray().Select(c=>c-'0').Sum();}

Pretty

int c(int n)
{
    return string.Join("", Enumerable.Range(1, n).Select(i => i + "")).ToArray().Select(c => c - '0').Sum();
}

3
It is not a valid answer as it is function and char count is kind a big
ST3

1
You don't need the ints; in C, everything defaults to int... Oh, it's C#.
wizzwizz4

1

Ruby -> 83-50 = 33

p (1..eval(gets.chomp)).each.inject{|c,e|c+e.to_s.chars.map{|x|x.to_i}.inject(:+)}                     

"To test" version:

module Math
  class CountSum
    def sum(number)
      (1..number).each.inject do |c, e|
        c + e.to_s.chars.map{ |x| x.to_i }.inject(:+)                                                  
      end
    end
  end
end 

Tests results

$ rspec sum_spec.rb  --format doc --color

Math::CountSum
  #sum
    single digit number
      when 5, should return 15
    double digit number
      when 12, should return 51
    arbitrary number
      when 1000000 should return 27000001

Finished in 5.34 seconds
3 examples, 0 failures

1

C# (80)

Its my another attempt.

double c(int n){double s=0;while(n>0)foreach(var c in n--+"")s+=c-48;return s;}

Pretty

double c(int n)
{
    double s = 0;
     while (n > 0)
        foreach(var c in n--+"") 
            s += c - 48;
    return s;
}

Is the whitespace between n-- and + needed? I don't think it is in other C-style languages.
FireFly

1
Does this work with the given range? The result for 2^64-1 doesn't fit in 64 bits.
marinus

2
It is not a valid answer as it is function and char count is kind a big.
ST3

@marinus Can you give us the result for 2^64-1 so that we can know what range we need to work with? I dare not test it in my language (PowerShell) since the processing time would be enormous.
Iszi

@Iszi: I'm not going to actually run it, but you can do some math: 1) the average value of a digit is 4.5; 2) the average sum of 20 digits is 90 (2^64 has 20 digits); so the expected value will be around 90 * 2^64 ≈ 1.66*10^21. So you'd need at least 71 bits, at most 72.
marinus

1

Ruby 69-50 = 19 (or -4)

This can definitely be golfed together but here is the first fifth try

p (1..eval(gets)).inject{|i,s|i+=s.to_s.chars.map(&:to_i).inject :+}

It also works for all numbers but is very slow for them as it runs slower than O(n), so I wouldn't add the -25. If slowness is fine, then it would be -4 though

Ruby 133-50-25 = 58

This is the faster version, that runs in less-than O(n) time (and uses actual math!), so it can provide results for large integers fast, thereby I added the -25:

n=eval(gets);p (d=->n,l{k=10**l;c,r=n.to_s[0].to_i,n%k;n<10?n*(n+1)/2:c*45*l*k/10+k*(c*(c-1)/2)+(r+1)*c+d[r,l-1]})[n,n.to_s.length-1]

We write exactly the same code (you golfed a little more)!
Beterraba

@Beterraba yup, and almost the same time, but you were a bit faster, so I have to figure out something different :)
SztupY

1

Haskell, 74-25=49

main=getLine>>=print.sum.map(\c->read[c]).concatMap show.(\x->[0..x]).read


Using interact and the fact that >>= for lists is the same as flip concatMap, you can golf this down to 63 chars like this: main=interact$show.sum.map(\c->read[c]). \x->[0..read x]>>=show
Flonk

One more byte to save: \c->read[c] is read.(:[])
nimi

1

ECMAScript 6, 86 - 50 = 36

for(s="",i=eval(prompt());i;s+=i--)alert(s.replace(/\d/g,c=>Array(-~c).join()).length)

One character less: for(i=eval(prompt(s=""));i;s+=i--)alert(s.replace(/\d/g,c=>Array(-~c).join()).length).
Toothbrush

Quite a bit smaller (you don't need the .join()): for(i=eval(prompt(s=""));i;s+=i--)alert(s.replace(/\d/g,c=>Array(-~c)).length). 78 - 50 = 28!
Toothbrush

1

R (72 points)

f=function(n) sum(as.integer(strsplit(paste0(1:n,collapse=""),"")[[1]]))

Output:

> f(5)
[1] 15
> f(12)
[1] 51
> f(1000000)
[1] 27000001

In these challenges do you need to explicitily write "f=function(n) " or just the function with n?
skan

@skan, it depends on requirements. Usually it is not required to have an explicit function.
djhurio
En utilisant notre site, vous reconnaissez avoir lu et compris notre politique liée aux cookies et notre politique de confidentialité.
Licensed under cc by-sa 3.0 with attribution required.