73
\$\begingroup\$

If you like this, consider participating in:


Make 12 snippets/expressions, in the same language, that result in the numbers 0 through 10, and 42 respectively, but without writing any literal numeric, string, or character data.

Build-in data, like PI() and ALPHABET(), are fine, and so are e.g. CJam's U, X, Y, Z, and A constants, and Processing's BLEND, CHORD, CENTER, BREAK, and LINES.

Every snippet must be able to stand on its own, i.e. they may not be interdependent. However, inside a single snippet, you may assign a variable and use it freely, as long as you refer to it directly by name, and not through a string containing its name.

All the snippets must be valid on the submitter’s computer at the time of submission (as reported by SE), but may not rely on unusual local conditions like number of files in a directory, the exact date or time, or specific input from the user.

Examples of valid snippets

3: INT(LOG10(YEAR(TODAY()))) because it remains true in the foreseeable future
4: CUBICROOT(LEN(CHARACTERSET())) because a 256 letter character set is very common
8: SQRT(SYSTEMTYPE()) because 64-bit systems are very common

Examples of invalid snippets

5: LEN(USERNAME()) because most people do not use “Admin” as login :-)
9: LOG10(SYSTEMMEMORY()) because it only works on systems with exactly 1 GB of memory
42: CODE("*") because it contains a string/character literal

The result of each snippet must result in an actual number (value, int, float, etc.) that can be used for further calculations using the same language as the snippet, i.e not a text string representing that number.

Only character based languages allowed.

Score is total byte count of all the 12 snippets combined. Newlines separating the snippets are not counted in.

Note that the above rules may prevent some languages from participating, even if they are Turing complete.

FAQ

Q Can the programs accept any input?
A Yes, but you may not just ask for input and enter the relevant number.

Q Are physical digits (non-data) digits allowed?
A Yes, e.g. LOG10().

Q Do Symbols in Ruby count as literals?
A Yes.

Q Does score include newlines between each snippet?
A No.

Q Is TI-BASIC "character based" enough to be valid?
A Yes.

Q Do false and true count as number literals?
A No, they are acceptable.

Q Can we use a number literal to call a function if that's the only way and the number doesn't influence the output of the function?
A Yes, if that is the normal way to write code in your language.

Q My language assumes there is a [something] at the start of each program/expression. Must I include it, or should my snippets just work if placed in the middle of a program/expression?
A They should just work in the middle of a program/expression.

Q What about regex literals?
A Forbidden, except for languages that only do regexes.

Q Is one piece of code that could print all the specified numbers acceptable?
A No, they have to be separate and mutually independent.

Q May I assume a boilerplate like int main() {}... or equivalent?
A Yes.

Q What output datatypes are allowed?
A Any numeric datatype, like int, float, etc.

Q Do I need to print the result of each snippet?
A No, making the result available for subsequent use is enough.

Q Are pre-set variables allowed?
A Yes, and they become reset (if changed) for every snippet.

Q Are π and e considered number literals?
A No, you may use them.

Q May I return 4 and 2 in different cells for 42?
A No, they must be connected as one number.

Q Bytes or characters?
A Bytes, but you may choose any desired codepage.

Q May constant functions and preset variables like J's 9:, Actually's 9, and Pretzel's 9 be used?
A Yes, if the vocabulary is finite (19 for J, 10 for Actually and Pretzel).

\$\endgroup\$
12
  • \$\begingroup\$ If 0-9 are not number literals but are pre-populated variables, would they be fair game? \$\endgroup\$
    – Cyoce
    Aug 28, 2016 at 7:58
  • \$\begingroup\$ @Cyoce So 10 is {1, 0} and not 2×5? \$\endgroup\$
    – Adám
    Aug 29, 2016 at 9:34
  • \$\begingroup\$ no, there is simply another syntax for literals that is not base-10, so 0-9 are not literals. They hold the value of 0-9, but are considered variables \$\endgroup\$
    – Cyoce
    Aug 29, 2016 at 15:17
  • \$\begingroup\$ @Cyoce Then it's fine. What language is that? \$\endgroup\$
    – Adám
    Aug 29, 2016 at 18:56
  • \$\begingroup\$ pretzel (a language I'm working on). \$\endgroup\$
    – Cyoce
    Aug 29, 2016 at 22:47

84 Answers 84

51
\$\begingroup\$

Funciton, 1222 bytes

Apart from numeric literals, there are two ways I can produce a value (any value at all) in Funciton: stdin and lambda expressions. Stdin is a single box while a full lambda expression requires more syntax, so I’m going with stdin. However, while stdin could be anything, all of the following work regardless of what input is provided.

All of the library functions used here existed before the challenge was posted.

0 (40 bytes in UTF-16)

╔╗┌┐
║╟┤└┼┬┐
╚╝└─┘└┘

This uses the raw syntax for less-than. A value is never less than itself, so the result of this is 0.

1 (52 bytes in UTF-16)

╔╗┌─╖┌─╖
║╟┤⌑╟┤ɕ╟
╚╝╘═╝╘═╝

returns a lazy sequence containing a single element and ɕ counts the number of elements. (The lazy sequence is lazy enough that this snippet doesn’t actually evaluate stdin at all!)

2 (70 bytes in UTF-16)

╔╗┌─╖┌─╖┌─╖
║╟┤⌑╟┤ʂ╟┤ɕ╟
╚╝╘═╝╘═╝╘═╝

= 2¹. ʂ generates all subsequences of a sequence, and thus turns a sequence of n elements into one with 2ⁿ.

3 (88 bytes in UTF-16)

╔╗┌─╖┌─╖┌─╖┌─╖
║╟┤⌑╟┤ʂ╟┤ɕ╟┤♯╟
╚╝╘═╝╘═╝╘═╝╘═╝

= 2 + 1. increments a value by 1.

4 (88 bytes in UTF-16)

╔╗┌─╖┌─╖┌─╖┌─╖
║╟┤⌑╟┤ʂ╟┤ʂ╟┤ɕ╟
╚╝╘═╝╘═╝╘═╝╘═╝

= 2².

5 (106 bytes in UTF-16)

╔╗┌─╖┌─╖┌─╖┌─╖┌─╖
║╟┤⌑╟┤ʂ╟┤ʂ╟┤ɕ╟┤♯╟
╚╝╘═╝╘═╝╘═╝╘═╝╘═╝

= 4 + 1.

6 (106 bytes in UTF-16)

╔╗┌─╖┌─╖┌─╖┌─╖┌─╖
║╟┤⌑╟┤ʂ╟┤ɕ╟┤♯╟┤!╟
╚╝╘═╝╘═╝╘═╝╘═╝╘═╝

= 3 factorial.

7 (110 bytes in UTF-16)

┌───┐┌─╖┌─╖┌─╖╔╗
│┌─╖├┤ɕ╟┤ʂ╟┤⌑╟╢║
└┤A╟┘╘═╝╘═╝╘═╝╚╝
 ╘╤╝

= A(2, 2) (Ackermann function).

8 (118 bytes in UTF-16)

┌────┐┌─╖┌─╖┌─╖╔╗
│┌──╖├┤ɕ╟┤ʂ╟┤⌑╟╢║
└┤<<╟┘╘═╝╘═╝╘═╝╚╝
 ╘╤═╝

= 2 << 2 (shift-left).

9 (128 bytes in UTF-16)

┌───┐┌─╖┌─╖┌─╖┌─╖╔╗
│┌─╖├┤♯╟┤ɕ╟┤ʂ╟┤⌑╟╢║
└┤×╟┘╘═╝╘═╝╘═╝╘═╝╚╝
 ╘╤╝

= 3 × 3.

10 (146 bytes in UTF-16)

┌───┐┌─╖┌─╖┌─╖┌─╖┌─╖╔╗
│┌─╖├┤♯╟┤ɕ╟┤ʂ╟┤ʂ╟┤⌑╟╢║
└┤+╟┘╘═╝╘═╝╘═╝╘═╝╘═╝╚╝
 ╘╤╝

= 5 + 5.

42 (170 bytes in UTF-16)

┌──────┐┌─╖┌─╖┌─╖┌─╖┌─╖╔╗
│┌─╖┌─╖├┤!╟┤♯╟┤ɕ╟┤ʂ╟┤⌑╟╢║
└┤♯╟┤×╟┘╘═╝╘═╝╘═╝╘═╝╘═╝╚╝
 ╘═╝╘╤╝

= 6 × (6 + 1).

\$\endgroup\$
2
  • 7
    \$\begingroup\$ +1 for the Ackermann function. \$\endgroup\$ Dec 18, 2015 at 18:42
  • 5
    \$\begingroup\$ I wish I could upvote more than once... \$\endgroup\$
    – user46167
    Dec 19, 2015 at 0:28
26
\$\begingroup\$

JavaScript, 144 141 140 138 132 125 123 bytes

With help from @edc65, @Sjoerd Job Postmus, @DocMax, @usandfriends, @Charlie Wynn and @Mwr247!

result.textContent = [

+[]                          ,// 0  (3 bytes)
-~[]                         ,// 1  (4 bytes)
-~-~[]                       ,// 2  (6 bytes)
-~-~-~[]                     ,// 3  (8 bytes)
-~Math.PI                    ,// 4  (9 bytes)
-~-~Math.PI                  ,// 5  (11 bytes)
-~-~-~Math.PI                ,// 6  (13 bytes)
Date.length                  ,// 7  (11 bytes)
(a=-~-~[])<<a                ,// 8  (13 bytes) = (2 << 2)
(a=~Math.E)*a                ,// 9  (13 bytes) = (-3 * -3)
(a=-~-~[])<<a|a              ,// 10 (15 bytes) = ((2 << 2) | 2)
(a=Date.length)*--a           // 42 (19 bytes) = (7 * 6)

];
<pre id="result"></pre>

\$\endgroup\$
13
  • \$\begingroup\$ Try 4: -~Math.PI \$\endgroup\$
    – edc65
    Dec 18, 2015 at 7:32
  • \$\begingroup\$ @edc65 Thanks! I thought there would be something I could do with PI. :) \$\endgroup\$
    – user81655
    Dec 18, 2015 at 7:34
  • \$\begingroup\$ For 5, 6 you can also use -~-~Math.PI and -~-~-~Math.PI saving another byte (twice). \$\endgroup\$ Dec 18, 2015 at 13:48
  • 1
    \$\begingroup\$ Save one on 42 with (escape+NaN).length. P.S. Today I learned that JavaScript is really weird... \$\endgroup\$
    – Mwr247
    Mar 1, 2016 at 22:17
  • 1
    \$\begingroup\$ I was gonna say that NaN counts as a number, but it's literally Not a Number :P \$\endgroup\$ Mar 2, 2016 at 21:44
21
\$\begingroup\$

Hexagony, 13 bytes


1
2
3
4
5
6
7
8
9
10
42

Try it online!

In Hexagony, 0 through 9 are functions that multiply the current memory by 10, and then add the number represented by the function name. Therefore, the first snippet is empty as memories start off as 0.

For example, if the current memory is 65, executing the function 3 will make the current memory 653.

(To downvoters: downvote all you want; I am ready.)

\$\endgroup\$
1
  • \$\begingroup\$ Sneaky, but gets my upvote and tick. \$\endgroup\$
    – Adám
    Aug 23, 2016 at 13:30
16
\$\begingroup\$

Mouse-2002, 27 26 17 14 bytes

The first snippets push 0-10, and ZR+ pushes 25 then 17 and 25 17 + 42 = is 1.

A
B
C
D
E
F
G
H
I
J
K
ZR+
\$\endgroup\$
2
  • \$\begingroup\$ It says "a snippet or expression" so you can remove all the !'s \$\endgroup\$
    – user46167
    Dec 19, 2015 at 0:45
  • 3
    \$\begingroup\$ @cat I can't speak for the questioner, but I think that means stand alone from each other - no defining a function in one, then using it in another. However, each snippet doesn't need to be an entire program, it can assume the boilerplate int main() {}... equivalent that will make it run. \$\endgroup\$ Dec 19, 2015 at 3:16
15
\$\begingroup\$

CJam, 27 24 bytes

U    e# 0
X    e# 1
Y    e# 2
Z    e# 3
Z)   e# 3 + 1
YZ+  e# 2 + 3
ZZ+  e# 3 + 3
AZ-  e# 10 - 3
YZ#  e# 2³
A(   e# 10 - 1
A    e# 10
EZ*  e# 14 × 3

Thanks to @MartinBüttner for -1 byte!

Try it online!

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

Brainfuck, 70 bytes

+
++
+++
++++
+++++
++++++
+++++++
++++++++
+++++++++
++++++++++
--[>+<++++++]>-

Each line must be run individually.

The first 10 are self explanatory: we increment the value of the cell via each plus.

The 42 is a lot more complex. It relies on the fact the most brainfuck interpreter use 8-bit cells, meaning that all operations on it are done modulo 256. The -- sets cell #0 to 254. Then we enter a loop which runs until cell #0 is 0. Each iteration adds 1 to cell #1 and adds 6 to cell #0. This loops runs 43 times, so cell #1 is 43. Finally, we subtract 1 from cell #1 to make it 42.

I got the most efficient 42 ever found from http://esolangs.org/wiki/Brainfuck_constants

\$\endgroup\$
2
  • 1
    \$\begingroup\$ @someonewithpc 4 and 2 is different from 42: the OP says The result of each snippet must result in an actual number that can be used for further calculations using the same language as the snippet, i.e not a text string representing that number. You can do calculations on 42 directly, but it's not the same for 4 and 2 in separate cells. \$\endgroup\$
    – p1xel
    Dec 19, 2015 at 23:00
  • \$\begingroup\$ Oh, ok. I had missed that.. \$\endgroup\$ Dec 19, 2015 at 23:01
12
\$\begingroup\$

Darkness, 339 303 bytes

This is where Darkness really shines. Get it? :~)!

Without printing (replaced the space with \s in the first line since it won't show otherwise):

\s

█ 

██ 

███ 

████ 

█████ 

██████ 

███████ 

████████ 

█████████ 

██████████ 

██████████████████████████████████████████ 

With printing:

■ 

█■ 

██■ 

███■ 

████■ 

█████■ 

██████■ 

███████■ 

████████■ 

█████████■ 

██████████■ 

██████████████████████████████████████████■ 

Each line must be run individually in this case since the program terminates in the light (a space). However, it is possible to write this on one or several lines in the same program.

Regular darkness (█) increments a register by 1, and the ■ instruction (some sort of mini-darkness) outputs the contents of the register.

\$\endgroup\$
1
  • \$\begingroup\$ I don't think this asks for full programs, just snippets. \$\endgroup\$ Oct 6, 2016 at 15:59
12
\$\begingroup\$

Perl 5, 86 75 71 66 bytes

All ^Fs are literal control characters (0x06 in ASCII), and hence a single byte.

$[          # array start index, defaults to 0                                  2
!$[         # !0 is 1                                                           3
$^F         # max sys file descriptor number, 2 on all sane systems             2
++$^F       # 2 + 1                                                             4
~-$]        # 5 - 1                                                             4
int$]       # $] is Perl version, int truncates                                 5
length$~    # 1 + 5                                                             8
~~exp$^F    # floor(e^2)                                                        7
$^F<<$^F    # 2 bitshift-right 2                                                6
-!$[+ord$/  # -1 + 10                                                          10
ord$/       # input record separator, newline by default, ord gets ASCII val    5
ord($"^$/)  # 32 + 10                                                          10

Thanks to msh210 for saving 11 bytes and Dom Hastings for 9 bytes!

\$\endgroup\$
10
  • 3
    \$\begingroup\$ Perl is strange. zoitz.com/comics/perl_small.png \$\endgroup\$
    – ldam
    Dec 18, 2015 at 15:49
  • \$\begingroup\$ downgoated because "max sys file descriptor number, 2 on all sane systems = 3" but i have 65+ \$\endgroup\$
    – cat
    Dec 20, 2015 at 12:28
  • \$\begingroup\$ (i'm just joking, i upvoted of course) \$\endgroup\$
    – cat
    Dec 20, 2015 at 12:29
  • 1
    \$\begingroup\$ You ever read the comments on an SE post and think, "wow, which dumbass wrote that"? That's me right now, at myself. \$\endgroup\$
    – cat
    Mar 1, 2016 at 22:13
  • \$\begingroup\$ !$[+ord$/ # -1 + 10 — I don’t understand. In line 2 you say that !$[ gives you 1, not −1, so this snippet gives 11. \$\endgroup\$
    – Timwi
    Mar 6, 2016 at 13:50
10
\$\begingroup\$

MATL, 30 bytes

O
l
H
I
K
Kl+
HI*
KYq
HK*
IH^
K:s
IH^:sI-

H, I, and K are predefined constants for 2, 3, and 4 (like pi). O and l are functions that returns a matrix of zeros (O) or ones (l), the default size is 1x1. : makes a vector, and s sums it, so K:s makes a vector from 1 to 4 and sums it to get 10. Yq is the n-th prime function, so KYq is the 4th prime, 7.

\$\endgroup\$
2
  • \$\begingroup\$ That Yq function (and its implementation) was a very nice suggestion of yours :-) \$\endgroup\$
    – Luis Mendo
    Dec 18, 2015 at 1:26
  • \$\begingroup\$ IK+ would work just as well for 7, but it's a bit too boring :P \$\endgroup\$
    – David
    Dec 18, 2015 at 2:34
10
\$\begingroup\$

Prolog, 113 99 bytes

Snippets:

e-e              % 0.0
e/e              % 1.0
e/e+e/e          % 2.0
ceil(e)          % 3
ceil(pi)         % 4
ceil(e*e-e)      % 5
ceil(e+e)        % 6
floor(e*e)       % 7
ceil(e*e)        % 8
ceil(pi*e)       % 9
ceil(pi*pi)      % 10
ceil(e^e*e)      % 42

Combines the mathematical constants e and pi in different ways converted to int.

Edit: Saved 14 bytes by utilizing floats for 0-2.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ anything wrong with e/e=1? \$\endgroup\$ Dec 17, 2015 at 22:29
  • \$\begingroup\$ @steveverrill: it will become a float (1.0). I interpreted the challenge description to mean that the numbers should be integers. Most of these, if not all could be shortened otherwise. \$\endgroup\$
    – Emigna
    Dec 18, 2015 at 7:18
  • 1
    \$\begingroup\$ @steveverrill Floats are fine. We just need the right value. \$\endgroup\$
    – Adám
    Dec 20, 2015 at 3:29
9
\$\begingroup\$

PHP, 157 145 91 bytes

First time posting on Code Golf, figured I'd give it a shot. I'll get better eventually :P If you see any obvious (to you) spots where I could save characters, let me know.

EDIT: Realized I didn't need the semicolons, since these are just snippets.

EDIT2: Thanks to Blackhole for many suggestions!

LC_ALL
DNS_A
~~M_E
~~M_PI
LOCK_NB 
LC_TIME
LOG_INFO
INI_ALL
IMG_WBMP
SQL_DATE
SQL_TIME
LOG_INFO*INI_ALL
\$\endgroup\$
6
  • 5
    \$\begingroup\$ PHP has a lot of extensions, and therefore a lot of predefined constants far better than the mathematical ones for this challenge ;). LC_ALL for 0 (-1 byte), DNS_A for 1 (-3 bytes), LOCK_NB for 4 (-3 bytes), LC_TIME for 5 (-7 bytes), LOG_INFO for 6 (-8 bytes), INI_ALL for 7 (-5 bytes), … \$\endgroup\$
    – Blackhole
    Dec 19, 2015 at 20:33
  • 5
    \$\begingroup\$ IMG_WBMP for 8 (-4 bytes), SQL_DATE for 9 (-9 bytes), SQL_TIME for 10 (-3 bytes) and LOG_INFO*INI_ALL for 42 (-11 bytes). Hence a total of 51 bytes saved! These constants are valid at least in PHP 5.6.1 on Windows. \$\endgroup\$
    – Blackhole
    Dec 19, 2015 at 20:33
  • \$\begingroup\$ @Blackhole isn't LC_ALL a locale dependent thing? \$\endgroup\$
    – cat
    Dec 20, 2015 at 12:22
  • \$\begingroup\$ @cat It's indeed a constant used with setlocale() to change all the categories of locales. But the value of the constant itself is of course independent of the locale :). \$\endgroup\$
    – Blackhole
    Dec 20, 2015 at 14:49
  • \$\begingroup\$ @Blackhole ah, i see! \$\endgroup\$
    – cat
    Dec 20, 2015 at 14:50
9
\$\begingroup\$

Python 2, 191 159 158 157 156 149 146 bytes

My first submission ever, I hope I got everything right ! Based on the time I spent on this, I guess there's surely a better one for a few of them.

# 0 | Bytes : 5
int()

# 1 | Bytes : 5
+True

# 2 | Bytes : 6
-~True

# 3 | Bytes : 8
-~-~True

# 4 | Bytes : 10
-~-~-~True

# 5 | Bytes : 12
-~-~-~-~True

# 6 | Bytes : 14
-~-~-~-~-~True

# 7 | Bytes : 16
-~-~-~-~-~-~True

# 8 | Bytes : 15
a=True;a<<a+a+a

# 9 | Bytes : 19
a=True;(a<<a+a+a)+a

# 10 | Bytes : 20
int(`+True`+`int()`)

# 42 | Bytes : 16
~-len(`license`)
# Especially proud of this one !

Total byte count: 146

Many thanks to FryAmTheEggman !

\$\endgroup\$
4
  • \$\begingroup\$ Welcome to PPCG :) I'm not 100% sure about this challenge but I think using True as an idiom for 1 should be acceptable, as I don't know when they aren't equivalent as snippets. \$\endgroup\$ Dec 20, 2015 at 21:59
  • \$\begingroup\$ Oh, you're right ! Not using it for the 1 itself, because True is not 1 but for all the computation based on 1, it helps ! Editing now. \$\endgroup\$ Dec 20, 2015 at 22:02
  • 1
    \$\begingroup\$ Use #8 len(`id(id)`). Then 8, 9, and 10 will be shorter. Also, maybe add a hyperlink to Try it online. \$\endgroup\$
    – mbomb007
    Mar 3, 2016 at 16:12
  • \$\begingroup\$ You can get 9 with len(`{()}`) for 11 bytes, and that gives you 7 through 10 shorter. \$\endgroup\$
    – xnor
    Aug 25, 2016 at 23:06
9
\$\begingroup\$

C#, no usings, 234 bytes

new int()                       // 0
-~new int()                     // 1
-~-~new int()                   // 2
-~-~-~new int()                 // 3
-~-~-~-~new int()               // 4
-~-~-~-~-~new int()             // 5
-~-~-~-~-~-~new int()           // 6
-~-~-~-~-~-~-~new int()         // 7
-~-~-~-~-~-~-~-~new int()       // 8
(int)System.ConsoleKey.Tab      // 9
(int)System.TypeCode.UInt32     // 10
(int)System.ConsoleKey.Print    // 42

This is much more boring than I initially thought it was going to be. I had pretty varied ideas, such as new[]{true}.Length and true.GetHashCode() and typeof(int).Name.Length and uint.MinValue etc., but new int() beat them all.

\$\endgroup\$
6
  • \$\begingroup\$ Would the rules allow you to do something like var a = new int(); and then use a in each snippet? \$\endgroup\$
    – ldam
    Dec 18, 2015 at 15:51
  • \$\begingroup\$ @LoganDam: I find it more interesting if every expression has to stand on its own. That’s also why I didn’t use any using declarations. \$\endgroup\$
    – Timwi
    Dec 18, 2015 at 22:57
  • \$\begingroup\$ Whoa what are those for 9/10/42 :Oo \$\endgroup\$
    – user46167
    Dec 19, 2015 at 0:35
  • \$\begingroup\$ @ev3commander: They are simply the shortest-named enum values defined in mscorlib that have the necessary integer value. For ConsoleKey.Tab, the value 9 is not surprising (9 is also the ASCII of the tab character). The others are probably arbitrary. \$\endgroup\$
    – Timwi
    Dec 19, 2015 at 1:49
  • \$\begingroup\$ Shorter one for 8: int a=-~-~new int();a<<a \$\endgroup\$ Dec 25, 2015 at 23:51
9
\$\begingroup\$

PowerShell, 147 bytes

These use + to implicitly cast things to integers. The later numbers use Enums from the .Net Framework unerpinnings of PowerShell which happen to have the right values.

+$a                          #0, 3 bytes (unset vars are $null, +$null == 0)
+$?                          #1, 3 bytes (bool previous result, default $true, +$true == 1)
$?+$?                        #2, 5 bytes (same as #1, twice)
$?+$?+$?                     #3, 8 bytes (beats [Int][Math]::E)
$?+$?-shl$?                  #4, 11 bytes (-shl is shift-left)
$?+$?+$?+$?+$?               #5, 14 bytes
$?+$?+$?-shl$?               #6, 14 bytes (enum value, + casts to integer)
+[TypeCode]::Int16           #7, 18 bytes
$?+$?-shl$?+$?               #8, 14 bytes
+[consolekey]::tab           #9, 18 bytes
+[TypeCode]::UInt32          #10, 19 bytes
+[consolekey]::Print         #42, 20 bytes

#Total:                      147 bytes

  • -~-~-~ used in JavaScript, C# and PHP answers would be - -bnot - -bnot - -bnot in PowerShell.

  • x^y exponentiation used in Perl answers, or x**y in Python or JavaScript ES7, would be [Math]::Pow($x,$y)

  • constants e and Pi are the character-heavy [Math]::E and [Math]::PI

\$\endgroup\$
4
  • \$\begingroup\$ x^y is xor in JavaScript. JavaScript (ES7) has ** for exponents. Source \$\endgroup\$
    – mbomb007
    Mar 3, 2016 at 16:18
  • \$\begingroup\$ @mbomb007 Ah, thanks - I've updated my note on that. \$\endgroup\$ Mar 4, 2016 at 3:56
  • \$\begingroup\$ @mbomb007 I still think that's kind of silly \$\endgroup\$ Mar 15, 2016 at 15:50
  • \$\begingroup\$ @SuperJedi224 Why? That's how Python does it. And xor is an important operator. \$\endgroup\$
    – mbomb007
    Mar 15, 2016 at 16:23
8
\$\begingroup\$

DC, 35 bytes

K
KZ
IZ
Iv
EI-
FI-
IZd*
IIZ/
Ivd+
IIv-
IIZ-
IKZ-
I
EdE++

To test the snippets append a f to print the stack and pass that string to dc:

$ echo 'EdE++f' | dc
42
\$\endgroup\$
4
  • \$\begingroup\$ I believe that E and F here are digits (even when they are greater than the input radix). Evidence for this is that they combine as digits; e.g. F0 -> 150. You can see the same behaviour with decimal digits once you change input and output radix. \$\endgroup\$ Mar 2, 2016 at 8:52
  • \$\begingroup\$ There are lots of other answers using similar stuff... why should I be the only one where this is not ok? \$\endgroup\$
    – user19214
    Mar 2, 2016 at 9:26
  • \$\begingroup\$ You shouldn't - if there are other solutions using digits, then they aren't valid answers either. \$\endgroup\$ Mar 2, 2016 at 9:27
  • 1
    \$\begingroup\$ I don't care any more! Codegolf even accepts a graphic mandelbrot set program as solution to the task to write an ascii art mandelbrot program... codegolf needs a big reset and when restarted I may or may not care about rules again... :-Þ \$\endgroup\$
    – user19214
    Mar 2, 2016 at 9:29
7
\$\begingroup\$

TI-BASIC, 41 bytes

0~10:

X
cosh(X
int(e
int(π
-int(-π
int(√(π³
int(π+π
int(e²
int(eπ
int(π²
Xmax

42:

int(π²/ecosh(π

In TI-BASIC, all uninitialized single-letter variables start at 0, and Xmax (the right window boundary of the graph screen) starts at 10.

The mathematical constant π is one byte, but e is two bytes.

\$\endgroup\$
3
  • \$\begingroup\$ Isn't π considered numeric data? \$\endgroup\$
    – vsz
    Dec 18, 2015 at 12:35
  • \$\begingroup\$ @vsz perhaps, but it isn't a number literal. The op even says so. \$\endgroup\$ Dec 18, 2015 at 14:01
  • \$\begingroup\$ @NBZ very good point. My bad. \$\endgroup\$
    – GamrCorps
    Dec 20, 2015 at 4:35
6
\$\begingroup\$

Python 2, 306 275 274 bytes

I used the fact that for any x (integer and not 0) the expression x/x equals 1 and played around with some bitwise operations.

I adjusted the snippets such that they still meet the requirements (thanks @nimi this saved me 24 bytes), but you have to manually test them. Here is the code and individual byte counts:

zero.py Bytes: 7
len({})
--------------------------
one.py  Bytes: 12
r=id(id)
r/r
--------------------------
two.py  Bytes: 17
r=id(id)
-(~r/r)
--------------------------
three.py    Bytes: 20
r=id(id)
-(~r/r)|r/r
--------------------------
four.py Bytes: 20
r=~id(id)/id(id)
r*r
--------------------------
five.py Bytes: 26
r=~id(id)/id(id)
(r*r)|r/r
--------------------------
six.py  Bytes: 25
r=~id(id)/id(id)
(r*r)|-r
--------------------------
seven.py    Bytes: 27
r=~id(id)/id(id)
-~(r*r|-r)
--------------------------
eight.py    Bytes: 24
r=-(~id(id)/id(id))
r<<r
--------------------------
nine.py Bytes: 29
r=-(~id(id)/id(id))
r-~(r<<r)
--------------------------
ten.py  Bytes: 31
r=~id(id)/id(id)
-r*((r*r)|r/r)
--------------------------
answer.py   Bytes: 37
r=-(~id(id)/id(id))
(r<<r*r)|(r<<r)|r
--------------------------
Total byte count: 274
\$\endgroup\$
1
  • \$\begingroup\$ You can save bytes with i=id(id);r=~i/i \$\endgroup\$
    – Cyoce
    Aug 26, 2016 at 16:52
5
\$\begingroup\$

Javascript (Browser Env), 155 136 130 bytes

+[]
-~[]
-~-~[]
-~-~-~[]
-~-~-~-~[]
-~-~-~-~-~[]
-~-~-~[]<<-~[]
-~-~-~-~-~-~-~[]
-~[]<<-~-~-~[]
~(~[]+[]+-[])
-~[]+[]+-[]
-~(top+top.s).length // Requires browser environment

Thanks to:
@Ismael Miguel: 155 -> 136 -> 130 bytes

\$\endgroup\$
5
  • 1
    \$\begingroup\$ You can use -~[]+[]+-[] to produce 10. It will return a string, but it's still usable as a number. Also, you can use -~(top+top.s).length to calculate 42 (-8 bytes) and drop your dependency on Google Chrome. To save more 3 bytes, use (P=Math.PI)*P>>+[] to calculate 9. \$\endgroup\$ Dec 18, 2015 at 19:57
  • 1
    \$\begingroup\$ Sorry, forgot a few bytes you can shave. You can use ~(~[]+[]+-[]) to generate 9. That should cut down a few more bytes. \$\endgroup\$ Dec 18, 2015 at 23:06
  • \$\begingroup\$ curiously, +[12] gives 12 and +[1, 2] gives NaN. I hate JS \$\endgroup\$
    – cat
    Dec 19, 2015 at 12:56
  • 2
    \$\begingroup\$ @cat It's because of JavaScript's weird cast system. Arrays cast to strings like [1,2,3] => "1,2,3" and strings cast to numbers like "12" => 12 but if there are non-number characters in the string the cast returns NaN. +[1,2] casts to a string then a number but the string contains a comma so "1,2" becomes NaN. \$\endgroup\$
    – user81655
    Dec 19, 2015 at 22:56
  • \$\begingroup\$ @user81655 that. is. HORRID. \$\endgroup\$
    – cat
    Dec 19, 2015 at 23:00
5
\$\begingroup\$

Seriously, 39 33 bytes

Stuff in parentheses is explanations:

 (single space, pushes size of stack, which is 0 at program start)
 u (space pushes 0, u adds 1 (1))
 ⌐ (space pushes 0, ⌐ adds 2 (2))
 u⌐ (space pushes 0, u adds 1 (1), ⌐ adds 2 (3))
 ⌐⌐ (space pushes 0, ⌐⌐ adds 2 twice (4))
 ⌐P (space pushes 0, ⌐ adds 2 (2), P pushes the 2nd prime (5))
Hl▓ (H pushes "Hello, World!", l pushes length (13), ▓ pushes pi(13) (6))
QlP (Q pushes "QlP", l pushes length (3), P pushes the 3rd prime (7))
Ql╙ (Q pushes "QlP", l pushes length (3), ╙ pushes 2**3 (8))
úl▓ (ú pushes the lowercase English alphabet, l pushes length (26), ▓ pushes pi(26) (9))
 u╤ (space pushes 0, u adds 1 (1), ╤ pushes 10**1 (10))
HlPD (H pushes "Hello, World!", l pushes length (13), P pushes the 13th prime (43), D subtracts 1 (42))

Hexdumps of programs:

20
2075
20a9
2075a9
20a9a9
20a950
486cb2
516c50
516cd3
a36cb2
2075d1
486c5044

Thanks to quintopia for 6 bytes!

\$\endgroup\$
5
  • 1
    \$\begingroup\$ I suppose Seriously uses a 256 char codepage that includes pseudo graphic chars? \$\endgroup\$
    – Adám
    Dec 18, 2015 at 14:34
  • 2
    \$\begingroup\$ @NBZ CP437 \$\endgroup\$
    – user45941
    Dec 18, 2015 at 15:46
  • \$\begingroup\$ You can save a byte on 6 with Hl▓ \$\endgroup\$
    – quintopia
    Dec 20, 2015 at 14:19
  • \$\begingroup\$ The same idea with ú saves a byte on 9 \$\endgroup\$
    – quintopia
    Dec 20, 2015 at 14:28
  • \$\begingroup\$ HlPD saves 2 bytes on 42, and QlP saves a byte on 7, and Qlª saves a byte on 9, and Ql╙ saves a byte on 8. I think that gets Seriously down to 33 bytes all told, tying Pyth. \$\endgroup\$
    – quintopia
    Dec 21, 2015 at 4:31
5
\$\begingroup\$

dc, 42 bytes

K
zz
OZ
zzz+
OZd*
OdZ/
zzzz*
Ozz+-
OdZ-
Oz-
O
Od+dz++

Results

0
1
2
3
4
5
6
7
8
9
10
42

There aren't many ways to generate new numbers with dc. I use O: output base, initially 10; K: precision, initially 0; z stack depth, initially 0; Z significant digits of operand. We combine these with the usual arithmetic operators.

Test program

#!/bin/bash

progs=(                                         \
    "K"                                         \
    "zz"                                        \
    "OZ"                                        \
    "zzz+"                                      \
    "OZd*"                                      \
    "OdZ/"                                      \
    "zzzz*"                                     \
    "Ozz+-"                                     \
    "OdZ-"                                      \
    "Oz-"                                       \
    "O"                                         \
    "Od+dz++"                                   \
)

a=0
results=()
for i in "${progs[@]}"
do
    results+=($(dc -e "${i}p"))
    (( a+=${#i} ))
done

echo "#dc, $a bytes"
echo
printf '    %s\n' "${progs[@]}"
echo
echo '##Results'
echo
printf '    %s\n' "${results[@]}"
\$\endgroup\$
5
\$\begingroup\$

Math++, 92 bytes total

0 (1 bytes): a

1 (2 bytes):!a

2 (3 bytes):_$e

3 (4 bytes): _$pi

4 (7 bytes): _$e+_$e

5 (8 bytes): _($e+$e)

6 (9 bytes): _$pi+_$pi

7 (8 bytes): _($e*$e)

8 (9 bytes): _($e*$pi)

9 (10 bytes): _($pi*$pi)

10 (12 bytes): _$e*_($e+$e)

42 (19 bytes): _($pi+$pi)*_($e*$e)

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

MS Excel formulas, 163 151 150 143 bytes

Not exactly a programming language, but here it goes...

0:  -Z9                         (03 bytes)
1:  N(TRUE)                     (07 bytes)
2:  TYPE(T(Z9))                 (11 bytes)
3:  TRUNC(PI())                 (11 bytes)
4:  TYPE(TRUE)                  (10 bytes)
5:  ODD(PI())                   (09 bytes)
6:  FACT(PI())                  (10 bytes)
7:  ODD(PI()+PI())              (14 bytes)
8:  EVEN(PI()+PI())             (15 bytes)
9:  TRUNC(PI()*PI())            (16 bytes)
10: EVEN(PI()*PI())             (15 bytes)
42: EVEN(CODE(-PI())-PI())      (22 bytes)

PI() is used in most cases as it is the shorter way (that I am aware of) to introduce a numeric value without using a number or string literal. N converts various things (incl. booleans) to numbers, and T converts various things to text. TYPE returns 2 for a text argument and 4 for a boolean argument. TRUNC discards fractional part (i.e. rounds positive numbers down), EVEN rounds up to the next even number, and ODD rounds up to the next odd number. CODE(-PI()) is the ASCII code of the first character of the conversion to text of -π, i.e. 45 (for "-").

EDIT: Removed equal signs from the byte count (-12!) - as pointed out by Nᴮᶻ in the comments, they are not supposed to be included.

EDIT 2: Assuming the rest of the worksheet is empty, it is possible to use a reference to an empty cell as zero (again, suggested by Nᴮᶻ) provided that we include a minus sign (or use it in other numeric expression) to resolve type ambiguity.

\$\endgroup\$
12
  • \$\begingroup\$ 0=Z9​​​​​​​​​ \$\endgroup\$
    – Adám
    Mar 3, 2016 at 22:24
  • \$\begingroup\$ Removed the =s, thanks. Regarding your suggestion, I avoided cell references on purpose, to make sure the formulas are independent of table contents - if Z9 contains a text, =Z9 won't return zero anymore. I decided not to assume anything about the table. \$\endgroup\$
    – dnep
    Mar 3, 2016 at 22:46
  • \$\begingroup\$ anyway, you made me loot at it again, and I realized I could at least save 1 byte by using to 0 the same logic that I used to 1... \$\endgroup\$
    – dnep
    Mar 3, 2016 at 23:06
  • \$\begingroup\$ But some languages have all variables set to 0 if they are not set to something else. In this context, I would see Excel as a language with (theoretically infinite) memory cells A1:ZZZ..:999... and no distinction between program code and data (i.e. may be self-modifying). Since each snippet is independent of the others, I assume it is in A1, and the rest of the sheet it blank. (For programs, I would have one whole statement in each cell A1, A2, etc.) \$\endgroup\$
    – Adám
    Mar 7, 2016 at 13:11
  • 1
    \$\begingroup\$ 42=ROW(Z42)​​ \$\endgroup\$
    – Adám
    Mar 7, 2016 at 14:16
4
\$\begingroup\$

Mathematica, 101 bytes

a-a
a/a
⌊E⌋
⌈E⌉
⌈π⌉
⌊E+E⌋
⌈E+E⌉
⌊E*E⌋
⌈E*E⌉
⌈E*π⌉
⌈π*π⌉
⌊π*π^π/E⌋

I'm pretty sure some of these are suboptimal. Those rounding brackets are really expensive.

For consistency, the first two could also be E-E and E/E of course, but I thought it's quite nifty to get 0 and 1 from a computation with undefined variables.

\$\endgroup\$
2
  • \$\begingroup\$ @NBZ Sorry, forgot about 0. If a gets the value 0 afterwards, that's not an issue, as long as it's unused when a/a is evaluated. \$\endgroup\$ Dec 18, 2015 at 14:37
  • \$\begingroup\$ @NBZ The byte count is simply the UTF-8 byte count. \$\endgroup\$ Dec 18, 2015 at 14:37
4
\$\begingroup\$

Japt, 34 33 30 bytes

1 byte saved thanks to @ThomasKwa

T
°T
C-A
D-A
E-A
F-A
G-A
°G-A
Iq
´A
A
H+A

Here's what each of the different chars means:

T    0
A    10
B    11
C    12
D    13
E    14
F    15
G    16
H    32
I    64
q    sqrt on numbers
°    ++
´    --
\$\endgroup\$
3
  • \$\begingroup\$ Does Japt take hexadecimal numbers by default? If so, then A through F would be numeric literals... \$\endgroup\$
    – Adám
    Dec 21, 2015 at 14:10
  • \$\begingroup\$ @NBZ A-I are variables that are, by default, assigned to various numbers, as shown above. A-F are assigned to 10-15. Does this invalidate those variables? \$\endgroup\$ Dec 21, 2015 at 14:52
  • \$\begingroup\$ No. I was just wondering if, say 12AB3 was valid. Now I know it isn't. No literals here, move along. :-) \$\endgroup\$
    – Adám
    Dec 21, 2015 at 17:31
4
\$\begingroup\$

Marbelous, 98 bytes

Not terribly exciting, it relies on the ?n devices which turn any marble into a random value in the range 0..n (inclusive) a side effect of this is that ?0 turns any marble into a 0 regardless of input. I think the use of literals is permitted because the value does not affect the outcome and there is no other way to call a function once in Marbelous.

0:

00  # A hexadicemal literal: value 0
?0  # Turn any marble into a random value from the range 0..0 (inclusive)

1:

00
?0
+1  # increment by one

...

9:

00
?0
+9

10:

00
?0
+A  # increment by 10

42:

00
?0
+L  # increment by 21
+L
\$\endgroup\$
2
  • \$\begingroup\$ It sure looks like 0...L are numeric literals. \$\endgroup\$
    – Adám
    Dec 20, 2015 at 12:50
  • 2
    \$\begingroup\$ @NBZ They're not, you can't use them independently. +0 through +Z are like builtin functions. It's boring but valid. \$\endgroup\$
    – overactor
    Dec 20, 2015 at 14:10
4
\$\begingroup\$

><>, 86 bytes

  • 0: ln;
  • 1: lln;
  • 2: llln;
  • 3: lll+n;
  • 4: lll:+n;
  • 5: llll+n;
  • 6: llll++n; or llll:+n;
  • 7: lllll+n;
  • 8: lllll:+n;
  • 9: lllll++n; or llllll+n;
  • 10: llll+:+n; or lll:l+*n;
  • 42: llll*ll+*n;

Relies on stack size to get its literals.

\$\endgroup\$
7
  • \$\begingroup\$ You can probably remove the n on each because functions in stack-based languages can leave the output on the stack for -11 bytes. \$\endgroup\$ Dec 5, 2016 at 14:43
  • \$\begingroup\$ @redstarcoder then I could even remove the ; for a total of 22 bytes and consider the end of the function reached at the end of the line, but it's a little ambiguous since ><> doesn't have out-of-the-box functions. \$\endgroup\$
    – Aaron
    Dec 5, 2016 at 15:29
  • \$\begingroup\$ Technically if you want functions in ><>, your functions have to accept a return position (x & y) on the stack in addition to their parameters, make sure these aren't in the way while doing calculations ([ comes in handy in that context), then jump to the return position after having finished execution. I had done a POC a while ago, check it out if you're interested \$\endgroup\$
    – Aaron
    Dec 5, 2016 at 15:36
  • \$\begingroup\$ I've actually seen your post before, and good work! I did a meta post about the ambiguities of functions. What I'm saying is this is typically allowed, with the ;. The reason I say to leave in ; is because otherwise there's no way of denoting when the function ends without a .. Most people seem to consider this fair but I could try to write a specific meta post here if you're concerned. \$\endgroup\$ Dec 5, 2016 at 15:44
  • 1
    \$\begingroup\$ @redstarcoder thanks for the info ! I feel like suffixing the ><> snippets by . would be the best way to stick to the definition of function as described in the most upvoted answer of the meta post, however I agree ; is a good alternative which requires less explanation. \$\endgroup\$
    – Aaron
    Dec 5, 2016 at 16:39
4
\$\begingroup\$

Dyalog APL, 59 52 bytes

-7 bytes by @NBZ

I wasn't about to let one of @NBZ's questions go without an APL answer!

⍴⍬            0
⎕IO           1
⍴⎕SD          2
⎕WX           3
⍴⎕AI          4
⌈⍟⎕PW         5
⌊⍟⎕FR         6
⍴⎕TS          7
+⍨⍴⎕AI        8
⌊○○⎕IO        9; floor(pi^2 times ⎕IO)
⍴⎕D           10
⍎⌽⍕⌈*○≢#    42

In the last snippet, by NBZ, ≢# equals 1. ceil(e^(pi*1)) is calculated as 24, whose digits are then swapped.

Constants used:

  • , is the empty numeric one-dimensional vector. Therefore, its shape ⍴⍬ is 0.
  • # is a special vector of length 1.
  • ⎕IO (index origin) starts at 1.
  • ⎕AV, the character set, is of length 256.
  • ⎕PW, the print width, is 79 characters.
  • ⎕WX, window expose (whatever that is) is 3.
  • ⎕FR, the float representation, is 645. I have no idea what this is either.
  • ⎕D, "digits", is '0123456789'.
  • ⎕TS, timestamp, has seven elements: Year, month, day, hr, min, sec, ms.
  • ⎕SD, screen dimensions, has two elements: width and height.
  • ⎕AI, account info, has four elements. I don't know what they are.
\$\endgroup\$
7
  • \$\begingroup\$ Finally ;-) But you can do way better than that if you use full-fledged Dyalog APL. If you don't have one, it is easy to get. \$\endgroup\$
    – Adám
    Dec 20, 2015 at 3:14
  • \$\begingroup\$ I already had Dyalog, but I didn't know there were so many system constants! \$\endgroup\$
    – lirtosiast
    Dec 20, 2015 at 19:54
  • \$\begingroup\$ ⎕WX is a similar to ⎕ML; it controls the level of reserved-ness for names that are properties etc. of GUI objects. ⎕FR switches floats between 64 and 128 bit representation. ⎕AI is (UserName, CPUtime, SessionLength, KeyingTime. \$\endgroup\$
    – Adám
    Dec 21, 2015 at 2:16
  • \$\begingroup\$ Better, but you are forgetting to think arrays! E.g. 0 and 1 can each be written with only two chars... \$\endgroup\$
    – Adám
    Dec 21, 2015 at 2:18
  • 2
    \$\begingroup\$ 0=⍴⍬ 1=≢# 2=⍴⍬⍬ ... 42=⍎⌽⍕⌈*○≢# \$\endgroup\$
    – Adám
    Dec 31, 2015 at 18:29
4
\$\begingroup\$

DUP, 68 bytes

[
 [
  [
[)]!
[ )]!
[  )]!
[)~_]!
[ )~_]!
[  )~_]!
[   )~_]!
[)$+]!
[ )$+~_$+]!

Try it here.

There are a LOT of ways to do this, but I'm abusing the return stack for this one.

Explanation

To fully figure this out, you need to understand DUP's behavior regarding lambdas. Instead of pushing the lambda itself to the stack, it actually pushes the current IP to the stack when the lambda is detected. That can explain the first 3 snippets, which involve lambdas.

The next snippets use the return stack. When ! is executed, the current IP is pushed to the return stack, and the top of the stack is set as the new IP to start lambda execution. ) pops a number from the return stack onto the data stack.

That's pretty much enough to explain the rest of the snippets. If you still don't get it, keep in mind that the Step button is quite handy!

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

05AB1E, 40 38 24 bytes

¾
X
Y
XÌ
Y·
T;
T;>
T;Ì
TÍ
T<
T
žwT+
  • Push counter_variable
  • Push 1
  • Push 2
  • Push 1+2
  • Push 2*2
  • Push 10/2
  • Push (10/2)+1
  • Push (10/2)+2
  • Push 10-2
  • Push 10-1
  • Push 10
  • Push 32, 10, add
\$\endgroup\$
2
  • 1
    \$\begingroup\$ is more stack-clean than Yx, X can be used instead of º here (it defaults to 1, º means len(stack)>1, so it does not default to anything). Also, your bytecount is 24, not 35 (CP-1252, newlines do not count if they are separate snippets). \$\endgroup\$ Dec 4, 2016 at 20:40
  • \$\begingroup\$ I know it wasn't possible yet at the time you posted this, but 6 can be ₆t now (push 36, square-root) to save a byte (Try it online). \$\endgroup\$ Mar 29, 2019 at 10:40
3
\$\begingroup\$

Pyth, 35 34 33 bytes

-1 byte by @Mimarik

There are a number of possibilities for some programs.

0, 1 byte

Z

1, 2 bytes

hZ
!Z

2, 3 bytes

hhZ
eCG
eCd
lyd
lyb
lyN

3, 3 bytes

l`d

4, 3 bytes

l`b
eCN

5, 4 bytes

hl`b
telG

6, 3 bytes

elG

7, 4 bytes

tttT
helG

8, 3 bytes

ttT

9, 2 bytes

tT

10, 1 byte

T

42, 4 bytes

yhyT

All of these involve either basic double (y), +1 (h) and -1 (t) commands, or l (length of a string). The Z variable is initialized to zero.

For 5, b is initialized to a newline character. Backtick gives "\n" (including the quotes, and the length of that string is 4.

Try them here!

\$\endgroup\$
1

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.