# Write a program to print the sum of the ascii codes of the program

Your task is to write a program to print the sum of the ASCII codes of the characters of the program itself. You are not allowed to open any file (Any input such as command line arguments, standard input or files is prohibited).

The program that prints the lowest number (i.e. has the lowest sum of ASCII codes) wins.

Here is an example (not the shortest) of such a program written in C:

#include <stdio.h>
int main(){
printf("4950");/*i*/
return 0;
}


(no new line after })

• It'd be interesting to see a quine solution: one that produces and sums its own bytes. – Joey Adams Jun 21 '11 at 19:39
• Not very challenging imho. It can be trivially brute-forced, especially for languages that simply dump tokens, such as PowerShell, Golfscript, etc. – Joey Jun 21 '11 at 20:32
• I downvoted and the comment gives the reason, why. As I have noted before, I don't consider the number of answers an indication of quality. Just because it can be easily solved does not make this an interesting or even challenging challenge. My opinion, at least. – Joey Jun 22 '11 at 14:00
• Wait. What? On one hand you complain that the question is easy and not interesting and then you downvote me (-2 for me). On the other hand you post 25% of answers on this question (+70 for you). – Alexandru Jun 22 '11 at 15:28
• So you see this as a reputation battle? Fine, I can easily change my answers to CW. In a way the large number of answers was a bit of a protest and to show that it's trivial to churn out lots of answers. Compare this for example to the task that wanted the alphabet four times. Also my answering self (bound to the task specifications) and my commenting self (hoping to improve general site quality) are often fairly separate. Anyway, happier now that I deleted them? They were still valid answers, though. – Joey Jun 22 '11 at 20:09

## wc, prints 0

Someone said "cat-like languages", so...

An empty file:

Execute with wc -c file.wc. At 0 bytes, I think this is the winner in the 'not really a programming language' category.

Also

## cat, prints 80 (base 13)

80


No terminating newline, the number 8013 is equivalent to 104 in decimal. You can go shorter with 6017 (102 dec), but I figured "base 13" would be worth more geek points.

EDIT: New wc example, this one can be run as a program.

#!/usr/bin/wc
ÿÿzw17


(As encoded in Latin-1 - the ÿ is a byte with value 255)

Sum of bytes is 2223, output is:

  2  2 23 ./w

• But wc should read a file, containing a 0-byte to produce Number 0, not an empty one. Empty is not Null. – user unknown Jun 22 '11 at 13:40
• user: One might argue that the sum of values in an empty set is still 0. Nevertheless, wc -c is forbidden in the question anyway. – Joey Jun 22 '11 at 13:53
• The only clause which seems to do so is "Any input such as command line arguments ... is prohibited" so leave off the -c, then it prints 0 0 0 (if the file being passed as a file prohibits it, then all scripting languages are also forbidden) – Random832 Jun 22 '11 at 15:02
• +1 for wc, -1 for base cheating, +1 for making jokes in base 13. – user475 Jun 22 '11 at 16:16
• I would say wc is an application, not a language. – Thomas Eding Jul 24 '11 at 7:34

## PHP, m4, and other cat-like languages: 150

150


Found this solution using a simple Haskell program to brute-force it:

f :: String -> Bool
f s = (read s :: Int) == (sum . map fromEnum) s

main = mapM_ print [filter f $sequence$ replicate n ['0'..'9'] | n <- [1..10]]

• I guess this is the absolute shortest. – Alexandru Jun 21 '11 at 19:57
• @Alexandru: Not necessarily. There may be program that is one or two characters long that correctly prints the sum using some built-in functions, say like 5! in J. – mellamokb Jun 21 '11 at 21:24
• @mellamokb The sum of the ASCII characters in 5! is 86, not 125. – Peter Olson Jun 21 '11 at 22:53
• @Peter: Indeed, I was showing an example of the type of solution that might be smaller than 150, but not an actual solution. I haven't found one yet (and btw, 5! is 120, not 125) :-) – mellamokb Jun 21 '11 at 22:56

## Brainf*ck, 255

-.¤


This will not print the number 255, but rather the 255th ASCII character.

This might be considered cheating because the BF compiler skips over the ¤.

• Tim: The lowest number wins, not the shortest program. In any case, I don't think the ¤ is cheating, as it's just a normal comment. – Joey Jun 22 '11 at 21:22

## Javascript, prints 9432 6902

(function a(){b="("+a+")()";c=0;for(i=0;i<b.length;i++){c+=b.charCodeAt(i-0)}alert(c)})()


This is the first quine solution so far, unless I am not understanding the Haskell one correctly.

• That Haskell code is just searching for the smallest number which will equal the sum of its digit's ASCII codes. I guess most answers here were done by brute-force. – Joey Jun 21 '11 at 23:43
• Can be reduced further from 89 to 86 bytes with: (function a(){b="("+a+")()";for(i=c=0;i<b.length;i++)c+=b.charCodeAt(i-0);alert(c)})() – WallyWest Mar 9 '14 at 11:52

## PowerShell

((310))


prints 310.

## Perl, 500

say     500


There are two tabs between say and 500. :)

(Run as a one-liner with perl -E, as far as I can tell this is within the rules)

• I like the 2 tabs – Steve P Jun 25 '11 at 15:20

## Ruby, prints 380

p (380)


No trailing newline after the closing parenthesis.

## PowerShell

(230)


prints 230, obviously.

## PowerShell

-(-320)


prints 320.

## J, 150

?!6


With the caveat that it will be correct only 1/720th of the time.

• Random on factorial 6? Hmmm, I don't think so, Tim. – MPelletier Jun 22 '11 at 0:01

## Python, prints 781

print  781


Two spaces.

• I especially like the 2 spaces. – Steve P Jun 25 '11 at 15:19

# Element, 220

This is a language of my own creation, and it is documented on my answer to another question here.

220!


Here is a walkthrough of how it works: The 220 pushes that number onto the stack. Then the  outputs the top element of the stack. The! then performs a logical not on the control stack (a separate stack), setting it to 1.

# PHP, prints 4440

<?php
for($x=0;$x<15000;$x++)if($x==4440){printf($x);exit;}  ## PowerShell, prints 3902 &{[char[]]$myinvocation.Line|%{$s+=+$_};$s}  Looks into the line currently run and sums the code point values. ## INTERCAL, 1572 I can't believe no one's done INTERCAL yet! DOREADOUT#1572 DOGIVEUP  (Includes terminating newline.) This program prints out MDLXXII. ## Perl, prints 690 die 690 .$/


Or, if we can post one-liners (perl -E)

say(570)


Prints 570.

(No trailing newlines)

## JavaScript, 1750 900 860 790

alert(790)

(Carriage return (CR, \r or \x0D) after or before the program)

These programs are found by brute-forcing.

Bigger values:

alert(860)%0
document.write(1750)


# Java -128

I know reading stdin isn't allowed but I wanted to provide an example of how I calculated my score.

My code sums the ASCII count of itself passed on stdin and prints out -128

class P{public static void main(String[]z)throws Exception{byte v=0;int b=0;while((b=System.in.read())!=-1){v+=(byte)b;}System.out.println(v);}}


No trailing new line

• I think your sum is overflowing. – Alpha Dec 20 '11 at 1:28

## Ruby, prints 300

p   300


There is a space and a tab between the p and the 300. No trailing newline.

## Batch files, 500

ECHO  500


As well, notice the two spaces between "ECHO" (uppercase on purpose) and "500".

## C, 1700

Strange - nobody posted a C solution yet (excluding the example in the question).

main(R){puts("1700");}


No newline at the end.

# K (923 796 795 746 513)

I'm not sure if this falls afoul of the rules or not. It doesn't use stdin, it opens itself as a vector of bytes and sums.

+/1:.z.f


Usage:

q scriptname.k


edit 2012.05.08 - no need to hsym the file handle 2012.05.09 - saved 1 point by converting to byte instead of int

2012.05.17 - Can save a load of points by reading file as bytestream rather than text:

## J, 198

33*6


and

6*33


Found it by brute force. In J there are no 1 or 2 char solutions, and the only 3-char solution is 150. Barring any bugs in my search, there are no other 4-char solutions, either.

From the #jsoftware IRC channel, we also had <.%:10!20 at 429 and a self-counting quine +/a.i.2#(,{:)'+/a.i.2#(,{:)''' at 1706.

### bc 1160

invoked with echo and blanks, the whole String, including 7 blanks, echo 1160 | bc has a bytesum of 1160.

echo 1160    |  bc


150 works for bc too:

echo "150" > 150
bc -q 150
150


## D, 9752

this one actually calculates it similar to my quine

enum c=q{import std.stdio;void main(){int s;foreach(d;"enum c=q{"~c~"};mixin(c);")s+=d;write(s);}};mixin(c);


## Whitespace, 369

This 20-character program prints the number 369, which is the sum of the ascii values of its characters (which are Tab,Space,Linefeed characters, here symbolized by T,S,L, respectively):

SSSTSTTTSSSTLTLSTLLL


(369 = 7*9 + 8*32 + 5*10, there being 7 Tabs, 8 Spaces, and 5 Linefeeds.)

A small modification of my quine:

main=print.sum.map fromEnum$q++show q;q="main=print.sum.map fromEnum$q++show q;q="


## Brainf*ck, 253 (or 252)

Slight improvement on Peter Olson's solution:

---.H


Provided non-printables are allowed, it can be improved further by adding a - and replacing H by ASCII code 26.

println (1090)

## Python, 5440

Highest score wins, right?

import sys