13
\$\begingroup\$

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 })

\$\endgroup\$
  • 4
    \$\begingroup\$ It'd be interesting to see a quine solution: one that produces and sums its own bytes. \$\endgroup\$ – Joey Adams Jun 21 '11 at 19:39
  • 1
    \$\begingroup\$ Not very challenging imho. It can be trivially brute-forced, especially for languages that simply dump tokens, such as PowerShell, Golfscript, etc. \$\endgroup\$ – Joey Jun 21 '11 at 20:32
  • 2
    \$\begingroup\$ 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. \$\endgroup\$ – Joey Jun 22 '11 at 14:00
  • 5
    \$\begingroup\$ 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). \$\endgroup\$ – Alexandru Jun 22 '11 at 15:28
  • 4
    \$\begingroup\$ 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. \$\endgroup\$ – Joey Jun 22 '11 at 20:09

34 Answers 34

10
\$\begingroup\$

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
\$\endgroup\$
  • \$\begingroup\$ But wc should read a file, containing a 0-byte to produce Number 0, not an empty one. Empty is not Null. \$\endgroup\$ – user unknown Jun 22 '11 at 13:40
  • \$\begingroup\$ 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. \$\endgroup\$ – Joey Jun 22 '11 at 13:53
  • 1
    \$\begingroup\$ 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) \$\endgroup\$ – Random832 Jun 22 '11 at 15:02
  • 1
    \$\begingroup\$ +1 for wc, -1 for base cheating, +1 for making jokes in base 13. \$\endgroup\$ – user475 Jun 22 '11 at 16:16
  • \$\begingroup\$ I would say wc is an application, not a language. \$\endgroup\$ – Thomas Eding Jul 24 '11 at 7:34
14
\$\begingroup\$

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]]
\$\endgroup\$
  • \$\begingroup\$ I guess this is the absolute shortest. \$\endgroup\$ – Alexandru Jun 21 '11 at 19:57
  • 2
    \$\begingroup\$ @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. \$\endgroup\$ – mellamokb Jun 21 '11 at 21:24
  • \$\begingroup\$ @mellamokb The sum of the ASCII characters in 5! is 86, not 125. \$\endgroup\$ – Peter Olson Jun 21 '11 at 22:53
  • 3
    \$\begingroup\$ @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) :-) \$\endgroup\$ – mellamokb Jun 21 '11 at 22:56
11
\$\begingroup\$

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 ¤.

\$\endgroup\$
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – Joey Jun 22 '11 at 21:22
8
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – Joey Jun 21 '11 at 23:43
  • \$\begingroup\$ 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)})() \$\endgroup\$ – WallyWest Mar 9 '14 at 11:52
7
\$\begingroup\$

PowerShell

((310))

prints 310.

\$\endgroup\$
6
\$\begingroup\$

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)

\$\endgroup\$
  • 1
    \$\begingroup\$ I like the 2 tabs \$\endgroup\$ – Steve P Jun 25 '11 at 15:20
5
\$\begingroup\$

Ruby, prints 380

p (380)

No trailing newline after the closing parenthesis.

\$\endgroup\$
5
\$\begingroup\$

PowerShell

(230)

prints 230, obviously.

\$\endgroup\$
5
\$\begingroup\$

PowerShell

-(-320)

prints 320.

\$\endgroup\$
4
\$\begingroup\$

J, 150

?!6

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

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

Python, prints 781

print  781

Two spaces.

\$\endgroup\$
  • 1
    \$\begingroup\$ I especially like the 2 spaces. \$\endgroup\$ – Steve P Jun 25 '11 at 15:19
3
\$\begingroup\$

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.

\$\endgroup\$
2
\$\begingroup\$

PHP, prints 4440

<?php
for($x=0;$x<15000;$x++)if($x==4440){printf($x);exit;}
\$\endgroup\$
2
\$\begingroup\$

PowerShell, prints 3902

&{[char[]]$myinvocation.Line|%{$s+=+$_};$s}

Looks into the line currently run and sums the code point values.

\$\endgroup\$
2
\$\begingroup\$

INTERCAL, 1572

I can't believe no one's done INTERCAL yet!

DOREADOUT#1572


DOGIVEUP

(Includes terminating newline.) This program prints out MDLXXII.

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

Perl, prints 690

die 690 . $/

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

say(570)

Prints 570.

(No trailing newlines)

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

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
alert(900)&&6
document.write(1750)
\$\endgroup\$
1
\$\begingroup\$

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

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

Ruby, prints 300

p   300

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

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

Batch files, 500

ECHO  500

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

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

C, 1700

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

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

No newline at the end.

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

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:

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

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.

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

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

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

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.)

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

Haskell, 7518

A small modification of my quine:

main=print.sum.map fromEnum$q++show q;q="main=print.sum.map fromEnum$q++show q;q="
\$\endgroup\$
0
\$\begingroup\$

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.

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

Scala 1090

println (1090)

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

Python, 5440

Highest score wins, right?

import sys
print sum(ord(i)for i in open(sys.argv[0]).read())

I just thought I'd post it anyway b/c it's a halfway quine solution.

\$\endgroup\$

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.